我正在制作一个包含简单开关条件的程序。
我遇到的问题是我也在验证用户输入,这样用户就不能通过输入字符来破坏程序。
当我删除时开关工作正常,isdigit()
所以我知道这是数据验证发生的事情。
我被告知要做的是%c
在我的中使用,scanf()
但如果我这样做,那么其他东西会阻止程序工作。我怀疑这是因为不再引用开关,因为案例是 1,2,3 ...
我想这样做的方法是在字符到达开关之前将其转回整数,但我不确定我该怎么做。
当我复制和粘贴程序的一部分时发生了一些事情,所以我将粘贴整个程序。
#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