do-while loop issue

Hello! I am new to c++ and I am trying to use a do-while loop with strictly if-else nested statement. But I am encountering an issue where my loop does not break after pressing Cntrl Z which is my condition. Below is my coding, I hope someone can help me!

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{
unsigned int a = 0, b = 0, c = 0, d = 0, f = 0;
char grade;
float average, total = 0;

puts("Enter the letter grades (EOF character, Crtl+z, to end):");

do
{
scanf("%c", &grade);
if (grade == 'A' || 'a')
{
++a;
total += 4;
}
else if (grade == 'B' || 'b')
{
++b;
total += 3;
}
else if (grade == 'C' || 'c')
{
++c;
total += 2;
}
else if (grade == 'D' || 'd')
{
++d;
total += 1;
}
else if (grade == 'F' || 'f')
{
++f;
total += 0;
}
} while (grade != EOF);

average = total / (a + b + c + d + f);

printf("\nThe totals for each grade are:\nA: %u\nB: %u\nC: %u\nD: %u\nF: %u\n", a, b, c, d, f);
if (average > 3.5)
puts("Average grade is A.");

else if (average >= 2.5)
puts("Average grade is B.");

else if (average >= 1.5)
puts("Average grade is C.");

else if (average >= 0.5)
puts("Average grade is D.");

else
puts("Average grade is F.");
}
Last edited on
First, that code is C, it just so happens to also compile with C++. Just needed to clarify that.
If you want to actually learn C++, let us know and we can direct you to better places (like this site's tutorial).

scanf is not going to be put EOF in the grade variable, that's not how it works. It returns EOF if it reads EOF from the input. So you need to check the return value of scanf.
https://www.csd.uoc.gr/~math106/slides/scanf_Tutorial.pdf

Also, each condition in an if-statement must be an independent clause.
You need to do else if (grade == 'B' || grade == 'b') etc.
Last edited on
Oops! So sorry, didn't know about that, thank you for clarifying. So what should I use so that EOF can be read? getchar()? Thanks for the help, really appreciate it
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>

int main(void)
{
    char grade;

    puts("Enter the letter grades (EOF character, Crtl+z, to end):");

    while (scanf("%c", &grade) == 1)
    {
        printf("%c", grade);
    }
}
Last edited on
so scanf does not work with EOF? I tried to change it but it is still not breaking out of the loop
I changed my scanf into getchar and it works but now I am encountering an issue where only grade A is being counted...

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{
unsigned int a = 0, b = 0, c = 0, d = 0, f = 0;
char grade;
float average, total = 0;

puts("Enter the letter grades (EOF character, Crtl+z, to end):");

while ((grade=getchar()) != EOF)
{
if (grade == 'A' || 'a')
{
++a;
total += 4;
}
else if (grade == 'B' || grade == 'b')
{
++b;
total += 3;
}
else if (grade == 'C' || grade == 'c')
{
++c;
total += 2;
}
else if (grade == 'D' || grade == 'd')
{
++d;
total += 1;
}
else if (grade == 'F' || grade == 'f')
{
++f;
total += 0;
}
}

average = total / (a + b + c + d + f);

printf("\nThe totals for each grade are:\nA: %u\nB: %u\nC: %u\nD: %u\nF: %u\n", a, b, c, d, f);
if (average > 3.5)
puts("Average grade is A.");

else if (average >= 2.5)
puts("Average grade is B.");

else if (average >= 1.5)
puts("Average grade is C.");

else if (average >= 0.5)
puts("Average grade is D.");

else
puts("Average grade is F.");
}
oops all is good , I forgot to change for the grade A as you mentioned earlier, i didn't put grade == 'a. Thanks so much!
You didn't change if (grade == 'A' || 'a') to
if (grade == 'A' || grade == 'a')

PS: What I showed earlier probably didn't work because "%c" doesn't consume whitespace, I forgot about that.
[I'm not the best at C I/O]
Last edited on
Topic archived. No new replies allowed.