This is the code that i have, my project is to do this exact code, but with structs. I'm obviously very shaky on the structs concepts, however, i really need to get this assignment done.
Any help in this matter would be greatly appreciated!
#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);
}