While loop to validate input is a number in C?

King Sutter

So today I am trying to find a way to check if this input is a number. I cant find a way to make this code work so far. If I enter a number, the while loops ends, which is my intention. But, if I enter anything else, the code SHOULD print the printf once and then reprompt me for input (via the scanf statement back at the top of loop). But it instead prints the printf statement infinitely. How can I fix this?

float num1;
while (scanf("%f",&num1)==0)
    {
        printf("Invalid input. Please enter a number: ");
    }
StoryTeller - Unslander Monica

If scanf fails to preform the requested conversion, it will leave the input stream unchanged. So while your check is correct, you need to clean the input stream of the erroneous input before re-attempting to read a number again.

You can do it with scanf itself and and the input suppression modifier:

float num1;
while (scanf("%f",&num1)==0)
{
  printf("Invalid input. Please enter a number: ");
  scanf("%*s");
}

%*s will instruct scanf to parse the input as though it's attempting to convert a string of characters (removing characters in the process from the stream), but thanks to the asterisk, it won't attempt to write it anywhere.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Validate the input using a while loop

From Dev

How to validate input string using while loop

From Dev

Check if input is a number in a while loop

From Dev

input a number in a while loop for a python bowling program

From Dev

python checking user input for number in a while loop

From Dev

Number triangle with while loop in c

From Dev

Python 3 - 'While loop' to validate that the user input is between 2 numbers

From Dev

Validate number Input

From Dev

Validate if input number is prime

From Dev

while loop not prompting for user input (c++)

From Dev

C while loop between number range

From Dev

Count the total number of names in a 'while loop' from user input, in Python

From Dev

largest and smallest number using input while loop python

From Dev

create form number input and validate it

From Dev

Angular 6 validate number input

From Javascript

While Loop Input Issue

From Dev

Providing input for while loop

From Dev

While loop user input?

From Dev

The number of commands in while loop

From Java

Input any number and print all the odd number in descending order using while loop

From Dev

Catch user input when running while loop in C

From Dev

Get user input without blocking while loop using threads in C

From Dev

getting infinite loop in c while using eof for specific value of input

From Dev

why while loop do not need user to input in c++

From Dev

c - while loop keeps ignoring scanf after bad input

From Dev

C++ while loop to read from input file

From Dev

While loop in C iterates twice before asking for user input

From Dev

C while loop does not stop running once input is made

From Dev

Taking input from user using do while loop in c

Related Related

HotTag

Archive