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 8376335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:24:35+00:00 2026-06-09T15:24:35+00:00

I face problem in my code below. When I enter any integer, that is

  • 0

I face problem in my code below. When I enter any integer, that is (0-9), then it is OK. But when I enter some other characters like A, B or any other letter scanf() fails. Then it doesn’t wait for any further input.

I’ve attached a code snippet for this. I highly require error handling using only the C standard library.

#include <stdio.h>

int main()
{
    int choice;
    while(1)
    {
        printf("Enter your choice: ");
        if(0 == scanf("%d", &choice))
          {
              printf("Some error occured\n");
              //break;/*I did not want to break this loop as I use this to show menu*/
          }

        switch(choice)
         {
                      case 0:printf("Case 0\n");break;
                      case 1:printf("Case 1\n");break;
                      case 2:printf("Case 2\n");break;
                      case 3:printf("Case 3\n");break;
                      case 4:printf("Case 4\n");break;
                      case 5:printf("Case 5\n");break;
                      case 6:printf("Case 6\n");break;
                      case 7:printf("Case 7\n");break;
                      case 8:printf("Case 8\n");break;
                      case 9:printf("Case 9\n");break;
                      default:printf("Default case\n");break;

         }
    }
}

Question more clearly is: why doesn’t it wait after failure?

  • 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-09T15:24:37+00:00Added an answer on June 9, 2026 at 3:24 pm

    With scanf, if the input given doesn’t match the format specification, then the input isn’t consumed and remains in the input buffer. In other words, the character that doesn’t match is never read.

    So when you type, e.g. an a character, your code will loop indefinitely as scanf continues to fail on the same character.

    When scanf returns an error, you need to get rid of the offending character(s).

    You can’t know how many characters will be typed, but one simple way to consume any offending characters is to try and read a string when scanf fails:

    char junkChars[256];
    
    printf("Enter your choice: ");
    if (scanf("%d", &choice) == 0)
    {
        printf("Some error occured\n");
        scanf("%s", junkChars);
    }
    

    This will clear the input buffer, but relies on an upper-bound. If the user types more than 256 characters, the scanf will continue to consume them on the next iteration(s).

    Clearly this is an ugly solution, but scanf isn’t really a great input mechanism for this reason. Better to use fgets to get the input line as a string, then control the parsing of that string yourself.

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

Sidebar

Related Questions

Did anyone face a problem of putting buttons (or any other widgets) on the
Hi I face some problem with my flash script. Here is my code onFrame
I face a problem with the str_replace function, see the code below : $query
when I insert record that time I face the below problem: java.lang.runtimeexception unable to
I face a recurring problem in the code I write: modifying some global value
My problem is certainly right on my face but I can't see it... I
I use iTextSharp to print a grid view but I face some problems: No
Problem that i face: -I have an input string, a SQL statement that i
I face a problem using Grails 2 submitToRemote tag. The following code is what
I face following problem with three20 photobrowser app : 1: Some time images are

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.