why is my program skipping scanf()? [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Part of my assignment is to get user input; then, if the input is incorrect, prompt them to try again. When I use if statements and while loops in this way, they completely skip over the scanf command, and the program either runs infinitely or terminates immediately, skipping over the second chance for user input. Any idea how to fix this?
#include <stdio.h>
int main(void)
{
int number;
printf("please insert positive number\n");
scanf(" %d", &number);
// if the user inputs a value that is not a positive integer, it becomes false
// i want the if statement to re-prompt them to input the correct value
if (number <= 0)
{
printf("try again\n");
scanf(" %d", &number);
}
return 0;
}

scanf Returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned.
The scanf function reads data from the standard input stream stdin and writes the data into the location given by argument. Each argument must be a pointer to a variable of a type that corresponds to a type specifier in format.
I recommend handle errors . it will happen in Real World code. (write Concise and robust Code).
so i recommend something like this:
#include <stdio.h>
int read_positive_int(int *n){
int retry = 3;
do{
printf("Please enter a positive number:\n");
if (scanf("%d", n) == 1){
if (*n > 0) return 1; //ok
}
} while (--retry);
return 0; //error
}
int main(void)
{
int n;
if (read_positive_int(&n))
printf("n = %d\n", n);
else
printf("error\n");
}
i hope this helps.

the code does what it's written to do, so in your case it's following:
message to user: enter a positive number
scanf: reads user's input
determine, if the number is positive
if yes - the if body will not be executed
and program ends
if not - print a message, read a number and program ends
the thing you are looking for is a loop
you want to do something like this:
while the entered number is not positive, ask the user to enter the number again
that's what loops are used for
int number;
printf("please insert positive number\n");
scanf("%d", &number);
while (number <= 0) {
printf("try again\n");
scanf("%d", &number);
}
return 0;

Related

isdigit() and system("cls") produce infinite loop

I was at first having trouble with a scanf() function being skipped, but I fixed that by adding in a space before %c in the scanf() function.
When trying to ask for input from the user as to whether the screen should be cleared, the scanf(" %c", cClear); conversion specifier gives an infinite loop, it is expecting a character, but responds to input as if not a character.
I believe it may have something to do with my input buffer.
I tried to use fflush(stdin) to no avail, I also used printf("%d", (int) cClear); to see the output, which was zero.
One other problem I have is trying to check user input for a digit.
I use:
if (isdigit(iSelection) == 0) {
printf("\nPlease select a valid numerical value.\n");
continue;
to check user input and restart the while loop, but anytime a character is entered and not an integer, I get an infinite loop.
My goal is to give the user the option to clear the screen after each calculation, and to also check input for being a digit.
Any help is appreciated.
//excluding code prior to main() and function definitions
int main(void) {
int iSelection = -1;
double foperand1 = 0, foperand2 = 0;
int ioperand1 = 0, ioperand2 = 0;
char cClear = '\0';
while (iSelection) {
printf("\n\nTHE CALCULATOR\n");
printf("\nCalculator menu:\n");
printf("\n1\tAddition");
printf("\n2\tSubtraction");
printf("\n3\tMultiplication");
printf("\n4\tDivision");
printf("\n5\tModulus (Integers only)");
printf("\n6\tTest if Prime (Integers only)");
printf("\n7\tFactorial (Integers only)");
printf("\n8\tPower");
printf("\n9\tSquare Root");
printf("\n0\tExit\n");
printf("\nPlease enter your selection: ");
scanf("%d", &iSelection);
//here we check for if input was a digit
if (isdigit(iSelection) == 0) {
printf("\nPlease select a valid numerical value.\n");
continue;
switch(iSelection) {
case 0:
break;
case 1:
printf("\nEnter the two numbers to add seperated by a space: ");
scanf("%lf %lf", &foperand1, &foperand2);
printf("\n%.5lf + %.5lf = %.5lf\n", foperand1, foperand2, addNumbers(foperand1, foperand2));
break;
}
//here we ask the user if they want to clear the screen
fflush(stdin)
if (iSelection != 0) {
printf("\nDo you want to clear the screen? ('y' or 'n'): ");
scanf("%c", cClear);
//printf("%d", (int) cClear); //used this to help debug
//scanf("%d", iSelection);
if (cClear == 'y')
system("cls");
}
}
printf("\nExiting\n");
return 0;
}
one error I get is "system" is declared implicitely. Could it possibly be the windows operating system not recognizing the pre defined function call?
Thanks to the people who commented to help me figure this out.
I had forgotten to add the (&) to the scanf() function call for the system"cls" function call, as well as didn't include the correct library (stdlib.h).
I was also able to make the program stop skipping the scanf() function by adding a space to the " %c" conversion specifier.
scanf Getting Skipped
I was able to make the isdigit() function work by changing the variable 'iSelection' to a character, but then I also had to change my case values to characters, not integers.

How to detect an invalid input by user in C? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I need to write a code where you ask the user for number between 3-69. If the input is incorrect I have to print in stderr either "wrong input" (if it's a letter) or "out of range" if it doesn't belong in the interval.
I can deal with the "out of range" stderr, but not the other one. When I input a letter it gets automatically transformed into some big number, which is I suppose an adress in memory or something. In that case the stderr becomes "out of range" and not "wrong input". Is there a way to fix it?
Maybe it's wrong that I introduced the variables as int?(these are the lines in code that get the input):
int x, y;
scanf("%i %i",&x,&y);
When I input a letter it gets automatically transformed into some big number, which is I suppose an adress in memory or something.
It's whatever garbage was in x and y.
scanf("%i %i",&x,&y); says to look for two integers. If scanf doesn't see two integers it will fail and it will not write anything to x nor y. Because you're not checking if scanf succeeded, when you read x and y they will be uninitialized; they'll contain whatever garbage was in memory at the time.
The naive approach is to first check if scanf succeeded. Then check if the numbers are in the right range. I'll simplify the problem to a single number for illustrative purposes.
#include <stdio.h>
int main() {
int num;
while(1) {
if( scanf("%i ",&num) != 1 ) {
fprintf(stderr, "Please enter a number.\n");
continue;
}
if( num < 3 || 69 < num ) {
fprintf(stderr, "The number must be between 3 and 69.\n");
continue;
}
break;
}
printf("Your number is %i.\n", num);
}
But when the user enters "a" this will enter an infinite loop.
$ ./test
a
Please enter a number.
Please enter a number.
Please enter a number.
When scanf fails it leaves the user's input on stdin. The above program will read the same a over and over again. This is a general problem with scanf, it is designed for well-structured input and user input is not well-structured. See the C FAQ for more problems with scanf.
Instead, read and parse separately. Read the whole line with fgets, parse it with sscanf, and if it isn't what you want read another line.
#include <stdio.h>
int main() {
int num;
char line[1024];
while(fgets(line, sizeof(line), stdin)) {
if( sscanf(line, "%i ",&num) != 1 ) {
fprintf(stderr, "Please enter a number.\n");
continue;
}
if( num < 3 || 69 < num ) {
fprintf(stderr, "The number must be between 3 and 69.\n");
continue;
}
break;
}
printf("Your number is %i.\n", num);
}

C Program Won't End When User Enters Invalid Character [duplicate]

This question already has answers here:
Why is scanf() causing infinite loop in this code?
(16 answers)
Closed 4 years ago.
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 when the user enters a character that isn't a number. It runs through the program once more after giving the message "The character you have entered is not a number.Please try again.The character you have entered is not a number.Please try again. The value is 1." Can someone help? I'm going insane trying to figure this out.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x, y, i,n=1,val;
printf("Please enter a number: \n");
scanf("%d", &x);
if (x!=1)
printf("The character you have entered is not a number.Please try again.");
else
printf("What power is the number raised to?\n");
scanf("%d", &y);
if (y!=1)
printf("The character you have entered is not a number.Please try again.");
else
for(i=1;i<=y;i++)
{
val=x;
n=n*val;
}
printf("The value is %d.\n", n);
return 0;
}
When your program detects invalid input, it prints a message, but otherwise just continues on ahead as if a valid input had been read. Instead, it needs to do one of these things:
abort when invalid input is detected, or
consume and ignore the invalid input, AND
loop back to provide a chance to enter new input, or
use a default value.
Its message suggests that the program will provide for entering new input, but in fact it does not follow through. Moreover, if it is the input for the first prompt that is invalid, then that input is still waiting to be read when the program prints the second prompt, where it is still invalid, and the program blithely continues on a second time, ultimately printing the initial value of n, 1, without having calculated anything.
You need to check how many items scanf successfully read:
int numItemsRead = 0;
printf("What power is the number raised to?\n");
numItemsRead = scanf("%d", &y);
if (numItemsRead!=1)
printf("The character you have entered is not a number.Please try again.");
} else {
'''
}
int main()
{
int x, y, i,n=1,val;
printf("Please enter a number: \n");
scanf("%d", &x);
if (x!=1)
printf("The character you have entered is not a number.Please try again.");
return 0;
else
printf("What power is the number raised to?\n");
scanf("%d", &y);
Maybe if u do like that even if u entered a invalid character program will be end.

Number in for loop doesn't increment [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I got the task to scan 10 numbers that will later on be converted into characters. The problem is that I don't get why there is an infinite loop if I don't enter 0. I got the task right with array but I am interested why does this happen in example bellow.
#include <stdio.h>
#include <stdlib.h>
int main() {
/**
* for the example enter numbers: 8 5 12 12 15 23 15 18 12 4 -> helloworld
*/
char n;
// message needs to be 10 numbers long.
for (int i = 1; i <= 10; i++){
// enter message in numbers.
scanf("%d", &n);
// check if it is 0. if it is, end the message.
if(n == 0) {
printf("\nEnd of the message!");
break;
// if number is not 0, add 64 to transform to char.
}else {
n = n + 64;
// print the char.
printf("%c ", n);
// print the i, that doesn't increment.
printf(" -> i:%d\n", i);
}
}
return 0;
}
You are using
char n;
...
scanf("%d", &n);
You cannot use %d with char. You should change n to an int or use %c for scanf and printf.
int n;
...
scanf("%d", &n);
OR
char n;
...
scanf("%c", &n);
You are using a char to read an int. The scanf fails and input remains in the buffer and so scanf keeps on reading the same value again and again resulting in an infinite loop.
So, declare n as an int.
It is a good practice to check the return value of scanf so that you will know if the input has been read properly.
The scanf function returns the value of the macro EOF if an input failure occurs before the first conversion (if any) has completed. Otherwise, the function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure
The problem is with the scan!
scanf("%c", &n);
%d for ints, %c for chars, %s for strings, %f for floats!
scanf("%d", &n) reads an int into n. Since n is a char this results in the 3 bytes that appear after n being overwritten by the scanf. In your case the variable i was allocated within memory that overlaps those 3 bytes and so each call to scanf modifies the variable i resulting in a potentially infinite loop. Use %c to read in a character rather than %d.

How to make sure that a value entered is a number? [duplicate]

This question already has answers here:
Check if input is integer type in C
(16 answers)
Closed 8 years ago.
I am working on code for one of my classes and I have hit a wall. I need the user to input a number that will be used as the number of times a for loop will be repeated. The first loop where I ask for this number is a while loop. I need to make sure the value entered is a number and not a letter or special character.
I do not know how to make sure that it is not a letter or special character.
The next problem is making sure that the for loop only runs for the specified number of times that is the number provided in the first loop.
This is what I have written so far.
#include <stdio.h>
int main()
{
int num_of_scores, n;
char enter;
float score=-1, total=0, average;
do
{
printf("\n\nEnter the number of quiz scores between 1 and 13: ");
scanf ("%d, %c", &num_of_scores, &enter);
}
while(num_of_scores<1 || num_of_scores>13/* && enter == '\n'*/);
printf("\nStill Going!");
for(n=0; n<num_of_scores; n++)
{
printf("\nEnter score %i: ", n+1);
scanf ("%f", &score);
while(score>=0 || score<=100)
{
total = total + score;
score = -1;
break;
}
}
average = total / num_of_scores;
printf("\nThe average score is %.0f.\n\n", average);
return 0;
}
So I have edited the code a little bit. There is a part in the first while loop that is in a comment which i removed because it made the program end after that loop. The printf("still going") is just a test to make sure the program gets that far. Any further pointers? I am still not sure how to check make sure a number is not entered. I though adding the && enter == '\n' would do it, but if it hangs the program it is no good. Many of the examples you have suggested are good, but i find them a little confusing. Thanks!
I'd check Check if input is integer type in C for the answer to this...
Check the return value of scanf. Per the man page:
RETURN VALUE
These functions return the number of input items successfully matched and assigned, which can be fewer than provided
for, or even zero in the event of an early matching failure.
The value EOF is returned if the end of input is reached before either the first successful conversion or a matching
failure occurs. EOF is also returned if a read error occurs, in which case the error indicator for the stream (see
ferror(3)) is set, and errno is set indicate the error.
do{
char ch = 0;
num_of_scores = 0;
printf("\nEnter the number of quiz scores between 1 and 13: ");
if(scanf("%d%c", &num_of_scores, &ch)!=2 || ch != '\n'){
int ch;
while((ch=getchar())!='\n' && ch !=EOF);
}
} while(num_of_scores<1 || num_of_scores>13);

Resources