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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:10:16+00:00 2026-06-16T17:10:16+00:00

Possible Duplicate: How to scanf only integer and repeat reading if the user enter

  • 0

Possible Duplicate:
How to scanf only integer and repeat reading if the user enter non numeric characters?

Hi i’m new to c and i am writing a program that asks the user to enter an integer and if it does not match the criteria of being within a certain range, like lass than 20, but greater than 5. It asks the user to enter the value again.

However i wondered what would happen if i entered a character, and well it creates an infinite loop.

So i want to know is there a way or a specific function that will check the entered value to see if it is the correct data type. And i not repromts the user.

  • 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-16T17:10:18+00:00Added an answer on June 16, 2026 at 5:10 pm

    you can read with scanf("%d") and if the user enter a string containing a non numeric character then you have to clean the stdin buffer before reading again with scanf("%d").

    when you clean the stdin, you will stop the infinite loop

    Use the following function to clean your stdin

    int clean_stdin()
    {
        while (getchar()!='\n');
        return 1;
    }
    

    And The read of your integer from stdin could be done in this way:

    char c;
    do
    {  
        printf("\nEnter an integer from 5 to 20: ");
    
    } while (((scanf("%d%c", &rows, &c)!=2 || c!='\n') && clean_stdin()) || rows<5 || rows>20);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Program doesn’t wait for user input with scanf(%c,&yn); I've just started programming
Possible Duplicate: exiting a while loop with a negative integer I want the user
Possible Duplicate: segmentation fault using scanf I am trying to run a C program
Possible Duplicate: Python : how to append new elements in a list of list?
Possible Duplicate: confusion in scanf() with & operator Why we need a & in
Possible Duplicate: Looking for C# equivalent of scanf I am trying to convert a
Possible Duplicate: When does Endianness become a factor? reading this tuto on endianess, i
Possible Duplicate: How do you allow spaces to be entered using scanf? char inputStr[64];
Possible Duplicate: Using fflush(stdin) My code is: scanf(%d, &_choice); fflush(stdin); gets(input); I use fflush(stdin);
Possible Duplicate: Correct way to use scanf / printf (and family) with fixed size

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.