0

I am totally new to C. I want to re-prompt when the input value is not a number and when it's a number it should be less than 1. when I give any sort of string it works correctly. But when I give any number it goes to the next line without printing "Number: ".Then in the next line, it prints "Number: "again if the input value is less than 1.

int x;
printf("Number: ");
while (scanf("%d", &x) != 1 || x < 1 )
{

    printf("Number: ");

    scanf("%*s");
}

and the result it gives me is this

result

  • "I want to re-prompt when the input value is not a number and is less than 1": what do you mean by "and is less than 1"? Why will you check if a string is "less than 1"? Do you mean "or the number is less than 1"? – JASLP doesn't support the IES Jul 11 '21 at 04:16
  • 1
    The `scanf("%*s")` call won't work as expected in all cases. My suggestion is that you read *lines* (with e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets)) instead, and then use `sscanf` to parse the input. – Some programmer dude Jul 11 '21 at 04:18
  • 2
    My advice is to stop mucking about with simple user input and just use a tried and tested method: https://stackoverflow.com/questions/4023895/how-do-i-read-a-string-entered-by-the-user-in-c/4023921#4023921. Once you've got the line, you can then use `sscanf` to check and retry. – paxdiablo Jul 11 '21 at 04:20
  • 1
    Did you try to compile your code with a recent [GCC](http://gcc.gnu.org/) invoked with all warnings and debug info: `gcc -Wall -Wextra -g` ? You could get warnings, and improve your C code to get none of them! Once your code has no warnings, use the [GDB](https://www.gnu.org/software/gdb/) debugger to understand its behavior. – Basile Starynkevitch Jul 11 '21 at 07:17
  • The easiest fix would be to change `scanf("%*s");` -> `scanf("%*[^\n]");` but you should really avoid this and learn how to use fgets+sscanf. – n. m. could be an AI Jul 11 '21 at 10:04

2 Answers2

1

It would be wise to use fgets to read the line, then use sscanf to parse the input. That way, you can get the line, then check if sscanf succeeds! Simple example:

int target_number; // The number you will have at the end of this.
while (1) { // Loop for rechecking number
    char line[16]; // See notes on how to read the whole line.
    fgets(line, sizeof(line), stdin);

    // We use 1 here because sscanf returns the number of format specifiers that are matched. Since you only need one number, we use 1.
    if (sscanf(line, "%d", &target_number) != 1) {
        fprintf(stderr, "Invalid Input! Please enter in a valid number.");
        continue;
    }
}

// Do whatever you will with target_number

Notes

You can see how to read the whole line here.

This code is not safe!

It does not protect against buffer overflow attacks and the like. Please see this on the right way to do this. If this is just for learning, you don't need to worry.

Isacc Barker
  • 507
  • 4
  • 15
0
/*This is how it will work the way you want. 
If I understand  your goal correctly, of course? 
If your goal was different, 
please specify and I will try to solve it.*/



#include<stdio.h>

int main(void)
{
int x;
printf("Number: "); 
while (scanf("%d.%*d", &x)!=1 || x<0) 
{
if(x<0)
{
printf("Number: ");
continue;
}
else
printf("Number: ");
scanf("%*s");
}
printf("Hello world!");
return 0;
}
Генс
  • 93
  • 5
  • My goal is to ask for input again and again until I give it a number that is more than 0. That number can be a float also. For example, if I input a string then it will again ask for input. If I input a negative number then it will ask for input again. But if I input any positive int/float number then it will go for the rest of the code. – MD Sharif Hossain Jul 11 '21 at 09:21
  • I changed my code. Check it out. If this is what you wanted, I will be glad. – Генс Jul 11 '21 at 10:42
  • Your code didn't work as it should because the conditions that you specified in while ( ) are at different levels of perception for the compiler. Therefore, the condition x<0 must be handled separately. – Генс Jul 11 '21 at 12:49
  • Brother this code works fine but when a negative fractional number is given as input it doesnt work. – MD Sharif Hossain Jul 12 '21 at 05:26
  • Well, then that's it. Check it out. Just what you need. Look, I changed the code a little. – Генс Jul 12 '21 at 08:19
  • Secret ingredient.))) – Генс Jul 12 '21 at 14:00
  • Bro in this updated code if I input anything like (-0.2,-0.4,-0.83) it executes the rest of the code. But I want to re-prompt for input in this case too. – MD Sharif Hossain Jul 12 '21 at 15:38
  • I think we should listen to our older comrades. – Генс Jul 12 '21 at 18:01