0

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.

5
  • Please post the output. I suspect that you are not seeing the prompt because you need to call 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). Commented Feb 13, 2013 at 22:19
  • 1
    Runs fine on my Mac (clang) and Windows (vc2012). Commented Feb 13, 2013 at 22:22
  • Hard to imagine any C I/O library that doesn't flush output before reading input. Commented Feb 13, 2013 at 22:42
  • Your code assumes that you will enter a grade of "-1" to terminate the first while loop. Is that what you entered? Commented Feb 13, 2013 at 23:01
  • yes the "-1" to end is what we are using currently in our introduction class so I figured I would stick to it for now. Commented Feb 13, 2013 at 23:30

1 Answer 1

2

A common problem you can run into is output buffering. In the case of your code:

printf( "Enter Assignment Grade, -1 to end: "); /* prompt for input */ scanf( "%d", &gradeA ); 

Your terminal might not flush the output buffer to screen. So it will wait for input, but you may not see the prompt. To force it, you can do this:

printf( "Enter Assignment Grade, -1 to end: "); /* prompt for input */ fflush(stdout); scanf( "%d", &gradeA ); 

Now, that makes for more repetition. It would be better if you only had to write the prompt once instead of twice for each loop. You can get around that by just initialising gradeA to zero before the loop. Then the first calculations inside the loop don't have any effect:

gradeA = 0; totalA = -1; while (gradeA != -1) { totalA = totalA + gradeA; countA = countA + 1; printf( "Enter Assignment Grade, -1 to end: "); fflush(stdout); scanf( "%d", &gradeA ); } 

Notice I set countE to -1 to undo the fact that you increment it at the top of the loop. This all starts to feel a bit clumsy.

The other problem is that if the user enters something that isn't an integer, your program will produce undefined behaviour (because you don't actually initialise gradeA). You can test whether scanf was successful because it returns how many items it read.

The lazy approach is this:

if( 1 != scanf( "%d", &gradeA ) ) { printf( "Invalid input!"\n" ); exit(1); } 

You can decide. I'll sort of ignore that for the rest of the answer.

As you beef up your loops, it becomes apparent that code repetition is a theme. All you are changing is the variables and the prompt. So it could be time to turn it into a function:

int grade_average( const char *what, int *total, float *average ) { int count = -1; int grade = 0; *total = 0; while( grade != -1 ) { count++; *total += grade; printf( "Enter %s Grade, -1 to end: ", what ); fflush(stdout); if( 1 != scanf("%d", &grade) ) return 0; // fail on input error. } if( count > 0 ) { *average = (float)*total / count; } return 1; } 

Now you can call like this:

if( grade_average("Assignment", &totalA, &averageA) ) { printf( "Total is %.2f\n", averageA ); } if( grade_average("Exam", &totalE, &averageE) ) { printf( "Total is %.2f\n", averageE ); } if( grade_average("Quiz", &totalQ, &averageQ) ) { printf( "Total is %.2f\n", averageQ ); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help, I'm trying to understand most of what you told me here and so it might take me a while for me to understand it, but I must ask will this allow me to continue entering data after the Assignment section has been entered?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.