0

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
0

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I see thank you. As I have not been able to analyze that part of the code in a while I haven't gotten around to analying syntax errors haha derp. Thank you for your assistance.
After some practice I have discovered C does not allow default: arguments. I am stuck once again trying to insure a user cannot break the program by enter a character.
Of course, isdigit() will work fine as long as the user types a number between 48 and 57 (for 0 to 9)...but it is a bit unkind to make the user type raw ASCII codes in decimal.
@Ev00: Of course C allows default: in a switch statement. Not case default: Just default:
1

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.

Comments

0

Look At this:

char charBusRoute;
int BusRoute;

/* ... */

scanf("%c",&charBusRoute);

if(isdigit(charBusRoute)){
    BusRoute = charBusRoute - '0';
    /* ... */
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.