Switch Case - char to int

I only have found examples for switch case using char but I need int,
When I try to change to an int, it does not work. I know its going to be something super small!!

OBS: when I used char, I could type many digits as I want and it would count only the first. eg.: 123=monday why?? would this problem b solve changing to int??

So it would work as char but its not right!!!

This is my first code.
hope anyone can help
cheers

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
  #include <stdio.h>

int main()

{
	int response;
	
	//menu choices: monday:sunday = 1:7
	printf("You have seven choices. \n");
	printf("Enter 1 for Monday\n");
	printf("Enter 2 for Tuesday\n");
	printf("Enter 3 for Wednesday\n");
	printf("Enter 4 for Thursday\n");
	printf("Enter 5 for Friday\n");
	printf("Enter 6 for Saturday\n");
	printf("Enter 7 for Sunday\n");
	
	//Read in user's response
	scanf("%d%*c", &response);
	
	//Perform appropriate response
	switch (response)
	{
		case '1':
			printf("The day of the week is Monday\n");
			break;
		case '2':
			printf("The day of the week is Tuesday\n");
			break;
		case '3':
			printf("The day of the week is Wednesday\n");
			break;
		case '4':
			printf("The day of the week is Thursday\n");
			break;
		case '5':
			printf("The day of the week is Friday\n");
			break;
		case '6':
			printf("The day of the week is Saturday\n");
			break;
		case '7':
			printf("The day of the week is Sunday\n");
			break;
		default:
			printf("Invalid number\n");
	}
	
	return(0);
}
closed account (18hRX9L8)
Changing the cases to ints works fine for me...

New code:
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
  #include <stdio.h>

int main()

{
	int response;

	//menu choices: monday:sunday = 1:7
	printf("You have seven choices. \n");
	printf("Enter 1 for Monday\n");
	printf("Enter 2 for Tuesday\n");
	printf("Enter 3 for Wednesday\n");
	printf("Enter 4 for Thursday\n");
	printf("Enter 5 for Friday\n");
	printf("Enter 6 for Saturday\n");
	printf("Enter 7 for Sunday\n");

	//Read in user's response
	scanf("%d%*c", &response);

	//Perform appropriate response
	switch (response)
	{
		case 1:
			printf("The day of the week is Monday\n");
			break;
		case 2:
			printf("The day of the week is Tuesday\n");
			break;
		case 3:
			printf("The day of the week is Wednesday\n");
			break;
		case 4:
			printf("The day of the week is Thursday\n");
			break;
		case 5:
			printf("The day of the week is Friday\n");
			break;
		case 6:
			printf("The day of the week is Saturday\n");
			break;
		case 7:
			printf("The day of the week is Sunday\n");
			break;
		default:
			printf("Invalid number\n");
	}

	return(0);
}


EDIT: Putting 123 gives me "Invalid number", not "The day of the week is Monday"...
Last edited on
Have you considered arrays to store the day? then just output day[reponse-1] you could also then for your initial output loop through and output something like: Enter i+1 for day[i]
Hi usandfriend,

On my screen, it shows "Invalid number" w/ int and %d%*c.
but works w/ char and %c%*c, but i can type as many integers as i want
eg: 123=monday, 269==tuesday

thanks for your support
later
R
hi glibt,

it has to be switch case, since the tutor asked!!!

Cheers bro
R
Why not just scanf("%d", &response);? The reason you get invalid number when changing response to an int is you probably didn't remove the quotes in your switch statement like usandfriend did.
closed account (18hRX9L8)
Is it possible to take a picture of your console and put it on the forum? I would like to see what exactly is going on... Very interesting... Did you copy / paste my code straight from my post? It is working fine on my Mac OSX 10.9 with
1
2
3
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix
with g++ filename.cpp -lstdc++ -std=c++11;
hi gliblit
"%d%*c"==invalid number
"%d%"==invalid number

hi usandfriends,
its a good ideia
im doing right know

cheers guys
hi guys,

Ive copied the code from usandfriend and worked...
it was the case '1', on mine
and case 1 on his

sorry and thanks soo much guys,
legends
Topic archived. No new replies allowed.