Also,
vlad from moscow is correct in his response. You
should pass a reference to the last call to printf(), although you
are not required to pass printf a pointer, you may pass it a value. (so your last printf should work just fine.)
@vlad from moscow
I would argue that the problem was not resolved, considering the OP seemed frustrated and never explicitly said the problem was resolved. In fact, he said the problem
wasn't resolved.
Even though you were correct about having to pass a reference to scanf_s(), you completely ignored what the debugger was blatantly telling you. If the OP's variables aren't initialized, this statement:
sum=sum+grade;
will cause errors because
sum wasn't initialized.
Initialize your variables. Always. Period.
EDIT:
@OP
So it asks you to put in a grade? Good, you have that in your code. It only asks you to put in one grade? That's because you only execute that code once.
The reason nothing outputs is because 'x' will always equal 1 (because that code is only executed once), so you never meet your if statement requirements to output something (x == 30). Instead, you return 10, which ends your program and tells the OS that there was a problem.
If you want to take multiple grades from a user, you need to use a loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
float sum = 0, avg = 0;
int x = 0;
bool done = false;
while(!done)
{
float grade = 0;
printf("Put in a Grade (enter negative number to end entry): ");
scanf_s("%f", &grade);
if(grade < 0)
done = true; //end loop, invalid grade entered.
sum += grade; //add grade to sum
++x; //increment x
}
//then calculate average after this loop, and output it.
|