0

Im trying to verify if the user enters the right value with the return values. He should enter an even number between 0 and 100.

I think I got it right, but now my problem is, that the user has to enter the "enter" key twice to end the scanf function.

Do I have another possibility to avoid the user from doing so?

Heres the code I wrote:

#include <stdio.h>
#include <windows.h> 

int main( void )
{
   int ok, input = -1;
   char c;

   while(input < 1 || input > 100 || input%2 != 0) {      //repeat loop if Input is not even or betwenn 0 and 100
       printf("Enter an odd number between 1 und 100: ");
       int ok = scanf("%d%c", &input, &c);                //Input is correct if ok = 2 and c = 10'\n'

       while((c = getchar()) != '\n' && c != EOF) {}      //this loop empties the input buffer to avoid infinite loops if users enters a character         
   }                                                                        

   printf("You habe chosen the number %d ", input);

   getchar(); 
   return 0;
}
Darius E.
  • 13
  • 5
  • Does this answer your question? [Capture characters from standard input without waiting for enter to be pressed](https://stackoverflow.com/questions/421860/capture-characters-from-standard-input-without-waiting-for-enter-to-be-pressed) – Robert Harvey Jun 17 '20 at 20:26
  • "*the user has to enter the "enter" key twice to end the scanf function*" - are you sure it is `scanf()` that is waiting? The `%c` will read an Enter, and `getchar()` will wait for another Enter. So you need at least 2 Enters for your outer loop to validate the input. Why do you have `scanf()` reading an Enter at all? On a side note, I would suggest using a `do..while` loop instead, do the validation only after `scanf()` is called, not before it is called. – Remy Lebeau Jun 17 '20 at 20:27
  • 4
    Another way to avoid infinite loops is to use `fgets` to read a line, and `sscanf` or `strtol` to extract the number. Reading the entire line first means that there's no mess to clean up if extracting the number fails. – user3386109 Jun 17 '20 at 20:47
  • You read, but overlooked `ok` which BTW suggests you think it is a boolean result. It's the number of items successfully converted, which here must be `2`. – Weather Vane Jun 17 '20 at 21:13
  • You'll want to stop using `scanf()` for everything under the sun. There's other functions that give you a lot more control. – tadman Jun 17 '20 at 23:49
  • @remylebeau that was very useful thank you! Im reading the enter, to make sure that the user only enters a number, if he enters a character I would see that in the c variable. I cant use a do...while loop cause the professor wanted us to solve it using only the while loop, dont ask me why... – Darius E. Jun 18 '20 at 01:11
  • @user338609 Thanks for your answer! I didnt reach fgets and sscanf in my lessons so Im only using the functions that I know :) – Darius E. Jun 18 '20 at 01:15
  • @tadman Thanks for your answer! I didnt reach fgets and sscanf in my lessons so Im only using the functions that I know :) – Darius E. Jun 18 '20 at 01:16
  • @WeatherVane Thanks for answering! 2 questions asked and you commented in both of them. Thats a curious coincidence :) – Darius E. Jun 18 '20 at 01:19

1 Answers1

0

I believe you don't need the second while loop at all.

As suggested above, the scanf() is waiting for enter to be pressed once, and then getchar() is waiting after.

If you remove the second while loop, the code should run correctly and only require the enter key to be pressed once.

I hope this helps!

Reece Coombes
  • 394
  • 2
  • 9
  • There are 2 `while` loops. You didn't say which one to remove. But the requirements say: "He should enter an even number between 0 and 100". The outer `while` loop is being used to ensure that the user actually enters a value in that range. The inner `while` loop is being used to read characters until Enter is reached. It doesn't really make sense to remove either loop. If anything, the `%c` needs to be removed from the `scanf()` instead – Remy Lebeau Jun 18 '20 at 03:39
  • Good point, I've classified which while loop I mean in my answer. However I do think it should work without the second one. The %c will remove '\n' from the input so the second while loop just isn't needed. – Reece Coombes Jun 18 '20 at 07:25
  • the `%c` won't catch an Enter if the user types in something else after the number and before an Enter. But the inner loop will. – Remy Lebeau Jun 18 '20 at 08:52
  • @RemyLebeau you are right! Removing %c from scanf solved my problem! – Darius E. Jun 18 '20 at 15:25