Reputation: 19
I am making a program that includes a simple switch condition.
The problem I have encountered is that I am also validating user input so that the user cannot break the program by entering a character.
The switch works fine when I remove the isdigit()
so I know it is something happening with data validation.
What I was told to do was use %c
in my scanf()
but if I do that then something else prevents the program from working. I suspect that it is because the switch is no longer is being referenced since the cases are 1,2,3...
The way I want to do it is just turn the character back into an integer before it reaches the switch but I am not sure how I could do that.
Something is happening when I copy and paste parts of the program so I will just paste the entire program.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int BusRoute, LTrigger;
char StartLoc,DestLoc;
LTrigger = 1;
BusRoute = 0;
StartLoc = 0;
DestLoc = 0;
while (LTrigger >= 1)
{
//Give user a menu and prompt to select input for BusRoute
printf("\n\n\tPlease only use the numbers provided.");
printf("\n\n 1.\n\tRoute_1\tCherokee Park and KFC YUM Center transit.");
printf("\n\n 2.\n\tRoute_2\tUL and Cherokee Park transit.");
printf("\n\n 3.\n\tRoute_3\tUL and KFC YUM Center transit.");
printf("\n\n\n\t Please select one route from the above menu.");
printf(" \n\n\tOnly use the single digit corresponding to the route: ");
scanf("%d" , &BusRoute);
//Refresh window
system("cls");
if(isdigit(BusRoute))
{
//Use switch to determin user's Route. Then present choice of To/From
switch (BusRoute)
{
case 1:
printf("\n\n\tYou have chosen Cherokee Park and KFC YUM Center transit.");
printf("\n\n\tIf you want to travel from Cherokee Park to KFC YUM Center enter C.");
printf("\n\n\tIf you want to travel from KFC YUM Center to Cherokee Park enter K.");
printf("\n\n\tEnter your seletion now: ");
scanf("%c" , &StartLoc);
break;
//give two if statements to determine users location and confirm destination
if (StartLoc == 'c' || StartLoc == 'C')
{
printf("\n\n\tYou have chosen to travel from Cherokee Park to KFC YUM Center.");
printf("\n\n\tTo confirm you want to travel from Cherokee Park to KFC YUM Center please enter K: ");
scanf("%c" , DestLoc);
//refresh
system("cls");
//confirmation of destination
if (DestLoc == 'k' || DestLoc == 'K')
{
printf("\n\n\tYour bus route will pick you up from Cherokee Park and take you to KFC YUM Center.");
getch();
}//end dest
}//end start
//false user input
else
{
printf("\n\n\tYou did not enter a correct character.\n\n\tPress enter and only enter specific values.");
getch();
//reset loop and refresh
LTrigger = 1;
system("cls");
}//end else
case 2:
printf("\n\n\tYou have chosen Cherokee Park and UL transit.");
break;
case 3:
printf("\n\n\tYou have chosen UL and KFC YUM Center transit.");
break;
}//end switch
}//end if
else
{
printf("\n\n\tYou did not enter a number.\n\n\tPress enter and only enter specific values.");
getch();
//reset loop and refresh
LTrigger = 1;
system("cls");
}//end else
}//end loop
getch();
}//end main
Upvotes: 0
Views: 1015
Reputation: 98476
isdigit
takes a character and says if it is a character between '0'
and '9'
. But you are passing it an integer, not a character, so its output is meaningless.
If you want to know if the scanf
succeeded, just check its return value: it will be 1 if it worked, or 0 if it failed (actually it will return the number of variables assigned):
if (scanf("%d", &BusRoute) > 0)
{
If you want to know if BusRoute
is one digit long, then you can simply check it to be between 0 and 9 (or 3), but there is no need to do that: instead, add a default:
clause to your switch
.
BTW, you have missed a &
in the line
scanf("%c" , DestLoc);
It should be:
scanf("%c" , &DestLoc);
Also, it is usually a good idea to add a space before the %c
it will eat out any space or carriage return left in the buffer (from the previous user operation, for instance):
scanf(" %c" , &DestLoc);
Ditto, for the StartLoc
case.
Upvotes: 3
Reputation: 4643
Look At this:
char charBusRoute;
int BusRoute;
/* ... */
scanf("%c",&charBusRoute);
if(isdigit(charBusRoute)){
BusRoute = charBusRoute - '0';
/* ... */
}
Upvotes: 0
Reputation: 111
It's tricky to answer this without seeing your code. You can turn a character into an integer with a typecast:
switch((int) variable)
If variable
is a character entered by the user, the typecast (int)
will convert it to its ASCII code for the switch
. Usually, though, C doesn't require typecasts to interpret characters as integers.
Upvotes: 1