Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to redo the same code using structs

fflush

  • Please log in to reply
4 replies to this topic

#1 sealearner

sealearner

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 26 July 2009 - 06:48 AM

I have this code that i am need to redo using structs. The basic format of the code should stay the same, just use structs to run the exact same code.

I've only manage to get as far as the struct declaration with the fields, and now i'm stuck.

any help on this will be greatly appreciated. thanks

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 5
#define LENGTH 41
#define COSTPERCREDITHOUR 120.25
#define ID_FEE 35.00

//need to do same project using only structs
typedef struct{
	int id;
	int crn[4];
	int name[40];
}STUDENT;

//void addStudent (char name[SIZE][LENGTH], int id[SIZE], int crn[SIZE][4]);
void addStudent(STUDENT []);
void menu (int *option);
int linearSearch (int id[SIZE], int key);
void addDelete (char name[SIZE][LENGTH], int crn[SIZE][4], int location);
void getInfo (int crn, char prefix[7], int *hours);
void printInvoice (int id[SIZE], char name[SIZE][LENGTH], int crn[SIZE][4], int location);
void sortInvoice (int id[SIZE], char name[SIZE][LENGTH], int crn[SIZE][4], int location);
//------------------------------------------------------------------------------
int main ()
{
    char name[SIZE][LENGTH] = {""};
    int id[SIZE] = {0};
    int crn[SIZE][4] = {{0}};
    char invoice;
	
    int option = 0, loop = 0, key = 0, location = 0;   
	
	while (loop == 0)
	 {
		 menu (&option);
		 
		 switch (option)
	  {
		  case 1:
	   {
		   addStudent (name, id, crn);                    
		   break;
	   }
		  case 2:
	   {
		   printf ("Enter the students ID: ");
		   scanf  ("%d", &key);
		   location = linearSearch (id, key);
		   addDelete (name, crn, location);
		   printf ("\nWould you like to display a new invoice? (Y/N): ");
		   fflush (stdin);
		   scanf  ("%c", &invoice);
		   if (invoice == 'y' || invoice == 'Y')
			{
				printInvoice (id, name, crn, location);
			}
		   break;
	   }
		  case 3:
	   {
		   printf ("Enter the students ID: ");
		   scanf  ("%d", &key);
		   location = linearSearch (id, key);
		   if (location == -1)
			   printf ("\nNo student found!\n");
		   else
			   printf ("\nStudent found @ index location %d.\n", location);
		   break;
	   }
		  case 4:
	   {
		   printf ("Enter the students ID: ");
		   scanf  ("%d", &key);
		   location = linearSearch (id, key);
		   printInvoice (id, name, crn, location);
		   break;
	   }
		  case 5:
	   {
		   printf ("Enter the students ID: ");
		   scanf  ("%d", &key);
		   location = linearSearch (id, key);
		   sortInvoice (id, name, crn, location);
		   break;
	   }
		  case 0:
	   {
		   loop=1;
		   break;
	   }
		  default:
	   {
		   printf ("Invalid option, try again.\n");
		   break;
	   }
			  
	  }
	 }  
	
	printf ("Goodbye!\n\n\n");
    
    system ("pause");
    return 0;
}/*end main function*/

void addStudent (char name[SIZE][LENGTH], int id[SIZE], int crn[SIZE][4])
{
	char temp[LENGTH];
	int i=0 , n , j;
	int finder;
	
	
	printf("Enter the ID: ");
	scanf("%d", &finder);
	while ( i < 4 && finder != id[i])
		i++;
	if(finder == id[i])
     {
		 printf("\nThe ID %d is already assigned to a student", finder);
		 return;
     }else if (finder != id[i])
	  {
		  
		  
		  printf ("Enter the name of the student: ");
		  //fflush (stdin);
		  gets (temp);
		  gets (temp);
		  
		  for ( i=0; i<SIZE; i++)
		   {
			   if ( strcmp (name[i] , "") == 0 )
				{
					id[i] = finder;
					strcpy ( name[i], temp);
					printf ("Enter how many CRNs %s is taking: ", temp);
					scanf  ("%d", &n);
					if (n > 4)
					 {
						 printf ("\nYou cannot take more than 4 classes.\n");
						 strcpy (name, "");
						 id[i] = 0;
						 return;
					 }
					
					else if (n < 0) 
					 {
						 printf ("\nYou must take at least one class.\n");
						 strcpy (name, "");
						 id[i] = 0;
						 return;
					 }
					
					else
					 {
						 printf ("Enter the %d CRNs: ", n);
						 for (j=0; j<n ; j++)
						  {
							  scanf ("%d", &crn[i][j]);
						  }
						 printf ("\nStudent added!\n");
						 break;
					 }
				}
		   }
	  }
}/*end addStudent function*/


void menu (int *option)
{
	printf ("\tWelcome!\n");
	printf ("Choose from the following options:\n\n");
	printf("=============================================\n");
	printf ("1 - Add a new student\n");
	printf ("2 - Add/delete a course\n");
	printf ("3 - Search for a student\n");
	printf ("4 - Print fee invoice\n");
	printf ("5 - Print fee invoice sorted by CRN\n");
	printf ("0 - Exit program\n");
	printf ("\nEnter your selection: ");
	scanf  ("%d", option);
	
}/*end menu option*/


int linearSearch (int id[SIZE], int key)
{
    int i;
    int location = -1;
    
    for ( i= 0; i<SIZE; i++)
	 {
		 if (id[i] == key )
		  {
			  location = i; 
			  break;
		  }
	 }
    
    return location;
}/*end linearSearch function */

void addDelete (char name[SIZE][LENGTH], int crn[SIZE][4], int location)
{
	int i, temp;
	//char invoice;
	char prefix[7];
	int hours;
	char selection;
	
	printf ("\nHere are the courses [%s] is taking: ", name[location]);
	printf ("\nCRN\tPREFIX\tCR HOURS");
	for ( i= 0; i<4; i++)
     {
         if (crn[location][i] != 0)
		  {    
			  getInfo ( crn[location][i], prefix , &hours);
			  printf ("\n%d\t\t%s\t\t%d", crn[location][i], prefix, hours);
		  }
         else
			 break;
     }
	printf ("\n\nChoose from:");
	printf ("\nA - Add a new course for [%s]", name[location]);
	printf ("\nD - Delete a course from [%s]'s schedule", name[location]);
	printf ("\nC - Cancel operation");
	printf ("\nEnter your selection here: ");
	fflush (stdin);
	scanf  ("%c", &selection);
	
	if (selection == 'A' || selection == 'a')
     {
		 
		 for ( i= 0; i<4; i++)
          {
              if (crn[location][i] == 0)
			   {
				   printf ("\nEnter course number to add: ");
				   scanf  ("%d", &crn[location][i]);
				   getInfo (crn[location][i], prefix, &hours);
				   printf ("\n[%d %s] successfully added!\n", crn[location][i], prefix);
				   break;
			   }
              
              if (i==3 && crn[location][i] != 0)
			   {
				   printf ("\nYou cannot have more than 4 courses.");
				   return;
			   }
          }
		 
		 
     }
	
	else if (selection == 'D' || selection == 'd')
     {
		 printf ("\nEnter course number to delete: ");
		 scanf  ("%d", &temp);
		 for ( i= 0; i<4; i++)
          {    
              if (temp == crn[location][i])
			   {
				   getInfo (crn[location][i], prefix, &hours);
				   printf ("\n[%d %s] successfully deleted!\n", crn[location][i], prefix);
				   crn[location][i]=0;
				   while ( i<4)
					{
						crn[location][i] = crn[location][i+1];
						i++;
					}
				   break;
			   }
          }
     }       
	else if (selection == 'C' || selection == 'c')
     {
		 return;
     }
	else
     {
         printf ("\nInvalid selection.");
     }
	
	
	
	
}

void getInfo (int crn, char prefix[7], int *hours)
{ 
    switch (crn)
 {
	 case 4587:
  {
	  strcpy (prefix, "MAT 236");
	  *hours = 4;
	  break;
  }
	 case 4599:
  {
	  strcpy (prefix, "COP 220");
	  *hours = 3;
	  break;
  }
	 case 8997:
  {
	  strcpy (prefix, "GOL 124");
	  *hours = 1;
	  break;
  }
	 case 9696:
  {
	  strcpy (prefix, "COP 100");
	  *hours = 3;
	  break;
  }
 }
}

void printInvoice (int id[SIZE], char name[SIZE][LENGTH], int crn[SIZE][4], int location)
{
	
    double total = 0;
    int hours;
    char prefix[7];
    int i;
    double subTotal;
    
    total = ID_FEE;
	
    printf ("\n\n\n\n\t\tVALENCE COMMUNITY COLLEGE");
    printf ("\n\t\tORLANDO FL 10101");
    printf ("\n\t\t*************************");
    printf ("\n\n\t\tFee Invoice Prepared for Student:");
    printf ("\n\t\t%d - [%s]", id[location], name[location]);
    printf ("\n\n\t\t1 Credit Hour = $%.2lf", COSTPERCREDITHOUR);
    printf ("\n\n\t\tCRN\tPREFIX\t    HOURS");
    for ( i= 0; i<4; i++)
	 {
         if (crn[location][i] != 0)
		  {
			  getInfo ( crn[location][i], prefix , &hours);
			  subTotal = hours * COSTPERCREDITHOUR;
			  printf ("\n\t\t%d\t%s\t\t%d\t$%.2lf", crn[location][i], prefix , hours, subTotal);
			  
			  total = total + subTotal;
		  }else if (crn[location][i] == 0)
			  break;
	 }
    printf ("\n\n\t\t\tHealth & ID Fees\t$ %.2lf", ID_FEE);
    printf ("\n\n\t\t---------------------------------------");
    printf ("\n\t\t\tTotal Payments\t\t$%.2lf\n\n", total);     
}

void sortInvoice (int id[SIZE], char name[SIZE][LENGTH], int crn[SIZE][4], int location)
{
	double total = 0;
    int hours;
    char prefix[7];
    int i;
    double subTotal;
    int j, temp ;
	
	
    for ( j= 0; j<SIZE ; j++)
	 {
		 for ( i=0; i<=SIZE-2 ; i++)
		  {
			  if ( crn[location][i] > crn[location][i+1] && crn[location][i+1] != 0 )
			   {
				   temp = crn[location][i];
				   crn[location][i] = crn[location][i+1];
				   crn[location][i+1] = temp;
			   }
		  }
	 }
    
    
    total = ID_FEE;
	
    printf ("\n\n\n\n\t\tVALENCE COMMUNITY COLLEGE");
    printf ("\n\t\tORLANDO FL 10101");
    printf ("\n\t\t*************************");
    printf ("\n\n\t\tFee Invoice Prepared for Student:");
    printf ("\n\t\t%d - [%s]", id[location], name[location]);
    printf ("\n\n\t\t1 Credit Hour = $%.2lf", COSTPERCREDITHOUR);
    printf ("\n\n\t\tCRN\tPREFIX\t    HOURS");
    for ( i= 0; i<4; i++)
	 {
         if (crn[location][i] != 0)
		  {
			  getInfo ( crn[location][i], prefix , &hours);
			  subTotal = hours * COSTPERCREDITHOUR;
			  printf ("\n\t\t%d\t%s\t\t%d\t$%.2lf", crn[location][i], prefix , hours, subTotal);
			  
			  total = total + subTotal;
		  }else if (crn[location][i] == 0)
			  break;
	 }
    printf ("\n\n\t\t\tHealth & ID Fees\t$ %.2lf", ID_FEE);
    printf ("\n\n\t\t---------------------------------------");
    printf ("\n\t\t\tTotal Payments\t\t$%.2lf\n\n", total);     
}

  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 26 July 2009 - 11:50 AM

I'm confused. The code you posted is using structs. Does this need to be in C, C++, other?
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 sealearner

sealearner

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 26 July 2009 - 12:48 PM

it needs to be in C.
Basically this section:
typedef struct{
	int id;
	int crn[4];
	int name[40];
}STUDENT;
is all that needs to be in the code. So all the other functions that were used previously should be taken out and functions created using 'typedef' or based on 'typedef' are to be used. I hope this makes some sense

Sorry if the post was not clear enough :)
  • 0

#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 26 July 2009 - 02:34 PM

OK, so you're bringing the 3 items into a struct. Then to use them, you have to prefix the items with the variable name that is of type struct. For example, if you declare an array you access the first element as follows:
  STUDENT[10] students;
  students[0].id = 12;
  students[0].crn = "hiya";
  students[0].name = "Robert Jones";

  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#5 sealearner

sealearner

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 28 July 2009 - 03:41 AM

Thank you
  • 0





Also tagged with one or more of these keywords: fflush

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download