Switch Statement Error

I keep getting the error: break not inside of loop or switch statement. Other errors include case "2" is not within a switch or a loop. What is wrong?!?

#include <iostream>

using namespace std;

int main(int nNumberofArgs, char* pszArgs[])

{
int nSexy;
cout << "On a scale of one to ten, how sexy are you? 10 = sexiest 1 = oh lord no.";
cout << endl;
cout << "Please type the number as a digit.";
cout << endl;
cin >> nSexy;

{
{switch (nSexy)

case '1':
cout << "You're so ugly, the computer purposely lowers the graphics when you use a webcam.";
break;

case '2':
cout << "I'd rather not look at you, but if I must, wretching in disgust might be a side effect.";
break;
case '3':
cout << "Please, don't take any pictures of yourself.";
break;
case '4':
cout << "eh.";
break;
case '5':
cout << "Not good, but alright.";
break;
case '6':
cout << "You'd be extremely hot if I were drunk.";
break;
case '7':
cout << "Baby, I could look at you all night.";
break;
case '8':
cout << "My word eleanor, who is that alluring person?";
break;
case '9':
cout << "You're a very beautiful person.";
break;
case '10':
cout << "Not even jesus is good enough for you.";
break;
default:
cout << "say what's that sound? I THINK ITS STUPIDITY! only stupid people don't put a correct number. although, it might be idiocy.";
break;
}
}
return 0;
}
READABILITY FIX


I keep getting the error: break not inside of loop or switch statement. Other errors include case "2" is not within a switch or a loop. What is wrong?!?

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
#include <iostream>

using namespace std;

int main(int nNumberofArgs, char* pszArgs[])

{
        int nSexy;
        cout << "On a scale of one to ten, how sexy are you? 10 = sexiest 1 = oh lord no.";
        cout << endl;
        cout << "Please type the number as a digit.";
        cout << endl;
        cin >> nSexy;

{
{
switch (nSexy)

        case '1':
                cout << "You're so ugly, the computer purposely lowers the"
                    << "graphics when you use a webcam.";
               break;

        case '2':
                cout << "I'd rather not look at you, but if I must,"
                      << "wretching in disgust might be a side effect.";
                break;
        case '3':
                cout << "Please, don't take any pictures of yourself.";
                break;
        case '4':
                cout << "eh.";
                break;
        case '5':
                cout << "Not good, but alright.";
                break;
        case '6':
                cout << "You'd be extremely hot if I were drunk.";
                break;
        case '7':
                cout << "Baby, I could look at you all night.";
                break;
        case '8':
                cout << "My word eleanor, who is that alluring person?";
                break;
        case '9':
                cout << "You're a very beautiful person.";
                break;
        case '10':
                cout << "Not even jesus is good enough for you.";
                break;
        default:
                cout << "say what's that sound? I THINK ITS STUPIDITY!"
                 << "only stupid people don't put a correct number." 
                 << "although, it might be idiocy.";
        break;
}
}
return 0;
}
Last edited on
With that in mind, read this article.
http://www.cplusplus.com/doc/tutorial/control/
Wow, for the readability fix, i have the indentations, its just that when i copied and pasted it, the formatting disappeared. Thanks for the article though, all I was missing was one "{".
iceadmiral wrote:
.... all I was missing was one "{".


You have a couple more problems than that:

You have too many braces. Delete lines 15,16,58. Add opening brace on line 17, so that the switch is now enclosed in braces.

Too many braces is a problem because it means scope, consider this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{ // outer scope
{
int a =1;
std::cout << "First a is " << a << "\n";
} // end of first a's scope

{  // braces mean a different scope
         int a = 2; // different to the other a
         std::cout << "Second a is " << a << "\n";
} // end of second a's scope
int a =3; // this a is in the outer scope
{  // braces mean a different scope
         int a = 4; // error a = 3 still in scope
         std::cout << "Third  a is " << a << "\n";  // a is 3
}

} //end of outer scope 


So it is generally a bad idea to introduce gratuitous scope.

The nSexy variable is of type int, but you have the cases specified as char, so change it like this (remove the single quotes):

19
20
21
22
23
24
        case 1: // int not char
                cout << "You're so ugly, the computer purposely lowers the"
                    << "graphics when you use a webcam.";
               break;

        case 2:  // int not char 


Especially here:

49
50
51
        case '10':  // char can only hold 1 char
                cout << "Not even jesus is good enough for you.";
                break;


Wow, for the readability fix, i have the indentations, its just that when i copied and pasted it, the formatting disappeared.


http://www.cplusplus.com/articles/z13hAqkS/


Hope all is well :+)
Last edited on
Thanks, I'm a beginner so when I don't know what's wrong, I tend to just add things here and there and so I have a lot of extra stuff. I should have just posted on here right away...
Topic archived. No new replies allowed.