I'm trying to get this code to calculate a class's average GPA. The problem I'm having is that I seem to have made a mistake in my do...while
code, because, when I run it, it constantly loops back to asking me to input another GPA, rather than asking if I want to calculate the average or not.
#include<stdio.h>
int main()
{
float faGPA[30], fSum = 0, fAvg = 0;
int x, y;
char cResp = '\0';
printf("\t\tGPA Calculator\n");
printf("\nEnter up to 30 GPAs into the calculator.\n");
do{
printf("\nEnter a GPA: ");
scanf("%f", &faGPA[x]);
x++;
printf("\nCalculate the GPA average (Y/N)?\n");
scanf("%c", &cResp);
} while(x < 30 && cResp != 'Y' || x < 30 && cResp != 'y');
for(y = 0; y < (x + 1); y++)
fSum += faGPA[y];
fAvg = (fSum / (y - 1));
printf("\nThe class GPA is:%.2f", fAvg);
return 0;
}
There are two issues here. First, you need to discard new lines on your scanf. See here for details.
Second the || operator is going to cause the whole statement to evaluate to true no matter if the user has entered Y or y. Try switching to an && operator and closing the two checks in their own parenthesis.
See sample below - it should at least get you further, though I'm still not getting the right answer from you math.