switch case question

Hi, I am fairly new with c++ and I would really appreciate any help right now.
So my question goes like this, can switch case function ask/run the statement again if the user inputs a wrong choice. Or if it does not, what options do i have? Thanks in advance

Sample Code below. It's not yet complete btw.
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
#include<iostream>
using namespace std;

int main()
{
	char c1,d1,d11;
	float distance;
	
	cout<<"Choose the corresponding number of what you want to MEASURE:"<<endl;
	cout<<"[1] Distance"<<endl;
	cout<<"[2] Volume"<<endl;
	cout<<"[3] Temperature"<<endl;
	cout<<"Enter choice: ";
	cin>>c1;
	cout<<endl;
	
	switch (c1)
	{
		case '1':
			cout<<"Enter length (m)"<<endl;
			cin>>distance;
			cout<<"Display Distance in: "<<endl;
			cout<<"[1] Kilometer"<<endl;
			cout<<"[2] Yard"<<endl;
			cout<<"[3] Centimeter"<<endl;
			cout<<"Enter choice: ";
			cin>>d11;
			cout<<endl;
			
			switch (d11)
			{
				case '1':
					cout<< distance <<" Meter is equals to "<< (distance / 1000)<<" Kilometer/s";
					break;
					
				case '2':
					cout<< distance <<" Meter is equals to "<< (distance * 1.09361)<<" Yard/s";
					break;
				
				case '3':
					cout<< distance <<" Meter is equals to "<< (distance * 100)<<" Centimeter/s";
					break;
					
				default:
					cout<<"Please enter the corresponding of your choice:";
					break;
			}
			
			
	}
	
}
Last edited on
can switch case function ask/run the statement again if the user inputs a wrong choice. Or if it does not, what options do i have?

Switch cases can not do that, no. What you want is a loop.

http://www.tutorialspoint.com/cplusplus/cpp_loop_types.htm
http://www.cprogramming.com/tutorial/c/lesson3.html

What you want specifically is a do-while loop.

If you prefer learning by watching videos, here is a big playlist with all kinds of basic information, including do-while loops - https://www.youtube.com/playlist?list=PLAE85DE8440AA6B83
Also, you could try a bool controlled while loop, with a quit case: Google this :+)

Each case should call a function, and the menu should be functions as well, this will tidy your code quite a bit.

Good Luck !!
I suggest a function to prompt the user for their choice, then the switch statement.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char getChoice()
{
    char result;
    while (true) {
        cout<<"Choose the corresponding number of what you want to MEASURE:" << endl
        cout<<"[1] Distance"<<endl;
        cout<<"[2] Volume"<<endl;
        cout<<"[3] Temperature"<<endl;
        cout<<"Enter choice: ";
        if (cin>>result &&
            result >= '1' &&
            result <= '3') {
            cout<<endl;
            return result;
        }
    }
}

You could even make the printed text a parameter. That way you could use the same function for other menus like lines 22-28
Alright, thanks for the tips guys. Appreciate it.
Topic archived. No new replies allowed.