0

this part of program which will calculate function. The main thing that does`t work is when i input letter I get an infinite loop. Can anyone help me?

#include <stdio.h>
#include <math.h>
#include <float.h>

int main()
{
    int k;
    double x;
    do{
        printf("input x:");
        k = scanf("%lf",&x);
        if(x<0 || fmod(x,90) == 0 || x > DBL_MAX || k == 0){
            printf("Wrong Format!Try again!\n");
            continue;
        }
    } while(x<0 || fmod(x,90) == 0 || x > DBL_MAX || k == 0);
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • 2
    When scanf fails because input was a letter, **that** letter is not removed from the input buffer. You do a `continue;` and scanf will fail again and again and again and again .... because of **that** letter (always the exact same letter). Suggestion: read all user input with `fgets()`. – pmg May 02 '20 at 15:04
  • Thanks, I will try fflush, as I didn`t get to fgets() at the moment. Thats explains everything! – Valery Dauzhuk May 02 '20 at 15:28
  • 1
    How can i like you? – Valery Dauzhuk May 02 '20 at 15:29
  • lol... just click the up triangle for my comment, thank you – pmg May 02 '20 at 15:34
  • Does this answer your question? [Why is scanf() causing infinite loop in this code?](https://stackoverflow.com/questions/1716013/why-is-scanf-causing-infinite-loop-in-this-code) – Joseph Sible-Reinstate Monica May 03 '20 at 03:23

0 Answers0