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

  • SEARCH
  • Home
  • 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 775923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:22:08+00:00 2026-05-14T19:22:08+00:00

The program asks for a number. The program should loop the scanf() function if

  • 0

The program asks for a number. The program should loop the scanf() function if the user types anything else other than numbers.

The code is,

 do{
     printf("Enter row number\n");
     scanf("%d",&row);
  }while(row>='a' && row<='z');

But the above code doesn’t work. I keep getting an error when typing in a letter. I tried manipulating around it and the whole thing loops infinitely.

Any suggestions?

Also how can I tell the C Compiler not to break the loop unless the input is an INTEGER?

  • 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-05-14T19:22:09+00:00Added an answer on May 14, 2026 at 7:22 pm

    You are ignoring the return from scanf() which tells you whether the typed information was accurate for the conversion (%d) or not. If it was inaccurate, you have to do error recovery, which is not particularly easy with scanf(). Most people go for the approach of ‘read a line of input and then parse it’, where the error recovery is simpler.


    I understand that return values as essential in error checking, but how do I scan if its numbers or letters? Can I say if (input!=(integers)) or anything similar?

    This is why people don’t use scanf(). If you get the line of data into a buffer (character array), then you can check the contents of the array as often as you like. If you use scanf(), you don’t get a reliable chance to process the data until after scanf() decides it has an error.

    The functions (usually also available as macros) in <ctype.h> allow you to classify characters. The functions in <stdlib.h> provide reliable conversions from strings to integers of various sorts.

    So, you can think about doing something like:

    char buffer[256];
    
    while (fgets(buffer, sizeof(buffer), stdin))
    {
        ...check contents of buffer using isdigit() etc...
        ...or just plough ahead with...
        long value;
        char *end;
        errno = 0;
        value = strtol(buffer, &end, 0);
        if (errno != 0 || (*end != '\0' && !isspace(*end))
            ...diagnose problems...
    }
    

    This code is a bit out of my league at the moment .. is there a simpler way?

    Well, I suppose you can use atoi() instead of strtol(), which simplifies the error handling (because it is less precise):

    char buffer[256];
    
    while (fgets(buffer, sizeof(buffer), stdin))
    {
        int value = atoi(buffer);
        if (value == 0)
        {
            puts("zero read - exiting loop");
            break;
        }
    }
    

    It doesn’t get much simpler than this. I don’t know which part of the previous solution you felt was beyond you. The alternatives, it seems to me, are much fiddlier, involving reading one character at a time and saving the digits and rejecting the non-digits:

    char buffer[256];
    char *dst = buffer;
    int c;
    int value;
    
    while ((c = getchar()) != EOF)
    {
        if (isdigit(c))
        {
            *dst++ = c;  /* Ignoring buffer overflow - bad! */
        }
        else if (isspace(c))
        {
            *dst = '\0';
            value = atoi(buffer);
            break;
        }
        else
        {
            printf("Unexpected character '%c'!\n", c);
        }
    }
    

    Etcetera. There are various issues to resolve in that code – like resetting the pointer after an erroneous character, and avoiding buffer overflow, and dealing with signs on the numbers, and … well, all sorts of stuff that I’d rather leave to routines like fgets() and strtol().

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

Sidebar

Ask A Question

Stats

  • Questions 423k
  • Answers 423k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Make your AbstractServlet generic: public class AbstractServlet<T extends AbstractUser> {… May 15, 2026 at 11:38 am
  • Editorial Team
    Editorial Team added an answer Just iterate over it with something like array_shift or array_pop:… May 15, 2026 at 11:38 am
  • Editorial Team
    Editorial Team added an answer Your table second design is better, and, is "normalised". You… May 15, 2026 at 11:38 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.