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
#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);
}
#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"...
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]
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
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.
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