How to make prog read 2digit numbers?

Nov 26, 2010 at 10:23pm
Hello :)

I want to make a program which asks a number and then executes the tasks linked to that number, but I'm having a hard time figuring out how to read a 2digit number.

Let us assume the following prog (simplified version of what I'm doing atm)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main()
{
start:	
	char x;
	cout<< "type a number\n";
	cin>>x;
	switch (x){
		case '1':
			cout << "1\n";
		break;
		case '10':
			cout << "10\n";
		break;
			}

goto start;
return 0;
}


Please tell me how I can make this program execute the 10th case
(atm it executes case 1 if I type 10)

tx on advance :)
Last edited on Nov 27, 2010 at 9:30am
Nov 27, 2010 at 12:10am
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
int main()
{
start:	
	int  x;
	cout << "type a number:\n";
	cin >> x;
	switch (x){
		case 1:
			cout << "1\n";break;
	                case 2:
			cout << "2\n";break;
		case 3:
			cout << "3\n";break;
		case 4:
			cout << "4\n";break;
		case 5:
			cout << "5\n";break;
		case 6:
			cout << "6\n";break;
		case 7:
			cout << "7\n";break;
		case 8:
			cout << "8\n";break;
		case 9:
			cout << "9\n";break;
		case 10:
			cout << "10\n";
			}

goto start;
return 0;

}

Last edited on Nov 27, 2010 at 12:10am
Nov 27, 2010 at 1:24am
You don't need the single quotes around the case number.
In general, numbers need no quotes, single letters need single quotes and strings need double straight quotes.
Nov 27, 2010 at 8:26am
kinda prefere to keep it char, 'cause I got another command which includes a letter to do another task ,so int won't work here. Besides if one would input a letter in your code , the prog goes haywire :p

You don't need the single quotes around the case number.
In general, numbers need no quotes, single letters need single quotes and strings need double straight quotes.


Using char , so if I'd leave the quotes away it would look for the ASCII value , which isn't the same as the int value :)
Last edited on Nov 27, 2010 at 8:59am
Nov 27, 2010 at 7:53pm
Then you need to use a string, and then convert to int. There is no way to hold "10" inside a char. You could do it in a char* or char array, but it would be much simpler to store it into an int. And then write another function to handle the char.
Nov 28, 2010 at 11:30am
Hmm , I'll supppose I'll do it that way then :)

Thanks ^^
Topic archived. No new replies allowed.