Linked Questions
85 questions linked to/from Why is scanf() causing infinite loop in this code?
5
votes
2
answers
13k
views
Why does scanf get stuck in an infinite loop on invalid input? [duplicate]
In line 5 I read an integer and isint is getting 1 if it reads an integer or 0 if it's not an integer. If isint is 0 I have a loop asking user to give an integer and I read until the user gives an ...
1
vote
2
answers
50k
views
How to make this program ask for input again if invalid input is entered (in C programming)? [duplicate]
There are two different spots where the check happens.
That is, where you enter a, s, m, d, or q and when you enter the first and second number.
At any of the checks, if the check is false, it ...
2
votes
5
answers
2k
views
Enter a character as an input to an integer variable, giving an infinite loop [duplicate]
#include<stdio.h>
#include<ctype.h>
int main()
{
int number, sum = 0;
// loop body is executed at least once
do
{
printf("Enter a number: ");
scanf("%d", &...
0
votes
2
answers
4k
views
How to check if scanf() worked fine in C? [duplicate]
I am trying to see if user enters 3 correct input. If not the loop ask for input again and again until they correct it correctly.
#include <stdio.h>
int main(void) {
int num1,num2;
...
0
votes
1
answer
3k
views
Why this example is stuck in an infinite loop in C? [duplicate]
In the example below, if I enter a character in Mac OS X terminal, the program will get stuck in an infinite loop, printing Please enter a number: line after line and never allowing the user to input ...
0
votes
1
answer
1k
views
Menu driven program ends in an infinite loop [duplicate]
I have been struggling to figure out a way to enhance the error checking in my simple menu driven C code shown below. The code works fine with the right inputs, but unfortunately it goes in an ...
-1
votes
3
answers
1k
views
C Program Won't End When User Enters Invalid Character [duplicate]
I'm trying to write a C program that computes X^Y (X to the Y power) without using the 'pow' function. The program works fine when I enter numbers, but I'm having an issue with the code not stopping ...
3
votes
3
answers
581
views
c programming scanf will not read input twice [duplicate]
I am writing a program in CodeBlocks in C. I want to check if the input is in the right type and if not try to get it again.
My problem is whenever I'm trying to get the input with scanf and the ...
0
votes
1
answer
1k
views
Checking input types with scanf() in a while loop [duplicate]
I've been writing a program that takes an input and checks if the number is even or odd and outputs an error message if the input is a character not a number my initial code was:
int main()
{
int ...
0
votes
1
answer
724
views
Code making an infinite loop when ask input with scanf [duplicate]
i have a function that ask to the user to enter a value to calculate its square root, however when i try to validate that the number of the input must be a number and not a char, it makes an infinite ...
-5
votes
1
answer
871
views
C program goes into infinite loop with scanf [duplicate]
Here is a program that "works", but will go into an infinite loop if character data "a" is entered or "-8" is entered.
Here is the expected output when data is entered into the program:
****Input (...
0
votes
3
answers
380
views
scanf resulting in infinite loop [duplicate]
I had this problem before but went around it using other operator. But the same operator can't be used here I think (the getche();). Anyway this works well and good but if I input a letter it goes ...
-2
votes
1
answer
595
views
C scanf ignored when invalid input [duplicate]
I wrote this code, and all it meant to do is input ints until a negative is entered:
int result = 0, input_number = 0;
while(input_number >= 0){
printf("Please enter students IDs(negetive to ...
0
votes
1
answer
349
views
scanf in C leading to infinite loop [duplicate]
I am trying to write a simple c program that takes input using scanf, and ensures that this input is an integer. To do this I have written a recursive function, but the function goes into an infinite ...
1
vote
0
answers
351
views
Using scanf return value in a for loop [duplicate]
I tried to use the return value of the scanf function in a for-loop
#include<stdio.h>
int main(){
int a, b, c;
for(a = 0; a < 10; ++a){
c = scanf("%d", &b);
if(c != 1)
...