0

I'm trying to make a simple program to calculate the average GPA but during the output the statements don't stop when they are supposed to. I don't think there's any problem with the buffers in the printf statements as I'm using a new line in every sentence. This in output for example:

Enter a GPA: 
9
Do you want to calculate the average GPA until now?
Press 'y' for yes or 'n' for no: 
Enter a GPA: 
y
Do you want to calculate the average GPA until now?
Press 'y' for yes or 'n' for no: 
The average GPA is 9.0

As you can see the loop continues and it prints out the question again.

What am I doing wrong?

This is my code:

#include <stdio.h>

int main(void){

    /*************************Variable declarations************************/

    float fGPA;
    float fUserInput = 0;
    float fArray[30];
    int x;
    char cYesNo = '\0';

    /*************************Initialize array********************************/

    for(x = 0; x < 30; x++){

        fGPA = 0;
        printf("Enter a GPA: \n");
        scanf("%f", &fUserInput);
        fArray[x] = fUserInput;
        fGPA += fUserInput;
        printf("Do you want to calculate the average GPA until now?\n");
        printf("Press 'y' for yes or 'n' for no: \n");
        scanf("%c", &cYesNo);

        if(cYesNo == 'y' || cYesNo == 'Y')
            break;
        else if(cYesNo == 'n' || cYesNo == 'N')   
            continue;
    }//End for loop

    printf("The average GPA is %.1f\n", fGPA / x);

}//End main
Ilya
  • 4,583
  • 4
  • 26
  • 51
JLDG
  • 13
  • 5
  • 1
    You should print the value of `cYesNo` just to be sure of what is in. – Mathieu Jun 09 '16 at 09:01
  • `'\n'` != `'y'`, `'y'` is not a valid floating-point number to convert. – EOF Jun 09 '16 at 09:06
  • You do realize that the whole block `else if(cYesNo == 'n' || cYesNo == 'N') continue;` does nothing, yeah? – Lundin Jun 09 '16 at 09:22
  • @Lundin: If you're going to critique the program in general, why not mention the possible floating-point division by zero, and the zeroing of the accumulator in the loop? – EOF Jun 09 '16 at 09:24

2 Answers2

3

Reason: this happens due to the white space i.e, '\n' character entered at the end of inputting fUserInput

    scanf("%f", &fUserInput);

this '\n' is consumed by the %c in scanf("%c", &cYesNo);


Solution:

avoid it by giving a space before %c in scanning cYesNo to consume any white spaces

    scanf(" %c", &cYesNo);

Why to give a space?

By giving a space,the compiler consumes the '\n' character or any other white space ('\0','\t' or ' ' ) from the previous scanf()


Suggestion

next time if you encounter such problem... try printing the ascii values scanned by the character this way:

printf("%d",(int)cYesNo); //casting char->int

and check your output against ascii table : here

for example :

  • output would be 32 if it's a ' ' //space
  • output would be 10 if it's a '\n' //new-line
  • output would be 9 if it's a '\t' //horizontal-tab

this way you'd know what's being scanned into the character and if it's a whitespace avoid it by the above method :)

Cherubim
  • 5,287
  • 3
  • 20
  • 37
3

You need to replace

scanf("%c", &cYesNo);

by

scanf(" %c", &cYesNo);

for reasons detailled here: How to do scanf for single char in C

Community
  • 1
  • 1