Why does ScanF take two inputs from string [closed] - c

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 3 years ago.
Improve this question
The first ScanF takes both inputs when I type a character and an integer is expected.
see image below:
As you can see when I type "55ape" for ScanNum_A it sets ScanNum_B = 0.
Why does this happen?

It happens because
ape is still left in the input stream and your second scanf tries to read it but fails as it was expecting integer but found chars.
You can clear the input buffer after your first scanf as below.
//first scanf
int c;
while ((c = getchar()) != '\n' && c != EOF) { }
//second scanf

Related

Looping input request from a char array in C [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 2 years ago.
Improve this question
When running the program and entering the text, it will ask me again and again to insert the input, until I give an input of one character only.
NOTE: it isn't in any sort of cycle loop, I don't know what the problem is. (I'm using cs50 library for the get char).
char nameless[] = { get_char("Insert the text here: ")};
char nameless[] = {get_char("Insert the text here: ")};
If you look up get_char(), it clearly states
Prompts user for a line of text from standard input and returns the equivalent char; if text does not represent a single char, user is reprompted.
You need to enter only one character. Not a line of characters or 'text' as you call it.
Enter char: a
That's it.
Here's a working code example:
#include <cs50.h>
int main(void)
{
// attempt to read character from stdin
char c = get_char("Enter char: ");
// ensure character was read successfully
if (c == CHAR_MAX)
{
return 1;
}
char next = get_char("You just entered %c. Enter another char: ", c);
if (next == CHAR_MAX)
{
return 1;
}
printf("The last char you entered was %c\n", next);
}
You can look it up at CS50 Library for C

C : While taking user input in while loop, it goes as infinite loop - user input [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 5 years ago.
Improve this question
I am trying to take user input in my program but once user inputs 'y' to continue the loop one more time, while loop goes crazy and ends up in an infinite loop or at times, exits the loop without taking scanf input.
char in = 'y';
while(in == 'y'){
// Code
printf("do you wanna continue?");
scanf("%c",&in); //enter y to continue
}
To scan into char variable you have to use %c
like this
scanf("%c", &in);

How to use fflush in c on OS/X [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 6 years ago.
Improve this question
program source codeHow should I use fflush in C on OS/X? When I use it, it does not clear my buffer and terminates the program straight away.
Calling fflush(stdin); invokes undefined behavior. You should not use this to flush characters from the standard input buffer. Instead, you can read the characters upto the next linefeed and ignore them:
int c;
while ((c = getchar()) != EOF && c != '\n')
continue;
You can also use scanf() for this, but it is tricky:
scanf("%*^[\n]"); // read and discard any characters different from \n
scanf("%*c"); // read and discard the next char, which, if present, is a \n
Note that you cannot combine the 2 calls above because it would fail to read a linefeed non preceded by any other characters, as the first format would fail.

Why b.r is not visible while printing? [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 6 years ago.
Improve this question
I have this structure after scanning and printing p and q are printed while r is not printed can you please let me know why?
struct book
{
int p;
float q;
char r;
};
int main()
{
struct book b;
scanf("%d%f",&b.p,&b.q);
scanf("%c",&b.r);
printf("%d......%f.....%c",b.p,b.q,b.r);
return 0;
}
Problem :
that's because b.r takes in the \n character entered at the end of the previous scanf() statement
scanf("%d%f",&b.p,&b.q);
Solution :
Avoid it by giving a space before %c in the scanf()
scanf(" %c",&b.r);
Why give a space ?
This would consume if there are any whitespaces (' ' or '\n' or '\0') present in the input stream
Suggestion :
next time when you don't get any output when you print, try printing it's ascii value by casting it to int, that way you'd know what value the variable is taking and see it's corresponding character in ascii table.
printf("%d",(int) b.r);
for example, without making any changes to your code except this to your printf statement :
printf("%d......%f.....%d",b.p,b.q,(int)b.r);
you'd get
input :
2
2
output :
2.....2.....10
why the 10?
because it's the ascii value of \n or the newline character

C reading input in while loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
In c what does this do after gets
int c;
while ((c = getchar()) != EOF && c != '\n');
I saw that many of you are telling me its while loop and all, why so much complication to it? Why cant we just use this code which I have given below?
gets(name);
if(name == '\n'|| name == EOF)
gets(name);`
First of, the gets function is not really secure and you might want to use fgets instead.
Anyway, your piece of code is used to clear the buffer. When you read from the user input, all the things that the user will type is going to be stored in a buffer, and then the program will read from it. That why sometimes you need to clear the buffer so you don't read other things that you didn't want.
Well, this piece of code
int c;
while ((c = getchar()) != EOF && c != '\n');
is used to clear the buffer, as mentioned in #napnac's answer. It is mainly used instead of fflush (stdin);, which is UB. But note that this is successful only if the input buffer happens to contain data ending in a newline.
Otherwise, you can use fflush (stdin);, which is not recommended. You can also use the flushinp function provided by the curses library. It throws away any typeahead that has been typed by the user and has not yet been read by the program

Resources