• 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

    • Also, Guru3D.com always has some pros and experts lurking in the comments.
    • If anyone is wondering, it's 30fps at 4K
    • This 4K webcam from Acer is now only $59.99 by Taras Buria Those looking for an affordable, high-resolution webcam from a reputable manufacturer can check out the latest deal from Acer, which puts its 4K webcam at a solid price. Thanks to a 14% discount, you can buy this all-metal 4K webcam from Acer for just $59.99. Despite the affordable price tag, the webcam has a pretty large 1/2" CMOS sensor manufactured by Sony. Apart from its size, it supports additional conveniences, such as high dynamic range and phase-detection autofocus (PDAF). Plus, the camera ensures you do not have to buy additional audio equipment, as it comes with two built-in high-sensitivity microphones, a built-in background noise filter, and the ability to pick up your voice from up to 2.5 meters away. Acer is not cheaping out on materials, and the webcam is enclosed in a more premium metal case, which is also good at heat dissipation (high-resolution cameras can get quite hot). And to make sure no one is peeping at you when the camera is in use, there is a magnetic cover also made of metal. Additional conveniences include an LED status indicator and a built-in mount that lets you place a camera on a tripod. Acer 4K Webcam for PC/Mac with All-Metal Unibody Sculpted - $59.99 | 14% off Good to know This Amazon deal is U.S.-specific and not available in other regions unless specified. We only use first-party seller links (at the time of article publishing); ensure that you purchase from a first-party seller link only. Check out Today's Deals on Amazon | or our recent tech deals. Become a Prime member (for Students or SNAP) via Neowin Get Prime Access - Prime for half price (for qualifying Medicaid, EBT, SNAP) Subscribe to Prime Video, Audible Plus, Music Unlimited, or Kindle Unlimited via Neowin As an Amazon Associate, we earn from qualifying purchases.
  • Recent Achievements

    • Conversation Starter
      NovaEdgeX earned a badge
      Conversation Starter
    • 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
  • Popular Contributors

    1. 1
      +primortal
      519
    2. 2
      +Edouard
      185
    3. 3
      PsYcHoKiLLa
      107
    4. 4
      Steven P.
      87
    5. 5
      ATLien_0
      67
  • Tell a friend

    Love Neowin? Tell a friend!