1

I dont understand why i have to press enter twice when entering an integer? I assume its due to the (fgetc(stdin) != '\n'); function.This is used so that when letters are input it stops it from infinitely looping.

How can I avoid having to press enter twice to get output?

Im just trying to validate if a number has been entered so if letters are entered then it will prompt the user to re-enter input

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>

double val;
char follow;
int count = 0;
int main(void) {

   while (count < 1) {
      printf("enter an number \n");
      int read = scanf_s("%lf%c", & val, & follow);
      while (fgetc(stdin) != '\n'); //prevent unlimited loop with character. Potential reason for pressing enter twice. 

      count = 0;

      if (read == 2) {
         if (isspace(follow)) {
            printf("input is an integer followed by whitespace, accept \n");
            printf("%lf", val);
            count = count + 1;
         } else {
            printf("input is an integer followed by non-whitespace, reject");
         }
      } else if (read == 1) {
         printf("input is an integer followed by EOF, accept");
         printf("%lf", val);
         count = count + 1;
      } else {
         printf("input is not an integer, reject");
      }
   }
}
  • 3
    Don't have that loop? What are you really trying to do? What is the problem your program is supposed to solve? And why do you ask the user to enter an integer, and read it as a floating-point value? – Some programmer dude Apr 06 '21 at 15:19
  • 3
    I also recommend that you [read whole lines](https://en.cppreference.com/w/c/io/fgets) into a string and then try to parse that string instead. Possibly by doing e,g, `if (sscanf(input, "%d %d", &val1, &val2) == 2)` – Some programmer dude Apr 06 '21 at 15:25
  • 1
    If `read` is 2 and `follow` is `'\n'` then you have already read the newline character into `follow`, so the `while (fgetc(stdin) != '\n');` will be reading in and skipping the next line. Also, it ought to check for `EOF` as well as `'\n'`. – Ian Abbott Apr 06 '21 at 15:48
  • https://stackoverflow.com/questions/58403537/what-can-i-use-for-input-conversion-instead-of-scanf has some good thoughts on why `scanf` is not a good idea for processing user input, and what alternatives are available. – Nate Eldredge Apr 07 '21 at 03:40

1 Answers1

0

Simply had to move the while (fgetc(stdin) != '\n'); below the else and else if statments

    switch (number) {
        case 1:
            printf("Managers\n");


            while (count < 1) {
                printf("enter an number\n");
                int read = scanf_s("%lf%c", &val, &follow);


                if (read == 2) {
                    if (isspace(follow)) {
                        printf("input is an integer followed by whitespace, accept \n");
                        printf("%lf", val);
                        count = count + 1;

                    }
                    else {
                        printf("input is an integer followed by non-whitespace, reject");
                        while (fgetc(stdin) != '\n'); //prevent unlimited loop with character. Potential reason for pressing enter twice. 

                    }
                }
                else if (read == 1) {
                    printf("input is an integer followed by EOF, accept");
                    printf("%lf", val);
                    count = count + 1;
                }
                else {
                    printf("input is not an integer, reject");
                    while (fgetc(stdin) != '\n'); //prevent unlimited loop with character. Potential reason for pressing enter twice. 

                }
            }