I'm completely new in programming and I recently found this site to help educate my self. I am trying to make a program using "C" that prompts for grades in three different categories in my book I ran into while statements and figured they where the best choice for me to do this with. My current code looks something like this.
int countA; int gradeA; int totalA; int weightA; float averageA; int countE; int gradeE; int totalE; int weightE; float averageE; int countQ; int gradeQ; int totalQ; int weightQ; float averageQ; totalA = 0; countA = 0; totalE = 0; countE = 0; totalQ = 0; countQ = 0; printf( "Enter Assignment Grade, -1 to end: "); /* prompt for input */ scanf( "%d", &gradeA ); while (gradeA != -1){ totalA = totalA + gradeA; /* add gradeA to totalA */ countA = countA + 1; printf( "Enter Assignment Grade, -1 to end: "); /* prompt for input */ scanf( "%d", &gradeA ); } if (countA != 0) { averageA = (float) totalA / countA; printf( "total is %.2f\n", averageA ); } printf( "Enter Exam Grade, -1 to end: "); /* prompt for input */ scanf( "%d", &gradeE ); while (gradeE != -1){ totalE = totalE + gradeE; /* add gradeE to totalE */ countE = countE + 1; printf( "Enter Exam Grade, -1 to end: "); /* prompt for input */ scanf( "%d", &gradeE ); } if (countE != 0) { averageE = (float) totalE / countE; printf( "total is %.2f\n", averageE ); } printf( "Enter Quiz Grade, -1 to end: "); /* prompt for input */ scanf( "%d", &gradeQ ); while (gradeQ != -1){ totalQ = totalQ + gradeQ; /* add gradeQ to totalQ */ countQ = countQ + 1; printf( "Enter Quiz Grade, -1 to end: "); /* prompt for input */ scanf( "%d", &gradeQ ); } if (countQ != 0) { averageQ = (float) totalQ / countQ; printf( "total is %.2f\n", averageQ ); } Now to what I am trying to do after this point is repeat the process two more times, however when I try to run the exe I get the 1st part to run but the other two parts simply do not get brought up for some reason. Is this simply do to a limitation on what while repetition statements do? or do I have an error some where. I'm trying to figure out what I am doing wrong but I just see it.
I'm not quite sure on how to post properly here just yet but this is an example of the output i get.
here is an example of my output, as you can see it prompts me for the 1st segment which is the Assignment part, but after entering the -1 to end the loop it just gives me the average and ends.
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin>project1.exe Enter Assignment Grade, -1 to end: 100 Enter Assignment Grade, -1 to end: 80 Enter Assignment Grade, -1 to end: 77 Enter Assignment Grade, -1 to end: 33 Enter Assignment Grade, -1 to end: 76 Enter Assignment Grade, -1 to end: 92 Enter Assignment Grade, -1 to end: -1 total is 76.33 I figure that at least the first part is working but after I get the total I am not prompted for the next look then asks for the Exam grades.
fflush(stdout). A lot of terminals are line-buffered and will not flush output to screen by default unless you write a newline (or explicitly flush).