I need to write a program which calculates fibonacci sequence but I stuck because of this infinite loop.
When I enter -5 it prints Please enter "positive" term(s) number:.
Then I enter "a" and it prints Please enter "numeric" term(s) number: infinitely.
I couldn't figure out why it is. Thanks for any help.
(note : I tried to use fflush(stdin) but didn't fix this. I thought maybe \n char left in the stdin buffer.)
#include <stdio.h>
void calculate_fibonacci_sequence(){
int n,is_entry_valid;
int flag = 0;
printf("Please enter term(s) number : ");
while(!flag){
is_entry_valid = scanf("%d",&n);
if(is_entry_valid == 1){
if(n > 0){
flag = 1;
}else{
printf("Please enter \"positive\" term(s) number: ");
}
}else{
printf("Please enter \"numeric\" term(s) number: ");
}
}
}
int main(){
calculate_fibonacci_sequence();
return(0);
}