If-else To Switch Statement

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
else if (menu =='B' || menu == 'b')
      {
           printf("\n\nGuessing Game");
           printf("Choose from 1 to 10");
           printf("\nEnter Here:");
           scanf("%d",&guess);
           srand(time(NULL));
           random=rand()%10+1;
           
           if (guess==random)
           {
              printf("Random Number:%d",random);
              printf("\nYour Guess is Correct"); 
           }
           else
           {
                  try=try-1;
                  printf("Your Wrong Try Again");
                  printf("\n\nYou have %d try left",try);
             
                  printf("\nEnter Your Guess Again:");
                  scanf("%d",&guess);
                  
                  if (guess==random)
                  {
                     printf("Random Number:%d",random);
                     printf("\nYour Guess is Correct");
                  }
                  else
                  {
                      try=try-1;
                      printf("Your wrong");
                      printf("\n\nYou have %d try left",try);
                      printf("\nEnter Your Number Again:");
                      scanf("%d",&guess);
                      
                      if (guess==random)
                      {
                         printf("Random Number:%d",random);
                         printf("\nAt Last You win!!");
                      }
                      else
                      {
                          printf("Random Number:%d",random);
                          printf("\nBETTER LUCK NEXT TIME LOSER!");
                      }
                  }
           }
      }


This my guessing game program
when I try to change it to switch
"case label does not reduce to an integer constant"
in this part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 printf("\n\nGuessing Game");
                      printf("Choose from 1 to 10");
                      printf("\nEnter Here:");
                      scanf("%d",&guess);
                      srand(time(NULL));
                      random=rand()%10+1;
                      
                      switch (guess)
                      {
                             case random:
                                         printf("Random Number:%d",random);
                                         printf("\nYour Guess is Correct");
                                         break;
                                  
                             default:
                                     try=try-1;
                                     printf("Your Wrong Try Again");
                                     printf("\n\nYou have %d try left",try);
             
                                     printf("\nEnter Your Guess Again:");
                                     scanf("%d",&guess);
                                     break;
                        }


my problem is on how to put the equal variable in case:
I don't know what "the equal variable" is, but you are already checking of guess == random. If guess == random, then you enter case random:.


I see you have a variable called try. Have you noticed the colour of try? This means it's a reserved word. Just like if switch return default case while for do goto try throw class struct int float double char unsigned static extern etc. You need to use another word because try has a specific meaning when compiled.
Last edited on
1
2
3
4
 switch (guess)
                      {
                             case random:
                      }


in this part it say "case label does not reduce to an integer constant" how do i put a assign variable with the value of rand
ah I see, the problem now.

1. Remove the line that defines random (which is not in the code above).
2. Replace line 6 with const int random = rand()%10+1;.

This makes random read-only (after definition). This is required for a case statement.

1
2
3
4
5
6
7
  const int gnum = rand()%10+1;
                      
                      switch (guess)
                      {
                             case gnum:
                                               printf("--");
                     }


i do like this but nothing happen

Did it compile? If if compiled and nothing happened when you ran the program, then guess != gnum.
Compiler:
case label does not reduce to an integer constant

closed account (o1vk4iN6)
The compiler needs to know the number of a switch statement before it's compiled, otherwise you could have 2 cases that are the same.

This is not the same as a constant integral since it won't be generated until runtime since you are calling "rand()";

const int gnum = rand()%10+1;

Why not use a loop?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

srand(time(0));

int value = rand();
int tries = 10;

do {
     int guess;

     cout << "Enter value: "
     cin >> guess;     

} while( guess != value && --tries );

if(tries > 0) cout << "You're right!\n";
else cout << "too bad.\n";


You could even use a for loop, which would make a bit more sense with the tries.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int value = rand();

for(int i = 10; i > 0; i--)
{
     int guess;

     cout << "tries: " << i << endl;
     cout << "enter value: ";
     cin >> guess;

     if( guess == value ){
          cout << "party!\n";
          break;
     }
     else {
          cout << " try again\n";
     }
}
Last edited on
Topic archived. No new replies allowed.