### Switch Statement Question

I am trying to use this switch statement and I am not sure if I can use it this way?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596`` ``````int NumWords = wholeList.getNumWords(); int num; char choice; cout << "FILE NAME: " << fileN << endl; cout << "CHAR NUM: " << charNum << endl; cout << "LINE NUM: " << lineNum << endl; cout << "Menu Choices:" << endl; cout << "A)ll Words with Multiplicity" << endl; cout << "P)rint Words That Appeared a Specified Number of Times" << endl; cout << "S)how first N Characters of All Words" << endl; cout << "F)ind a Word" << endl; cout << "Q)uit" << endl; do { cout << "Please enter a number. Enter negative number to exit. >> " << endl; cin >> choice; switch (choice) { case "A": cout << setw(20) << "WORD" << setw(20) << "COUNT" << endl; //for (int i = 0; i < NumWords; i++) { //output all the words and their multiplicity cout << wholeList << endl; case "a": cout << setw(20) << "WORD" << setw(20) << "COUNT" << endl; //for (int i = 0; i < NumWords; i++) { //output all the words and their multiplicity cout << wholeList << endl; break; case "P": //output particular words of specified multiplicity cout << setw(20) << "WORD" << setw(20) << "COUNT" << endl; for (int i = 0; i < NumWords; i++) { if (wholeList.getWordMult(i) == num) { cout << wholeList[i]; }//if delimiter }//for loop delimiter case "p": //output particular words of specified multiplicity cout << setw(20) << "WORD" << setw(20) << "COUNT" << endl; for (int i = 0; i < NumWords; i++) { if (wholeList.getWordMult(i) == num) { cout << wholeList[i]; }//if delimiter }//for loop delimiter break; case "F": cout << "Please enter the word you wish to find:" << endl; string FindWord; cin >> FindWord; for (int i = 0; i < NumWords; i++) { if (FindWord == wholeList[i].getWord()) cout << wholeList[i].getWord(); } case "f": cout << "Please enter the word you wish to find:" << endl; string FindWord; cin >> FindWord; for (int i = 0; i < NumWords; i++) { if (FindWord == wholeList[i].getWord()) cout << wholeList[i].getWord(); } case "Q": cout << "You have entered a "Q" - program will terminate." << endl; exit(0); case "q": cout << "You have entered a "Q" - program will terminate." << endl; exit(0); default: cout << "You have entered an invalid entry." << endl; }//case stment delimiter } while (num >= 0); ``````

When I compile it I get these errors:
 p3.cpp:194: error: case label does not reduce to an integer constant p3.cpp:200: error: case label does not reduce to an integer constant p3.cpp:208: error: case label does not reduce to an integer constant p3.cpp:217: error: case label does not reduce to an integer constant p3.cpp:229: error: case label does not reduce to an integer constant p3.cpp:239: error: case label does not reduce to an integer constant

Double quotes "a" is a string.
You need the character 'a' instead (single quotes).
Oh - thanks!
And you can simplify the code:

 ``1234567`` `````` case 'A': // Do something break; case 'a': // Do something break;``````

You can combine the upper and lower case choices:
 ``1234`` `````` case 'A': case 'a': // Do something break;``````
Last edited on
Like so?

 ``12345678910`` `````` case 'A': case 'a': choice == 'A' && choice =='a'; cout << setw(20) << "WORD" << setw(20) << "COUNT" << endl; //for (int i = 0; i < NumWords; i++) { //output all the words and their multiplicity cout << wholeList << endl; break;``````
Last edited on
You don't need line 4. Delete this: `choice == 'A' && choice =='a';`

The two case statements on lines 1 and 2 do that job.
Ok - thanks again!
Topic archived. No new replies allowed.