• 0

Newb w/ C++ Nested If Statement Question


Question

Hello all,

I'm not looking for an exact answer, but hopefully just a nudge in the correct direction. The following code compiles fine, but when I execute it and enter a valid value (i.e. A, B, C), it always prints "Incorrect grade, please enter a valid grade" on the screen. The number of grades tallies up just fine however. Am I using an incorrect operator somewhere in my nested If statement? Is my nested if statement even correct? I know that once I figure out what is wrong, I'll wonder how I overlooked it. Any help that you could offer would be much appreciated. Thank you for your time.

- Mike


#include<stdio.h>
/*function main begins program execution*/
int main()
{

int grade = 0; /*one grade*/
int aCount = 0;/*number of A's*/
int bCount = 0;/*number of B's*/
int cCount = 0;/*number of C's*/
int dCount = 0;/*number of D's*/
int fCount = 0;/*number of F's*/

printf("Enter the letter grades.\n");
printf("Enter the EOF character to end input which is ctrl z.\n\n");


   while ( (grade = getchar() ) != EOF) {
     if ( grade  == 'A' || grade == 'a' )
	aCount++; 
     else if ( grade == 'B' || grade == 'b')
        bCount++;
     else if (grade == 'C' || grade == 'c')
	cCount++;
     else if (grade == 'D' || grade == 'd')
        dCount++;
     else if (grade == 'F' || grade == 'f')
        fCount++;
     else
        printf("Incorrect grade, please enter a valid grade.\n");
} /*end while*/

printf("A: %d\n", aCount);
printf("B: %d\n", bCount);
printf("C: %d\n", cCount);
printf("D: %d\n", dCount);
printf("F: %d\n", fCount);


}/*end main*/

5 answers to this question

Recommended Posts

  • 0

There is nothing wrong with your if statements. What is happening is that when you enter (for example) "a" and press return, both of these keys are stored in the input buffer. Your loop runs through once with grade set to 'a', then it runs through again with grade set to '\n' (the newline character), which is where the "invalid grade" is coming from. So, to fix this particular problem, you could make your "invalid grade" message print if grade isn't the '\n' character.

Edited by David Scaife
  • 0
There is nothing wrong with your if statements. What is happening is that when you enter (for example) "a" and press return, both of these keys are stored in the input buffer. Your loop runs through once with grade set to 'a', then it runs through again with grade set to '\n' (the newline character), which is where the "invalid grade" is coming from. So, to fix this particular problem, you could make your "invalid grade" message print if grade isn't the '\n' character.

That's exactly it.

You could do it this way, by adding in another else if

else if ( grade == '\n' ) 
 continue;

The continue statement jumps to the while statement.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Sparkle 2.20.0 by Razvan Serea Sparkle is a free, open-source Windows optimization tool designed to make your PC faster, cleaner, and more private. With Sparkle, you can easily debloat Windows by removing unnecessary apps and services, disable Microsoft tracking to enhance privacy, and apply performance tweaks to boost speed. Its cleaner removes junk and temporary files, while every change is safe and fully reversible. Sparkle also features a modern, user-friendly interface with automatic updates, making system maintenance simple. Explore over 39 tweaks, from disabling telemetry and hibernation to optimizing network and game settings, all aimed at customizing and enhancing your Windows experience. Sparkle supports Windows 10 and 11. Sparkle 2.20.0 changelog: Debloat Tweak has animated border New homepage loading UI New Tweak Modal (Markdown Supported) Refactored GPU Detection Added Tests with vitest Added foobar2000 to apps Added Localsend to apps Updated Modal Styles Added styles for disabled inputs Added Animated Border to debloat-windows tweak Bumped dependencies Refactor System info logic for speed Tweak info modals now support Markdown Added Clear System info cache to settings Redesigned Home Page Loading UI Changed Some Icons around the app Download: Sparkle 2.20.0 | Portable | ~100.0 MB (Open Source) Links: Sparkle Website | Github | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • lol it was a typo, fixed! haha imagine an actual 4TB Gen4 NVMe for $40 in 2026
    • Get over it, has nothing to do with my point.
    • I really don't like these rounded corners and huge borders in the viewport. They just waste space without anything in return.
  • Recent Achievements

    • Reacting Well
      Dys Topia earned a badge
      Reacting Well
    • Conversation Starter
      NovaEdgeX earned a badge
      Conversation Starter
    • One Year In
      Console General earned a badge
      One Year In
    • Week One Done
      Twozo Technologies earned a badge
      Week One Done
    • One Month Later
      Twozo Technologies earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      517
    2. 2
      +Edouard
      184
    3. 3
      PsYcHoKiLLa
      106
    4. 4
      Steven P.
      88
    5. 5
      ATLien_0
      68
  • Tell a friend

    Love Neowin? Tell a friend!