• 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

    • And none of them have a "notchless" 3:2 OLED screen, for which I was always willing to pay more.
    • Remember when the PS2 was banned from export because "its too high tech".. "it could be used for missiles".....
    • Nvidia 610.62 driver lands with big bug fixes and Empulse support by Pulasthi Ariyasinghe There is a new driver available for Nvidia GeForce hardware owners, and it's carrying a whole lot of bug fixes. The WHQL-certified 610.62 Game Ready driver is also adding support for Empulse. Empulse lands from 1047 Games. That may be familiar to first-person shooter fans, as this is the studio that has been behind Splitgate and the Splitgate: Arena Reloaded sequel. This latest FPS entry will land into early access on June 24, and it will have support for DLSS 4.5 with dynamic multi-frame generation and Nvidia Reflex on day one. FIXED World of Warcraft: Gaming stability improvements [5563205] FIXED Apex Legends: Occasional visual corruption after extended gameplay [6239327] FIXED Users may observe DLSS settings being grayed out in certain games after updating to display driver 610.47 [6262805] FIXED Improved gaming stability in multi-monitor configurations when using V-SYNC with DLSS Frame Generation [6158481] FIXED Resolved an issue that could cause jittering or ghosting in some DirectX 11 games when Smooth Motion is enabled [5937897] FIXED Resolved an issue that could cause some games to crash when launched with Smooth Motion enabled [5466398] FIXED [Ada] Resolves a frame pacing issue on certain monitors when G-SYNC is enabled [6226972] FIXED Resolved an issue that prevented the EDID from being read on certain monitors causing them to be identified as "NVIDIA NV-Failsafe” [6005508] FIXED Resolved an issue where certain monitors would not wake from sleep mode [5806798/5635230] FIXED General stability improvements when the system fails to create a new allocation [5449920] Nvidia has only listed a single open issue for this release: “Prefer Maximum Performance” Power Management Mode may not be applied correctly [6007998] The NVIDIA 610.62 driver is now available for download from the NVIDIA app. For those who want to download it directly, standalone links are here. Here are the official release notes (PDF).
    • You could do that in the last 2 updates as well.
  • Recent Achievements

    • One Year In
      Console General earned a badge
      One Year In
    • One Year In
      Twozo Technologies earned a badge
      One Year In
    • One Month Later
      Twozo Technologies earned a badge
      One Month Later
    • Week One Done
      Twozo Technologies earned a badge
      Week One Done
    • Veteran
      branfont went up a rank
      Veteran
  • Popular Contributors

    1. 1
      +primortal
      510
    2. 2
      +Edouard
      190
    3. 3
      PsYcHoKiLLa
      109
    4. 4
      Steven P.
      89
    5. 5
      Nick H.
      68
  • Tell a friend

    Love Neowin? Tell a friend!