You are here:

C++/What Am I missing?? Help please


I don't seem to be able to make the program to accept lower case letters when ask the question Please help!!

#include <iostream>
using namespace std;

class toLower {public: char operator()(char c) const {return tolower(c);}};

char getGrade()
 char grade = 'Q'; // the default value
 while (true)
   cout << "What is your grade? [A, B, C, D, F, or Q to quit]: ";
   cin >> grade;
   cin.ignore(1000, 10);

    if (grade == 'A' || grade == 'B' || grade == 'C' || grade == 'a' || grade == 'b' || grade == 'c') break;
    if (grade == 'D' || grade == 'F') break;
    if (grade == 'Q' || grade == 'q') break;

   cout << grade << " is not a valid grade. Try again..." << endl;
 } // while

 return grade;
} // getGrade

int main()
 char grade;
 while (true)
   grade = getGrade();
   if (grade == 'Q' || grade == 'q') break;
   if (grade == 'A' || grade == 'B' || grade == 'C')
     cout << "You pass" << endl;
 } // while

 cout << "Thanks for checking your grades!" << endl;
 return 0;
} // main

getGrade returns the grade that was accepted, but doesn't change it.  By the way, it needs to check for 'd' and 'f' too.

main checks for 'Q' 'q' 'A' 'B' and 'C' which will never match a lower case value.  Before this line:

if (grade == 'A' || grade == 'B' || grade == 'C' || grade == 'a' || grade == 'b' || grade == 'c') break;

why not change grade to upper or lower case (I'd use upper).  

grade = toupper( grade );

Now you only need to check 'A', 'B', 'C', 'D', 'F' and 'Q' in getGrade and main is OK and doesn't need the 'q' test.

For more efficient code, I'd use:

if( strchr( "ABCDFQ", grade ) != NULL )



All Answers

Answers by Expert:

Ask Experts


Bill A


I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.


I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

BS Computer Engineering

©2016 All rights reserved.