Runtime Error Of Scanf(); [duplicate] - c

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.

Related

Code not asking for second %c input and just prints [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 10 months ago.
My code isn't asking for the second %c input and just printing the result. of the first and printing the next question after.
This is the assignment: Students can take Programming module if they are from SOE or they have passed Math. Otherwise, they cannot take the module.
Write a program to allow students to check their eligibility. Your program must have only one if-else statement and must use the OR logical operator in the condition.
Your program must be able to produce the sample outputs given below. Note that the values underlined are the input data.
this is the code i wrote:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char soe, math;
printf("Are you from SOE [y/n]? ");
scanf("%c", &soe);
printf("Did you pass Math [y/n]? ");
scanf("%c", &math);
if(soe == 'y' || math == 'y'){
printf("OK, you can take Programming");
}
else{
printf("Sorry, you cannot take Programming");
}
}
Are you from SOE [y/n]? y
Did you pass Math [y/n]? OK, you can take Programming
it immediately prints the 2nd printf after the 1st input and doesnt ask for a second input
You have to use getchar() between two scanf function calls, unless they are using the %d conversion format specifier.
(This function cleans the buffer by consuming the ENTER key that you pressed after the first scanf. If you don't do it, the ENTER key still exists and the next scanf will take it as an answer.)
This way :
printf("Are you from SOE [y/n]? ");
scanf("%c", &soe);
getchar();
printf("Did you pass Math [y/n]? ");
scanf("%c", &math);

Is it possible to use scanf() and getchar() in the same program to get input? [duplicate]

This question already has answers here:
The program doesn't stop on scanf("%c", &ch) line, why? [duplicate]
(2 answers)
Closed 1 year ago.
I'm struggling on a question proving scanf() and getchar() can both retrieve a character from the input.
However, when I try to put them inside the same program, only the first function is running properly. The latter is discarded completely.
#include <stdio.h>
char letter;
int main()
{
printf("I'm waiting for a character: ");
letter = getchar();
printf("\nNo, %c is not the character I want.\nTry again.\n\n",letter);
printf("I'm waiting for a different character: ");
scanf("%c",&letter);
printf("Yes, %c is the one I'm thinking of!\n",letter);
return(0);
}
output
I have tried switching the places of those two functions but it is of no use.
Can someone help me find the issue and provide a way to fix this? The only requirement is that the program takes input twice, once by the getchar() function and once via scanf()
The second read attempt just reads whitespace (the end of line character, since you pressed enter after the first letter). Simply replace it with this:
scanf(" %c", &letter);
The space before % will tell scanf to read the next non-whitespace character.

Printf and scanf not working properly? C programming [duplicate]

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().

C: Writing a loop to print the alphabet between two characters [duplicate]

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.

Interleaving of putchar() and printf() functions [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 3 years ago.
It is a statement given in K&R that printf() and putchar() can be interleaved. If it true then why is the following code not giving the required output:-
#include"stdio.h"
void main()
{
char c,d;
printf("Enter the first character\n");
scanf("%c",&c);
printf("%c\n",c);
printf("Enter the second character\n");
d=getchar();
putchar(d);
printf("\n");
}
Whenever I am executing this program, the output is as follows:-
Enter the first character
a
a
Enter the second character
This is the output. This is also happening if I replace printf() by putchar() and scanf() by getchar(). Why is this happpening?
The first scanf leaves in the input buffer the \n resulting from the Return press, so your second getchar() will acquire this \n instead of acquiring another character from the user.
If you want to skip that newline character, you can either instruct the scanf to "eat" it:
scanf("%c\n",&c);
or "eat it" directly with a call to getchar():
scanf("%c",&c);
getchar();
(notice that these are not exactly equivalent, since the second snippet will eat whatever character happens to be in the buffer, while the first one will remove it only if it's a \n)
You can correct your code like this:
#include <stdio.h>
int main() {
char c, d;
printf("Enter the first character\n");
scanf("%c\n", &c); // Ask scanf to read newline and skip
printf("%c\n", c);
printf("Enter the second character\n");
d = getchar();
putchar(d);
printf("\n");
return 0;
}
You are getting two a's because you type one in which is echoed to the console and then you print it out.
flush the stdin before using getchar()..
In turbo, use fflush()..
In gcc, use __fpurge(stdin)..(this is available in <stdio_ext.h> header)..
Flushing the standard input before scanning anything will solve your issue..

Resources