What am I doing wrong here with this Switch statement?

I have to create a program that asks a user to guess a random number 1 to 5, gives a statement for each value, including if they guess the number correct, using a switch statement. I keep getting the same error on just my switch line which says "unqualified id" before switch. Can someone help?

# include <iostream>
using namespace std;

int main()
{

cout << "Enter a digit 1-5";
int number;
cin >> number;
int guess;

cout<<"Enter number to guess";

cin>>guess;}

switch (number)
{
case 1:
{
if (number == guess)
{
cout << "A correct guess!";
}
else
{
cout << "An incorrect guess";
}
break;
}
case 2:
{
if (number == guess)
{
cout << "A correct guess!";
}
else
{
cout << "An incorrect guess";
}
break;
}
case 3:
{
if (number == guess)
{
cout << "A correct guess!";
}
else
{
cout << "An incorrect guess";
}
break;
}
case 4:
{
if (number == guess)
{
cout << "A correct guess!";
}
else
{
cout << "An incorrect guess";
}
break;
}
case 5:
{
if (number == guess)
{
cout << "A correct guess!";
}
else
{
cout << "An incorrect guess";
}
break;
}
}
Here's your code reformatted:
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
int main(){
    cout << "Enter a digit 1-5";
    int number;
    cin >> number;
    
    int guess;
    cout <<"Enter number to guess";
    cin >>guess;
}

switch (number){
    case 1:
        {
            if (number == guess){
                cout << "A correct guess!";
            }else{
                cout << "An incorrect guess";
            }
            break;
        }
    case 2:
        {
            if (number == guess){
                cout << "A correct guess!";
            }else{
                cout << "An incorrect guess";
            }
            break;
        }
    case 3:
        {
            if (number == guess){
                cout << "A correct guess!";
            }else{
                cout << "An incorrect guess";
            }
            break;
        }
    case 4:
        {
            if (number == guess){
                cout << "A correct guess!";
            }else{
                cout << "An incorrect guess";
            }
            break;
        }
    case 5:
        {
            if (number == guess){
                cout << "A correct guess!";
            }else{
                cout << "An incorrect guess";
            }
            break;
        }
}

You left an extraneous closing brace after reading in guess, leaving the switch statement on global scope, outside of any function.
Besides that, the switch doesn't make any sense, since all cases do exactly the same thing. It can be replaced with just
1
2
3
4
5
if (number == guess){
    cout << "A correct guess!";
}else{
    cout << "An incorrect guess";
}
Last edited on
It's as if it has an if statement like:
1
2
3
4
5
6
7
8
9
10
11
12
13
if (1 <= number && number <= 5)
{
    if (number == guess){
        cout << "A correct guess!";
    }else{
        cout << "An incorrect guess";
    }
}
// Added:
else
{
    cout << "Invalid guess!\n";
}
Last edited on
Thanks alot. I see what I did wrong.
I took the brace out that fixed the switch statement but now I keep getting unexpected '}' at end of input for line 58

#include <iostream>
using namespace std;
int main(){
cout << "Enter a digit 1-5";
int number = 4;
cin >> number;

int guess;
cout <<"Enter number to guess";
cin >>guess;


switch (number){
case 1:
{
if (number == guess){
cout << "A correct guess!";
}else{
cout << "An incorrect guess";
}
break;
}
case 2:
{
if (number == guess){
cout << "A correct guess!";
}else{
cout << "An incorrect guess";
}
break;
}
case 3:
{
if (number == guess){
cout << "A correct guess!";
}else{
cout << "An incorrect guess";
}
break;
}
case 4:
{
if (number == guess){
cout << "A correct guess!";
}else{
cout << "An incorrect guess";
}
break;
}
case 5:
{
if (number == guess){
cout << "A correct guess!";
}else{
cout << "An incorrect guess";
}
break;}
}

PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

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

HINT: you can edit your post and add code tags.

With that said, here's your latest source formatted and using code tags:
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
#include <iostream>
using namespace std;
int main()
{
   cout << "Enter a digit 1-5";
   int number = 4;
   cin >> number;

   int guess;
   cout << "Enter number to guess";
   cin >> guess;

   switch (number)
   {
   case 1:
   {
      if (number == guess)
      {
         cout << "A correct guess!";
      }
      else
      {
         cout << "An incorrect guess";
      }
      break;
   }
   case 2:
   {
      if (number == guess)
      {
         cout << "A correct guess!";
      }
      else
      {
         cout << "An incorrect guess";
      }
      break;
   }
   case 3:
   {
      if (number == guess)
      {
         cout << "A correct guess!";
      }
      else
      {
         cout << "An incorrect guess";
      }
      break;
   }
   case 4:
   {
      if (number == guess)
      {
         cout << "A correct guess!";
      }
      else
      {
         cout << "An incorrect guess";
      }
      break;
   }
   case 5:
   {
      if (number == guess)
      {
         cout << "A correct guess!";
      }
      else
      {
         cout << "An incorrect guess";
      }
      break;
   }
   }

You are missing a closing bracket } for main, add one after line 75.

Your case statements's code doesn't need to be enclosed in brackets:
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
#include <iostream>

int main()
{
   std::cout << "Enter a digit 1-5: ";
   int number { };
   std::cin >> number;

   std::cout << "Enter number to guess: ";
   int guess;
   std::cin >> guess;

   switch (number)
   {
   case 1:
      if (number == guess) { std::cout << "A correct guess!\n"; }
      else                 { std::cout << "An incorrect guess\n"; }
      break;

   case 2:
      if (number == guess) { std::cout << "A correct guess!\n"; }
      else                 { std::cout << "An incorrect guess\n"; }
      break;

   case 3:
      if (number == guess) { std::cout << "A correct guess!\n"; }
      else                 { std::cout << "An incorrect guess\n"; }
      break;

   case 4:
      if (number == guess) { std::cout << "A correct guess!\n"; }
      else                 { std::cout << "An incorrect guess\n"; }
      break;

   case 5:
      if (number == guess) { std::cout << "A correct guess!\n"; }
      else                 { std::cout << "An incorrect guess\n"; }
      break;
   }
}

Since the code within each case has exactly the same logic you can let each case 'fall through' (no break statement) to the final case.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main()
{
   std::cout << "Enter a digit 1-5: ";
   int number { };
   std::cin >> number;

   std::cout << "Enter number to guess: ";
   int guess { };
   std::cin >> guess;

   switch (number)
   {
   case 1:  // let each case switch 'fall through'
   case 2:
   case 3:
   case 4:
   case 5:
      if (number == guess) { std::cout << "A correct guess!\n"; }
      else                 { std::cout << "An incorrect guess\n"; }
      break;  // not really needed since there are no other case statements to skip
   }
}

Since each case is using the same logic you can remove the switch:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
   std::cout << "Enter a digit 1-5: ";
   int number { };
   std::cin >> number;

   std::cout << "Enter number to guess: ";
   int guess { };
   std::cin >> guess;

   if (number == guess) { std::cout << "A correct guess!\n"; }
   else                 { std::cout << "An incorrect guess\n"; }
}

Having different output from each case can justify using switch:
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
#include <iostream>

int main()
{
   std::cout << "Enter a digit 1-5: ";
   int number { };
   std::cin >> number;

   std::cout << "Enter number to guess: ";
   int guess;
   std::cin >> guess;

   switch (number)
   {
   case 1:
      if (number == guess) { std::cout << "You correctly guessed 1!\n"; }
      else                 { std::cout << "An incorrect guess\n"; }
      break;

   case 2:
      if (number == guess) { std::cout << "You correctly guessed 2!\n"; }
      else                 { std::cout << "An incorrect guess\n"; }
      break;

   case 3:
      if (number == guess) { std::cout << "You correctly guessed 3!\n"; }
      else                 { std::cout << "An incorrect guess\n"; }
      break;

   case 4:
      if (number == guess) { std::cout << "You correctly guessed 4!\n"; }
      else                 { std::cout << "An incorrect guess\n"; }
      break;

   case 5:
      if (number == guess) { std::cout << "You correctly guessed 5!\n"; }
      else                 { std::cout << "An incorrect guess\n"; }
      break;
   }
}

OR you can modify the single if/else to reflect the number guessed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
   std::cout << "Enter a digit 1-5: ";
   int number { };
   std::cin >> number;

   std::cout << "Enter number to guess: ";
   int guess { };
   std::cin >> guess;

   if (number == guess) { std::cout << "You correctly guessed " << guess << "!\n"; }
   else                 { std::cout << "An incorrect guess\n"; }
}
Topic archived. No new replies allowed.