[try Beta version]
Not logged in

 
 
Switch statement won't break and instead loops forever

Jan 31, 2019 at 1:21am
For some reason when I run my program, the switch statement wont break and instead loops infinetly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  switch (choice){
    	case 'a': ans=x+y;
              	cout<<"What is the sum of the two numbers you put in?";
              	cin>>z;
              	break;
    	case 'b': ans=x-y;
              	cout<<"What is the difference of the two numbers you put in?";
              	cin>>z;
              	break;
    	case 'c': ans=x*y;
              	cout<<"What is the product of the two numbers you put in?";
              	cin>>z;
              	break;
    	case 'd': ans=x/y;
              	cout<<"What is the quotient of the two numbers you put in?";
              	cin>>z;
              	break;
    	case 'e': ans=x%y;
              	cout<<"What is the remainder of the two numbers you put in?";
              	cin>>z;
              	break;
  	}
Jan 31, 2019 at 2:14am
Note that switch is not a looping/iterative construct. It does a single pass through the cases. Don't be confused by the fact that you can use break to exit the switch.

Your switch looks fine. Can you show the rest of the code? And describe a little about what makes you think the switch is looping?
Jan 31, 2019 at 2:16am
Is your switch statement wrapped within some looping structure? Because it seems fine to me.
Feb 2, 2019 at 4:17pm
@Grime and @Uk Marine thanks for the help I noticed I had put the switch statement in a while loop with no way to get out
Topic archived. No new replies allowed.