varibles with switch.

I am trying to create a basic switch statement that you enter a number ant it tells you the number. but if you enter something other than a integer to does a loop of the code on default.(run it to see for yourself) I tried changing it to string but it says

line 16
error: switch quantity is not an integer

Here it the code
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <stdio.h>
#include <windows.h>
using namespace std;

void menu()
{
string imput;
system("CLS");
cout << "enter the number 1,2,3 or 4." << endl;
cin >> imput;

switch(imput){
case 1:
cout << "One!" << endl;
Sleep(2500);
menu();
break;
case 2:
cout << "Two!" << endl;
Sleep(2500);
menu();
break;
case 3:
cout << "Three!" << endl;
Sleep(2500);
menu();
break;
case 4:
cout << "Four!" << endl;
Sleep(2500);
menu();
break;
default:
system("CLS");
cout << "some other number!" << endl;
system("Pause");
menu();
}
}

int main()
{
menu();
}
Last edited on
so what do to fix my problem?
If you want to allow the user to enter as many numbers as they wish, I might consider using a while loop instead of creating additional instances of the same function.
for the other program I am making it will not work to have a while loop. the code I provided is a example...
It's kind of meaningless to say "it will not work" without some sort of context. It may simply be that there was an error in the design.

closed account (z05DSL3A)
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
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <stdio.h>
#include <windows.h>

using namespace std;

int Convert_to_int(std::string str)
{
	try
	{
		return std::stoi(str);
	}
	catch (std::exception e)
	{
		return -1;
	}
}

void menu()
{
	string imput;
	while (true)
	{ 
		system("CLS");
		cout << "enter the number 1,2,3 or 4." << endl;
		cin >> imput;

		switch (Convert_to_int(imput))
		{

		case 1:
			cout << "One!" << endl;
			Sleep(2500);
			break;
		case 2:
			cout << "Two!" << endl;
			Sleep(2500);
			break;
		case 3:
			cout << "Three!" << endl;
			Sleep(2500);
			break;
		case 4:
			cout << "Four!" << endl;
			Sleep(2500);
			break;
		case -1:
			cout << "A number would be nice!" << endl;
			system("Pause");
			break;
		default:
			system("CLS");
			cout << "some other number!" << endl;
			system("Pause");
		}
	}
}

int main()
{
	menu();
}


Tyler151 wrote:
for the other program I am making it will not work to have a while loop. the code I provided is a example...
Just make sure that you don't recursively call your menu function in your real code, your stack will not like you.
Last edited on
their is an error with this.

error: 'stoi' is nor a member of 'std'

return std::stoi(str);
You should look up the ascii table and understand the difference between numbers and characters. For example the number 1 is different from the character "1". You can fix this problem by taking in an Integer rather than a string.

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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <stdio.h>
#include <windows.h>
using namespace std;

void menu()
{
int imput;
system("CLS");
cout << "enter the number 1,2,3 or 4." << endl;
cin >> imput;

switch(imput){
case 1:
cout << "One!" << endl;
Sleep(2500);
menu();
break;
case 2:
cout << "Two!" << endl;
Sleep(2500);
menu();
break;
case 3:
cout << "Three!" << endl;
Sleep(2500);
menu();
break;
case 4:
cout << "Four!" << endl;
Sleep(2500);
menu();
break;
default:
system("CLS");
cout << "some other number!" << endl;
system("Pause");
menu();
}
}

int main()
{
menu();
}
that works, but try entering a letter. that is the problem I am facing.
Last edited on
Can you explain in detail what you are exactly trying to write?
In this specific case, you could change int imput; to char imput;
and then change case 1: to case '1': and so on. A more general-purpose solution is possible, but needs a little more code (though not a lot more).
I want to have the user enter a number(1,2,3,4) and it will run some code. if the number they entered is other than 1,2,3,4 it says something like "sorry the number you entered it invalid". But if the user enters a letter(s) the code that says "sorry the number you entered it invalid" will rerun infinitely...try it for yourself.
Last edited on
Thank you Chervil that did work.
there is still one problem, if they enter more than one letter it runs the default code depending on how many letters were entered...how do I fix this
Last edited on
You could clear any remaining characters from the input buffer after getting cin >> imput, by using cin.ignore(1000, '\n'); which will ignore up to 1000 characters, or until the newline character is found.

As an alternative viewpoint, you might look at this:
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
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>

using namespace std;

void menu()
{
    int imput;
    string numbers[4] = {"One!", "Two!", "Three!", "Four!" };
    
    do {
        while (true)
        {
            cout << "enter the number 1,2,3 or 4, or 0 to exit" << endl;
            if (cin >> imput)
                break;
            else
            {
                cin.clear();
                cin.ignore(1000, '\n');
            }
        }

        switch(imput)
        {
            case 1:
            case 2:
            case 3:
            case 4:
                cout << numbers[imput-1] << endl;
                Sleep(2500);
                break;
                
            case 0:
                break;

            default:
                cout << "some other number!" << endl;
        }
    } while (imput != 0);
    
}

int main()
{
    menu();
}

I omitted the system() calls in this example.
Last edited on
closed account (z05DSL3A)
Tyler151 wrote:
their is an error with this.

error: 'stoi' is nor a member of 'std'

return std::stoi(str);

You are using an older compiler...in that case replace Convert_to_int with...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <sstream>

int Convert_to_int(std::string str)
{
	int n;
	char c;
	std::istringstream input(str);
	input >> n;
	if (input.fail() || input.get(c)) {
		// not an integer
		return -1;
	}
	return n;
}


Last edited on
nice! that works!! thank you for all the help
Topic archived. No new replies allowed.