Question: C program: My code isn't displaying anything when i pick a menu option choice; i took way the flush from the function userChoice but the
C program:
My code isn't displaying anything when i pick a menu option choice; i took way the flush from the function userChoice but the code display the menu twice
please help
thank you
#define _CRT_SERCURE_NO_WARNINGS
#define PAUSE system("pause")
#define CLS system("cls")
#define FLUSH myFlush()
#include
#include
#include
#include
typedef struct{
char name[50];
float score1;
float score2;
float score3;
float score4;
} STUDENTS;
//Prototype Function
void addStudent(STUDENTS record[], int *count);
void classAverage(STUDENTS record[], int *count);
void displayMenu(void);
void studentRecords(STUDENTS record[], int *count);
void studentAverage(STUDENTS record[], int *count);
char userChoice(void);
void myFlush(void);
int main(){
STUDENTS *record;
int amount;
int count = 0;
char choice = 0;
printf("Enter the Size of the Array: ");
scanf("%i", &amount);
record = calloc(amount,sizeof(STUDENTS));
do{
choice = userChoice();
switch(choice){
case 'A':
addStudent(record,&count);
PAUSE;
break;
case 'B':
studentRecords(record,&count);
PAUSE;
break;
case 'C':
studentAverage(record,&count);
PAUSE;
break;
case 'D':
classAverage(record,&count);
PAUSE;
break;
case 'E':
printf("Thank YOU for using my Program");
PAUSE;
break;
default:
printf("invaild choice.....Please try Again");
PAUSE;
break;
}// End Switch
}while(choice != 'E');
}// END MAIN
void addStudent(STUDENTS record[], int *count){
CLS;
char name[20];
printf("Enter the name of student: ");
scanf("%s", name);
//copy the names//
strcpy(record[*count].name, name);
printf("Enter Student score 1 Grade: ");
scanf("%f", &record[*count].score1);
printf("Enter Student score 2 Grade: ");
scanf("%f", &record[*count].score2);
printf("Enter Student score 3 Grade: ");
scanf("%f", &record[*count].score3);
printf("Enter Student score 4 Grade: ");
scanf("%f", &record[*count].score4);
(*count)++;
}// END addStudent
void classAverage(STUDENTS record[], int *count){
CLS;
float sum = 0.0;
float classAvg;
for(int i = 0; i < *count; i++){
classAvg = (record[i].score1 + record[i].score2 + record[i].score3 + record[i].score4) / 4.0;
sum += classAvg;
}// end for loop
printf("The Class Average is: %.2f ",sum);
}// end classAverage
void displayMenu(){
CLS;
printf("Main Menu ");
printf("[A] Add a student ");
printf("{B} Display all student records ");
printf("{C} Display student average ");
printf("{D} Display class average ");
printf("{E} Quit ");
printf("Enter Your Selection
}// displayMenu;
void studentRecords(STUDENTS record[], int *count){
CLS;
//loop //
for(int i = 0; i < *count; i++)
//print the all values//
printf("%s\t%.2f %.2f %.2f %.2f ", record[i].name, record[i].score1, record[i].score2, record[i].score3, record[i].score4);
}// end studentRecord
void studentAverage(STUDENTS record[], int *count){
CLS;
int i;
float average = 0.0;
printf("Student Average: ");
printf("Name\tAverage ");
for(i = 0; i < *count; i++){
// calculate the average for all four test score
average = (record[i].score1 + record[i].score2 + record[i].score3 + record[i].score4) / 4.0;
printf("%s\t%.2f ", record[i].name, average);
}// end for loop
}// end studentAverage
void myFlush() {
while (getchar() != '\0');
} // end myFlush
char userChoice(){
char result;
displayMenu();
scanf("%c", &result);
FLUSH;
return toupper(result);
}// End userChoice
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
