0
#include <iostream>

using namespace std;

int main()
{

    enter code here

   string grade ='Ahmed';
   switch(grade)
   {
       case 'A' :
       cout << "excellent!" <<endl;
       break;

       case 'B':
       cout << "very good!" <<endl;
       break;

       case 'C':
       cout << "well done!" <<endl;
       break;

       case 'D':
       cout << "you passed" <<endl;
       break;

       case 'F':
       cout << "better to try again!" <<endl;
       break;

       default :
        cout <<"invalid grade"<< endl;
   }
   cout <<"your grade is :"<< grade << endl;

   return 0;
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • 2
    That's not a string literal. You can't switch on strings. You can switch on `char` which seems to be what you are trying to do anyway... – ChrisMM Nov 28 '19 at 11:07
  • 1
    Does this answer your question? [Why the switch statement cannot be applied on strings?](https://stackoverflow.com/questions/650162/why-the-switch-statement-cannot-be-applied-on-strings) – ChrisMM Nov 28 '19 at 11:07
  • 2
    To be honest it looks like you skipped class the day they talked about strings and string literals. Or skipped those chapters in your book. Please go back to study it some more. – Some programmer dude Nov 28 '19 at 11:09

2 Answers2

2

You can't switch on a std::string in C++: case labels need to be compile time evaluable integral expressions.

'Ahmed' is actually a multi-character constant (has an int type but an implementation-defined value): you need to use double quotation characters to declare a suitable type for std::string construction.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

'Ahmed' is not a string literal. String literals need " not single quotes.

How to switch on strings?

You cannot. For switch the condition must be

any expression of integral or enumeration type, or of a class type contextually implicitly convertible to an integral or enumeration type, or a declaration of a single non-array variable of such type with a brace-or-equals initializer.

...and none of this applies for std::string.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185