How to let scanf() keep reading integer inputs unless a character is input?

Crysishy

I'm trying to do a program that reads inputs from user and determine whether the integer is Fibonacci or not.

The program should print out num is fib or num is not fib where num is a positive integer;

if the input is a non-positive number, the program should not produce any output (but it should produce an error mesage on stderr) and it should keep prompting;

if the input contains any characters, the program should not produce any output (but it should produce an error mesage on stderr) and then exit.

This is the expected results:

1 2 3 4 5
1 is fib
2 is fib
3 is fib
4 is not fib
5 is fib

(keep prompting inputs)

1 -2 3 -4 5
1 is fib
Error: input value -2 is not positive
3 is fib
Error: input value -4 is not positive
5 is fib

(keep prompting inputs)

1-23-45
1 is fib
Error: input value -23 is not positive
Error: input value -45 is not positive

(keep prompting inputs)

12a
12 is not fib
Error: input is not a number

I was able to check the first input number, but when it's done, the program exits itself. How do I let the scanf() know when to keep reading and when to exit?

This is my code

int main() {
    int num, x, y, z;

    while (scanf("%d", &num) > 0) {
        if (num <= 0) {
            fprintf(stderr, "Error: input value %d is not positive\n", num);
            return -1;
        } else {
            x = 0;
            y = 1;
            z = x + y;
            while (z < num) {
                x = y;
                y = z;
                z = x + y;
            }
            if (z == num) {
                printf("%d is fib\n", num);
            } else {
                printf("%d is not fib\n", num);
            }
            return 0;
        }
    }
    return 0;
}
chqrlie

You currently exit the main function when scanf returns less than 1. If a character is present in stdin that does not begin a number, such as a, scanf("%d", &num) will return 0, you will exit the program, but without a message. You also exit with return 0; after the first result, which is incorrect.

You must change to logic when a number cannot be parsed and only exit silently at end of file, when scanf returns EOF:

#include <stdio.h>

int main(void) {
    int status, num, x, y, z, exit_code = 0;

    while ((status = scanf("%d", &num)) >= 0) {
        if (status == 0) {
            fprintf(stderr, "Error: input is not a number\n");
            return 1;
        }
        if (num <= 0) {
            fprintf(stderr, "Error: input value %d is not positive\n", num);
            exit_code = 1;
            continue;
        }
        x = 0;
        y = 1;
        z = x + y;
        while (z < num) {
            x = y;
            y = z;
            z = x + y;
        }
        if (z == num) {
            printf("%d is fib\n", num);
        } else {
            printf("%d is not fib\n", num);
        }
    }
    return exit_code;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Reading character with scanf()

From Dev

How to handle exception when scanf of integer gets a character

From Dev

How to Stop Reading Input After a Certain Character?

From Dev

How to get integer input in an array using scanf in C?

From Dev

How to get Integer and float input without `scanf()` in c?

From Dev

how to keep 10 biggest integer while reading a list in java?

From Dev

how to keep 10 biggest integer while reading a list in java?

From Dev

Scanf to check reading of integer doesn't work

From Dev

How would I only let the user input one character in c

From Dev

How would I only let the user input one character in c

From Dev

Scanning character and integer in single scanf multiple times

From Dev

scanf() only reading first input (number)

From Dev

Scanf_s reading wrong input

From Dev

scanf check in while loop to restrict integer input

From Dev

Using Scanf() for taking input for both string and integer

From Dev

Using cin, how can I accept a character or an integer as an input?

From Java

Reading an integer from user input

From Dev

C: scanf input single character and validation

From Dev

Reading input one character at a time

From Dev

Reading input one character at a time

From Dev

Character by character reading the input from JEditorPane in Java

From Dev

re.split unless following character an integer in Python

From Dev

How to print decreasing odd numbers starting from scanf input integer in C?

From Dev

How to print decreasing odd numbers starting from scanf input integer in C?

From Dev

How to avoid spaces, enter key or non-numeric input in C ? scanf reads integer value

From Dev

How to keep a While True loop running with raw_input() if inputs are seldom?

From Dev

Keep lexer from reading first character in file

From Dev

scanf skipped after reading in integer in C, in while loop

From Dev

C Program :Error when reading character from stdin using scanf

Related Related

  1. 1

    Reading character with scanf()

  2. 2

    How to handle exception when scanf of integer gets a character

  3. 3

    How to Stop Reading Input After a Certain Character?

  4. 4

    How to get integer input in an array using scanf in C?

  5. 5

    How to get Integer and float input without `scanf()` in c?

  6. 6

    how to keep 10 biggest integer while reading a list in java?

  7. 7

    how to keep 10 biggest integer while reading a list in java?

  8. 8

    Scanf to check reading of integer doesn't work

  9. 9

    How would I only let the user input one character in c

  10. 10

    How would I only let the user input one character in c

  11. 11

    Scanning character and integer in single scanf multiple times

  12. 12

    scanf() only reading first input (number)

  13. 13

    Scanf_s reading wrong input

  14. 14

    scanf check in while loop to restrict integer input

  15. 15

    Using Scanf() for taking input for both string and integer

  16. 16

    Using cin, how can I accept a character or an integer as an input?

  17. 17

    Reading an integer from user input

  18. 18

    C: scanf input single character and validation

  19. 19

    Reading input one character at a time

  20. 20

    Reading input one character at a time

  21. 21

    Character by character reading the input from JEditorPane in Java

  22. 22

    re.split unless following character an integer in Python

  23. 23

    How to print decreasing odd numbers starting from scanf input integer in C?

  24. 24

    How to print decreasing odd numbers starting from scanf input integer in C?

  25. 25

    How to avoid spaces, enter key or non-numeric input in C ? scanf reads integer value

  26. 26

    How to keep a While True loop running with raw_input() if inputs are seldom?

  27. 27

    Keep lexer from reading first character in file

  28. 28

    scanf skipped after reading in integer in C, in while loop

  29. 29

    C Program :Error when reading character from stdin using scanf

HotTag

Archive