Chapo Chapo - 1 month ago 14
C Question

Display Full Value From String Array in C

I am completely stumped on this one. I am attempting to create a simple grade calculator. Yes this is a school assignment. However, there are things called deadlines, and my teachers make them impossible to meet, so I've already turned in my mostly-working program for grading. But I am a curious man and my teacher couldn't answer my question - so here I am.

Within my project everything works well; however, when printing my output the first student's name is never fully displayed. I cannot for the life of me even begin to understand how every other loop produces the name without issue, but the first iteration. What on earth have I done wrong?

I beleive that my issue is somewhere within my variable declarations or this for statement. Any help would be appreciated, and please feel free to critique. I won't learn any other way.

for (int m=0; m<numstudents; m++) {
printf("\n\n*****************\n\n***Report card***\n\n*****************");
printf("\nName of student: %s", namestudent[m]);
printf("\nPhysics: %.2f/100", physicsstudent[m]);
printf("\nChemistry: %.2f/100", chemistrystudent[m]);
printf("\nMath: %.2f/100", physicsstudent[m]);

total = physicsstudent[m]+chemistrystudent[m]+mathstudent[m];
realtotal = (total/300)*100;
printf("\nTotal: %.2f/300", total);

if(realtotal > 89) {
printf("\nYour letter grade: A");
} else if(realtotal > 79) {
printf("\nYour letter grade: B");
} else if(realtotal > 69) {
printf("\nYour letter grade: C");
} else if(realtotal > 49) {
printf("\nYour letter grade: D");
} else if((realtotal) < 50) {
printf("\nYour letter grade: F");
}
}


Entire code:

#include <stdio.h>
#include <string.h>

int main() {
char line[100][100], namestudent[20][20];
int numstudents, grade;
float realtotal, total, physicsstudent[100], chemistrystudent[100], mathstudent[100];

printf("\n***Welcome to the grade calculator***\n\n=>Press G to enter grade calculator\n\n=>Press H for help\n\nPlease enter your option: ");
char userinput = getchar();

if (userinput == 'g' || userinput == 'G') {
printf("\nWelcome to the grade calculator\nEnter the total number of students in your class: ");
scanf("%d", &numstudents);

if (numstudents <= 0) {
printf("Terminating program");
return(0);
}

for (int i=0; i<numstudents; i++) {
printf("\nPlease enter the name of student %d: ", i+1);
fgets(namestudent[i], sizeof(namestudent[i]), stdin);
namestudent[strlen(namestudent[i])-1][i] = '\0';
scanf("%s", namestudent[i]);

printf("Please enter %s\'s Physics score (out of 100 points): ", namestudent[i]);
scanf("%f", &physicsstudent[i]);

printf("Please enter %s\'s Chemistry score (out of 100 points): ",namestudent[i]);
scanf("%f", &chemistrystudent[i]);

printf("Please enter %s\'s Math score (out of 100 points): ",namestudent[i]);
scanf("%f", &mathstudent[i]);

}

for (int m=0; m<numstudents; m++) {
printf("\n\n*****************\n\n***Report card***\n\n*****************");
printf("\nName of student: %s", namestudent[m]);
printf("\nPhysics: %.2f/100", physicsstudent[m]);
printf("\nChemistry: %.2f/100", chemistrystudent[m]);
printf("\nMath: %.2f/100", physicsstudent[m]);

total = physicsstudent[m]+chemistrystudent[m]+mathstudent[m];
realtotal = (total/300)*100;
printf("\nTotal: %.2f/300", total);

if(realtotal > 89) {
printf("\nYour letter grade: A");
} else if(realtotal > 79) {
printf("\nYour letter grade: B");
} else if(realtotal > 69) {
printf("\nYour letter grade: C");
} else if(realtotal > 49) {
printf("\nYour letter grade: D");
} else if((realtotal) < 50) {
printf("\nYour letter grade: F");
}
}

} else if (userinput == 'h' || userinput == 'H') {
printf("~~~Help~~~\n\n*After typing G, you will be entering the grade calculator\n\n1.Then, you will be asked to enter the number of students that you are going to enter grades for.\nThis will be an integer value only.\n\n2.If you enter any other character (other thaninteger), you must prompt to the user to enter a valid integer number only.\n\n3.Then the calculator asks you for the names of the students (if you entered 3 in the step 1, the loop will run for 3 times....taking 3 student names).\n\n4.Then the program will start asking for the individual scores (physics, chemistry and math in our case).\n\n5.Finally, it will print a report card with scores and the grade (calculated). I have programmed to give A, B, C and F grades only.You are free to use other grading scales as well.");
} else {
printf("You entered a value that is not accepted. Terminating program");
}
return(0);
}


Results:

*****************

***Report card***

*****************
Name of student: a <--- input was 'apple'
Physics: 90.00/100
Chemistry: 90.00/100
Math: 90.00/100
Total: 270.00/300
Your letter grade: A

*****************

***Report card***

*****************
Name of student: orange
Physics: 90.00/100
Chemistry: 90.00/100
Math: 90.00/100
Total: 270.00/300
Your letter grade: A

*****************

***Report card***

*****************
Name of student: banana
Physics: 90.00/100
Chemistry: 90.00/100
Math: 90.00/100
Total: 270.00/300
Your letter grade: A

Process exited with code: 0

Answer

The fgets and/or assignment below it are mucking things up; take them out, and just use scanf as you do with the rest.