Problem with switch case

Whenever I try to run this code it works everytime. Thing is, it should only work 1/10 times as it's a random 1 to 10 integer. I don't think it's taking the input of 1 as it outputs the text every time. I'm running ChromeOS but that doesn't matter as I'm using an online compiler. (repl.it)

Output:

What is the element symbol for tin?
A - Pb
B - Sn
C - Ti
D - Au
B


You are correct! +1 PointWhat is the element symbol for tin?
A - Pb
B - Sn
C - Ti
D - Au








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

int main(){
  using namespace std;
  srand(time(NULL));
  
  while(1 != 0)
  {
    int ques;
    ques = rand() % 10 + 1;
    
    int points;
    points = 1;
    
    switch (ques){
      case '1': char answer1;
              answer1 = 'A';
              cout << "What is the element symbol for tin?" << endl;
              cout << "A - Pb\n" << "B - Sn\n" << "C - Ti\n" << "D - Au\n";
              cin >> answer1;
              if(answer1 == 'B'){
                cout << "\n\nYou are correct! +1 Point";
                points = points + 1;
              }
              else 
              {
                cout << "You are incorrect! -1 Point";
              }
              break;
    }
  }
  return 0;
}
Whenever I try to run this code it works everytime.


I wouldn't bank on that. What do you think
case '1'
(with single quotes) does when ques is actually an integer?

You are missing headers (so it doesn't compile).

It has only one case (actually it has no legitimate cases at present, but you can soon fix that) so in the 10% of times when ques gives that value it goes to it, otherwise it justs loops round silently until the next time it gets to an appropriate case.
Your while loop will loop forever. The nine times out of ten that it doesn't trigger the case '1' section just mean it goes round again and has another random number.
Oh... I just realized that. Thanks for the information.
Topic archived. No new replies allowed.