Error - crosses initialization?

I am not understanding these errors:

p3.cpp:228: error: jump to case label
p3.cpp:218: error: crosses initialization of `std::string FindWord'
p3.cpp:229: error: jump to case label
p3.cpp:218: error: crosses initialization of `std::string FindWord'
p3.cpp:231: error: expected `;' before "Q"
p3.cpp:236: error: jump to case label
p3.cpp:218: error: crosses initialization of `std::string FindWord'
p3.cpp:228: warning: destructor needed for `FindWord'
p3.cpp:228: warning: where case label appears here
p3.cpp:228: warning: (enclose actions of previous case statements requiring destructors in their own scope.)
p3.cpp:229: warning: destructor needed for `FindWord'
p3.cpp:229: warning: where case label appears here
p3.cpp:236: warning: destructor needed for `FindWord'
p3.cpp:236: warning: where case label appears here



Here is the code:
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
  cout << "FILE NAME: " << fileN << endl;
  cout << "CHAR NUM: " << charNum << endl;
  cout << "LINE NUM: " << lineNum << endl;

  do {
      	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;
    	cin >> choice;
    
    switch (choice)  {
      
    case 'A':
    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':
    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':
    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();
	}

	break;
	
    case 'Q':
    case 'q':

	cout << "You have entered a "Q" - program will terminate." << endl;
        exit(0);

	break;

    default:
	cout << "You have entered an invalid entry." << endl;
      
    }//case stment delimiter

  } while (num >= 0);


Do I need to overload the "==" operator? Is that why it is telling me those things? Just guessing....
In C/C++, switch statements allows fall-through semantics with the case labels. You declared a FindWord variable when your switch statement encounters case 'F' or 'f'. To solve this, make FindWord in its own scope or declare it outside the switch statements.

1
2
3
4
5
6
7
8
string FindWord;
switch (choice) {
    // code here...
    case 'F':
    case 'f':
        // use FindWord here...
        break;
}


or
1
2
3
4
5
6
7
8
switch (choice) {
    case 'F':
    case 'f': {
            string FindWord;
            // use FindWord here...
        }
        break;
}
Thank you :)
Topic archived. No new replies allowed.