Infinite loop when assigning a float to an integer

rullof

Please i'm having an issue with my program. Whenever i try to input a float it's getting into an infinity loop. I know that the input is stored as an integer. How can prevent the user from entering a float (how to filter the input).

Why is the program getting into an infinite loop when the input is a float.

This is an example:

#include <stdio.h>

main()
{
    int i = 0;
    while(i<10){
        system("cls>null");
        printf("%d^2 = %d\n", i, i*i);

        printf("Index: ");
        scanf("%d", &i);
    }
}
Sergey Kalinichenko

When you call scanf to read a number, but the input contains something incompatible with the input format specifier, scanf does not consume such incorrect input, leaving it in the buffer. Your program does not clear the buffer on input mismatch, entering an infinite loop: scanf tries to read an int again, sees that it's not there, and exits without modifying i. Your loop sees that i is less than 10, and calls the scanf again.

To fix this, check that scanf returned one input. Use the input when it is correct, or call scanf again with the %*[^\n]\n specifier, which means "read to the end of the string, and discard the input":

if (scanf("%d", &i) != 1) {
    scanf("%*[^\n]\n");
}

Note the asterisk - it means that the consumed input needs to be discarded, rather than being written into a variable.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Why is infinite loop not incrementing integer?

From Dev

Why is this loop becoming infinite when using String.valueOf or Float.toString?

From Dev

Why is this loop becoming infinite when using String.valueOf or Float.toString?

From Dev

Type mismatch error when assigning literal to Float

From Dev

Nullpointer Exception when assigning value to Integer array

From Dev

Groovy inconsistent behavior when assigning a string to an integer

From Dev

Incompatible integer to pointer conversion assigning to 'CGFloat *' (aka 'float *') from 'int'

From Dev

Infinite loop when running an activity

From Dev

AngularJS infinite loop when redirecting

From Dev

infinite loop when dispatching in componentWillReceiveProps

From Dev

Infinite Loop when a character is entered

From Dev

Infinite Loop when opening file

From Dev

Float value with 2 decimal places causes infinite loop

From Dev

How to prevent an infinite loop, if a non-integer is entered?

From Dev

Infinite loop when iterating through Linked List

From Dev

Infinite conversion loop when using custom JsonConverter

From Dev

angularjs infinite $digest Loop when no scope changes

From Dev

Infinite loop when trying to count letter frequencies

From Dev

Fortran infinite loop when calling a function

From Dev

Issue with infinite loop when reading from file

From Dev

infinite loop in Haskell when binding variables

From Dev

C# infinite loop when sorting GridView

From Dev

When would you write an infinite loop in Java?

From Dev

Infinite loop in JS when parsing between digits

From Dev

WPF falls into infinite loop when UseLayoutRounding="True"

From Dev

Infinite loop when iterating through Linked List

From Dev

ffmpeg - infinite loop when adding watermark

From Dev

Python: Infinite loop when testing if cell is empty

From Dev

C# infinite loop when sorting GridView

Related Related

  1. 1

    Why is infinite loop not incrementing integer?

  2. 2

    Why is this loop becoming infinite when using String.valueOf or Float.toString?

  3. 3

    Why is this loop becoming infinite when using String.valueOf or Float.toString?

  4. 4

    Type mismatch error when assigning literal to Float

  5. 5

    Nullpointer Exception when assigning value to Integer array

  6. 6

    Groovy inconsistent behavior when assigning a string to an integer

  7. 7

    Incompatible integer to pointer conversion assigning to 'CGFloat *' (aka 'float *') from 'int'

  8. 8

    Infinite loop when running an activity

  9. 9

    AngularJS infinite loop when redirecting

  10. 10

    infinite loop when dispatching in componentWillReceiveProps

  11. 11

    Infinite Loop when a character is entered

  12. 12

    Infinite Loop when opening file

  13. 13

    Float value with 2 decimal places causes infinite loop

  14. 14

    How to prevent an infinite loop, if a non-integer is entered?

  15. 15

    Infinite loop when iterating through Linked List

  16. 16

    Infinite conversion loop when using custom JsonConverter

  17. 17

    angularjs infinite $digest Loop when no scope changes

  18. 18

    Infinite loop when trying to count letter frequencies

  19. 19

    Fortran infinite loop when calling a function

  20. 20

    Issue with infinite loop when reading from file

  21. 21

    infinite loop in Haskell when binding variables

  22. 22

    C# infinite loop when sorting GridView

  23. 23

    When would you write an infinite loop in Java?

  24. 24

    Infinite loop in JS when parsing between digits

  25. 25

    WPF falls into infinite loop when UseLayoutRounding="True"

  26. 26

    Infinite loop when iterating through Linked List

  27. 27

    ffmpeg - infinite loop when adding watermark

  28. 28

    Python: Infinite loop when testing if cell is empty

  29. 29

    C# infinite loop when sorting GridView

HotTag

Archive