-
switch/case problem
Code:
#include<stdio.h>
main()
{
int day;
clrscr ();
printf("enter the integer;");
scanf("%d",&day);
switch (day)
{
case 1:
printf("monday");
printf("\n enter an integer:");
scanf("%d",&day);
break;
case 2:
printf("tuesday");
printf("\n enter an integer;");
scanf("%d",&day);
break;
default:
printf("\n day is not available");
printf("/n enter an integer:");
}
getche ();
}
when i input a integer in the 1st, the answer will show but when i input an integer again the answer didnt show
-
The first time you enter an integer, it's through the scanf. The second, it's through the call to getch() at the end - to end the program. If you want to backup and go through the switch/case block again, you need to put it inside a loop, that loops until some "exit condition" is reached.
Also - indent your code properly - it'll make it a lot easier for other people to read and debug.
-
Quote:
Originally Posted by
maxilog1009
Too late, the Bolivians beat you to him a long time ago :p
-
The answer is to set up a while loop and use a variable (like a bool). If the user enters something that is invalid, the variable changes values to meet the while loops conditions (say true?) and exit the program.