Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8569199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:16:45+00:00 2026-06-11T18:16:45+00:00

I am making a program that includes a simple switch condition. The problem I

  • 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:16:46+00:00Added an answer on June 11, 2026 at 6:16 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with a simple program im making with fork and pipes
I am thinking about making a program that will need to send input and
I'm making a simple program to interact with a database. I have taken a
I'm making a simple program that maintains a list of numbers, and I want
I'm making a small program for Rails that includes some of my methods I've
So I'm having a problem making a new socket. I had this program that
I am making a program that automates the seperation of a csv file. We
I am making a program that graphs a line based on data inputted by
I am currently making a program that will send an email if the date
I noticed while making a program that a lot of my int type variables

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.