Switch Statement Question

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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:

Instead of this:
1
2
3
4
5
6
7
    case 'A':
        // Do something
        break;
   
    case 'a':
        // Do something
        break;


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

1
2
3
4
5
6
7
8
9
10
    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.