This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 4 years ago.
I have a function that asks the user for user variable and user number.
void numReplace(char infix[50])
{
char userVar;
int userNum;
printf("Please enter the variable you want to change\n");
scanf("%c", &userVar);
printf("Please enter the replacement value for the variable\n");
scanf("%d", &userNum);
printf("%c %d", userVar, userNum);
int i=0;
char chrr;
infix[50] = '\0';
while((chrr=infix[i++])!='\0')
{
if (chrr == userVar){
chrr = userNum;
}
}
}
when running the program I should be asked the userVar and userNum. However the output is:
Please enter the variable you want to change
Please enter the replacement value for the variable
1
1
It only takes in one variable, I don't see a problem with my codes. Can someone help me?
Try adding a getchar(); after every call to scanf().
It's a workaround to the dangling newline character after you press <enter> when using scanf().
See this FAQ for more info.
This uservar may have received the \n you entered last time. If you have input before this input, please accept the newline with getchar().
Related
This question already has an answer here:
How to read / parse input in C? The FAQ
(1 answer)
Closed 4 years ago.
I am trying to figure out the best way to get an integer and a character from a user
Here is what I have so far:
#include <stdio.h>
int main()
{
int a;
char b;
printf("enter the first number: \n");
scanf("%d", &a);
printf("enter the second char: \n");
scanf("%c", &b);
printf("Number %d",a);
printf("Char %c",b);
return 0;
}
The output is not shown correctly. Is there any problem with this?
Your input and output statements are fine. Just replace printf("Number %d",a); with printf("Number %d\n",a); to better format the output. Also you should change your second scanf statement to scanf(" %c", &b);. This will deal with the newline character entered after the number is inputted.
After you enter the number, you pressed the Enter key. Since the scanf function works on the input stream, when you try to process the next char after reading the number, you are not reading the character you typed, but the '\n' character preceding that. (i.e. because the Enter key you pressed added a '\n' character to your input stream, before you typed your char)
You should change your second call to scanf with the following.
scanf(" %c", &b);
Notice the added space character in the formatting string. That initial space in the formatting string helps skip any whitespace in between.
Additionally, you may want to add \n at the end of the formatting strings of both printf calls you make, to have a better output formatting.
Here you need to take care of hidden character '\n' , by providing the space before the %c in scanf() function , so the "STDIN" buffer will get cleared and scanf will wait for new character in "STDIN" buffer .
modify this statement in your program : scanf("%c",&b); to scanf(" %c",&b);
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 5 years ago.
In following c code scanf is not working.
When program is run, it execute upto the second printf line and after it skips the scanf("%c",&sex); and directly execute next printf().
Why this so happen?
I run this code on different c compilers, but the output is same.
#include<stdio.h>
void main()
{
char mar,sex;
int age,flag=0;
printf("Married [Y/N]:");
scanf("%c",&mar);
printf("Sex [M/F] :");
scanf("%c",&sex); //**This not working**
printf("Age :"); //**execution directly jumped here**
scanf("%d",&age);
if(mar=='y')
flag=1;
else if(sex=='m'&& age>=30)
flag=1;
else if(sex=='f'&& age>=25)
flag=1;
else
{
}
if(flag)
{
printf("Congratulations!!!! You are Egligible..");
else
printf("Sorry... You are not egligible..");
getch();
}
//Output
Married [Y/N]:y
Sex [M/F] :Age :23
Congratulations!!!! You are Egligible..
The problem is with the new line character that you press after you enter values (Y/N) for the previous scanf. The new line character is taken as an input and the program proceeds with the next one. Try to use flushall(); before next read (that is scanf) this will solve your problem. You can also use space before the format specifier to solve this, that will escape the newline character.
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
C skipping one command of a function? [duplicate]
(2 answers)
Closed 6 years ago.
I've been given the pretty simple task of writing a program that will take two characters and then print the letters inbetween them using a for() loop.
Here's my code:
#include <stdio.h>
int main() {
char a, b;
printf("\nEnter the first character: ");
scanf("%c", &a);
printf("\nEnter the second character: ");
scanf("%c", &b);
for(char i = a; i <= b; i++) {
printf("%c ", i);
}
return 0;
}
When I run it, I am prompted to enter the first character correctly but when I press enter it only runs the next printf() and then terminates.
No errors or warnings or anything on compilation. Another similar question I found that was apparently solved does not work for me either.
Thanks in advance.
You have to consume the \n in stdin left by first scanf.
Fastest fix
scanf(" %c", &b);
The space before %c tells to scanf to ignore all whitespaces before to read the char.
If I read your code correctly, by pressing enter, you would enter the second character, which would most probably (depending on the environment) start with a numeric value of 13, which would be smaller than any letter, so the loop's body is executed only once.
This question already has answers here:
C: Multiple scanf's, when I enter in a value for one scanf it skips the second scanf [duplicate]
(7 answers)
Closed 8 years ago.
This is part of a university lab and the TA tells me there is an error but I haven't a clue. When I run it it asks me for the first char but then runs through the program and doesn't ask me at the second scanf.
#include <stdio.h>
int main(void) {
char sen, ben;
printf("Type in a character: ");
scanf("%c", &sen);
printf("The key just accepted is %d", sen);
printf("\nType in another character: ");
scanf("%c", &ben);
printf("The key just accepted is %d", ben);
}
Actually this is C not C++. Save it as file.c.
Try this:
#include <stdio.h>
int main(void) {
char sen, ben;
printf("Type in a character: ");
sen = getchar();
printf("The key just accepted is %d", sen);
printf("\nType in another character: ");
getchar();
ben = getchar();
printf("The key just accepted is %d", ben);
}
Explanation: when you enter the first character and press enter it takes enter's ASCII code as the second.
I suggest not to use scanf. But it works both ways if you put a getchar to "take" the enter.
Adding a space before %c in the second scanf will solve the issue.
This is done because scanf does not consume the \n character after you enter the first character and leaves it in the stdin.As the Enter key(\n) is also a character,it gets consumed by the next scanf call.The space before the %c will discard all blanks like spaces.
When you are scanning a character(%c) using scanf,add a space before %c as it would help reduce confusion and help you. Therefore, in both the scanfs , you can add the space.
When you pressed your key and then hit enter, you typed in two keys. The first was the desired key ,a for example, and the second was the key <enter> typically written as \n. So, your second scanf captures the result \n.
Since printing out the \n character doesn't result in something that is easy to see on the screen, it will appear like your program is just skipping the second scanf and printing out only the fixed parts of the printf without a easily viewable value.
One way to get around this problem is to consume all the key strokes just before the key you want to capture. This is done by accepting more input after the character up until you see a newline character \n. Once you see that character, then you do your next read.
// flush extra input up the to carriage return
char flush = 0;
while (flush != '\n') {
scanf("%c", &flush);
}
// now read my desired input
scanf("%c", &ben);
that's because nobody accepts '\n'. call scanf like this scanf("%c%*c", &sen). %*c means you want to omit one character, which is '\n'.
btw, void main() is allowed. main function is not the real entry point of executable, so it's ok to do that. but it seems not everybody likes it.
This question already has answers here:
abnormal behavior of scanf [duplicate]
(3 answers)
Closed 8 years ago.
Today I encountered with problem that when I use twice scanf which accept character as input then second scanf skipped.
I tried to figure out I came to the conclusion that when we press enter key after first scanf the second scanf is skipped because enter key is take as input in the second scanf.
Can some please explain what is the exact reason with it?
int main()
{
char ch;
int num;
scanf("%d",&num);
scanf("%c",&ch);//This is skipped but its accept input when space as scanf(" %c",&ch)
}
scanf("%c", &ch);
It will read '\n' from the previous scanf since you are inputting a number and pressing enter, if you don't write it like scanf(" %c", &ch);. That way it will ignore '\n' and wait until you enter a valid char.