Question: Please help me edit my code using designated functions instead of having everything in the main module. My instructions are as followed. Your program must
Please help me edit my code using designated functions instead of having everything in the main module.
My instructions are as followed.
Your program must implement the functions whose prototypes are given below. Feel free to add more functions whenever you see fit. void getId( int *pId ); This function asks the user to enter the students id that is stored in *pId int checkNumberOfCourses ( int numCourses ); This function returns 0 if numCourses is not a valid number of courses (recall a student takes 0,1 or 2 courses). It returns 1 otherwise. int checkCourseNumber ( int crn ); This function returns 0 if the passed crn isnt from the list of allowable crns. It returns 1 otherwise. int getCreditHours ( int crn ) This function uses a switch structure and returns the number of credit hours that go with the passed crn. For instance getCreditHours (4599 ) returns 3 void printInvoiceLine ( int crn, int creditHours); This function, when called, prints something like: 4599 3 $ 360.75 if crn is 4599 and creditHours is 3. This function should be called at most two times because a student takes up to two courses. void printInvoice ( int id, int crn1, int crn2 ); This function prints the fee invoice. Note that this function may call printInvoiceLine. Furthermore, make sure that your program doesnt have duplicate code; if you have duplicate code then you should use a function that you call twice or more to avoid duplication of code.
Here is my code.
#include
#include
#include
int main ()
{
int id,hours, hours1=0,hours2=0,courses, c1, c2, credits4587=4, credits4599=3, credits8997=1, credits9696=3; //Declaration of variables
float fee1,fee2,health=35.0,tot;
//Input Student id
printf("Enter the student Id: (NUMBERS ONLY) ");
scanf("%d", &id);
//Input number of courses below
printf("Enter how may courses taken(up to 2): ");
scanf("%d", &courses);
//loop validating input for the amount of courses
while (courses > 2 || courses < 0)
{
printf ("Invalid course amount. Please reenter your amount of courses taken (up to 2). ");
scanf("%d", &courses);
}
if (courses == 0) //actviates if the user is not taking any courses and exits the program
{
printf ("You are taking no courses. Enjoy your freedom, and have a great day! ");
return 0;
}
else //assigns the correct data for the proper output based on user input using a switch case
{
printf ("Enter the list of course numbers separated by the enter key ");
switch(courses)
{
case 1:
//receives input for the course
scanf("%d",&c1);
while ((c1 != 4587) && (c1 != 4599) && (c1 != 8997) && (c1 != 9696)) //validates course number input
{
printf ("Invalid course number. Please enter a proper course number. ");
scanf("%d",&c1);
}
switch(c1)
{
case 4587:
hours1=4;
break;
case 4599:
hours1=3;
break;
case 8997:
hours1=1;
break;
case 9696:
hours1=3;
break;
default:
hours1=0;
break;
}
hours=hours1;
fee1=hours*120.25;
tot=fee1+health;
break;
case 2:
//receives input for course 1
scanf("%d",&c1);
//validates input for course 1
while ((c1 != 4587) && (c1 != 4599) && (c1 != 8997) && (c1 != 9696))
{
printf ("Invalid course number for course 1. Please enter a proper course number. ");
scanf("%d",&c1);
}
//recieves input for course 2
printf ("Enter the course number for your second course. ");
scanf("%d",&c2);
//validates input for course 2
while ((c2 != 4587) && (c2 != 4599) && (c2 != 8997) && (c2 != 9696))
{
printf ("Invalid course number for course 2. Please enter a proper course number. ");
scanf("%d",&c2);
}
switch(c1)
{
case 4587:
hours1=4;
break;
case 4599:
hours1=3;
break;
case 8997:
hours1=1;
break;
case 9696:
hours1=3;
break;
default:
hours1=0;
break;
}
switch(c2)
{
case 4587:
hours2=4;
break;
case 4599:
hours2=3;
break;
case 8997:
hours2=1;
break;
case 9696:
hours2=3;
break;
default:
hours2=0;
break;
}
hours=hours1+hours2;
fee1=hours1*120.25;
fee2=hours2*120.25;
tot=fee1+fee2+health;
break;
default:
break;
}
}
printf(" VALENCE COMMUNITY COLLEGE");
printf(" ORLANDO FL 10101");
printf(" *************************");
printf(" Fee Invoice Prepared for Student %d ", id);
printf(" 1 Credit Hour = $120.25 ");
printf("CRN\tCREDIT HOURS ");
printf(" %d\t\t%d\t\t$%0.2f ", c1, hours1, fee1);
if (courses>1)
{
printf(" %d\t\t%d\t\t$%0.2f ", c2, hours2, fee2);
}
printf("\t\tHealth & id fees $ 35.00 ");
printf("---------------------------------------------- ");
printf("Total Payments $ %0.2f ", tot);
getch();
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
