単純なスイッチ条件を含むプログラムを作成しています。
私が遭遇した問題は、ユーザーが文字を入力してプログラムを中断できないように、ユーザー入力も検証していることです。
削除するとスイッチは正常に機能する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