This question already has answers here:
Yes/No loop in C
(3 answers)
Closed 8 years ago.
I am trying to execute a small program using do-while.
#include<stdio.h>
#include<conio.h>
void main()
{
char another;
int num;
do
{
printf("Enter a number");
scanf("%d",&num);
printf("Square of %d id %d",num,num*num);
printf("Want to another another number y/n");
scanf("%c",&another);
}while(another=='y');
}
Now when I try to execute the program, it runs fine. I input a number and it displays its square. And then I see Want to enter another number y/n. But as soon as I press any key (y or n), the program exits itself before I can press enter to provide the input. I tried it many times but no success.
But the program runs fine if I ask the user to input either 1 or 2(in place of y/n). In that case it takes an integer input and can check the while block. If another == 1, the program runs again.
My problem is that why can't I check for a character in the while condition.
The reason it doesn't work is that after scanf gets num, the new line is still in the buffer, so it will be processed by the next scanf with %c format specifier. A direct way of fixing it is to use:
scanf(" %c", &another);
// ^space
Note that your original scanf("%c:,&another); won't compile, but I assume that's a typo. And always use int main, or it's undefined behavior.
Related
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);
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 3 years ago.
My program should accept 2 int value as the number and the position at which it should be added. After that it should ask wheather you want to insert more Y/N? But my program dosen't take the input of char instead takes a garbage value and keeps on asking for numbers only.
I have tried using seperate scanf for each input.
#include<stdio.h>
#include<conio.h>
void main()
{
int x,n;
char opt;
clrscr();
do{
printf("\nEnter the no. to be added and the position:");
scanf("%d%d",&x,&n);
printf("Do you want to insert more elements Y/N?");
scanf("%c",&opt);
printf("opt=%c x=%d n=%d",opt,x,n);
}while(opt!='N'&&opt!='n');
}
My o/p should be the value of each variables instead I get a garbage value in opt and the loop continues as it isn't 'n'.
The new line character is being read into your variables.
Change this line:
scanf("%c",&opt);
To this:
scanf(" %c",&opt);
The space consumes the new line character. This is only necessary for your " %c" format specifier. The "%d" does not require the space.
This question already has an answer here:
Why inside a loop, scanf() with %d does not wait for the user input in case it received an invalid input previously?
(1 answer)
Closed 5 years ago.
So today I am trying to find a way to check if this input is a number. I cant find a way to make this code work so far. If I enter a number, the while loops ends, which is my intention. But, if I enter anything else, the code SHOULD print the printf once and then reprompt me for input (via the scanf statement back at the top of loop). But it instead prints the printf statement infinitely. How can I fix this?
float num1;
while (scanf("%f",&num1)==0)
{
printf("Invalid input. Please enter a number: ");
}
If scanf fails to preform the requested conversion, it will leave the input stream unchanged. So while your check is correct, you need to clean the input stream of the erroneous input before re-attempting to read a number again.
You can do it with scanf itself and and the input suppression modifier:
float num1;
while (scanf("%f",&num1)==0)
{
printf("Invalid input. Please enter a number: ");
scanf("%*s");
}
%*s will instruct scanf to parse the input as though it's attempting to convert a string of characters (removing characters in the process from the stream), but thanks to the asterisk, it won't attempt to write it anywhere.
This question already has answers here:
Simple C scanf does not work? [duplicate]
(5 answers)
Closed 7 years ago.
I have a question, We were asked by our teacher to write a rock paper scissors program using the if else statement
my problem is
if i code it like this
char a, b;
clrscr();
printf("\n Enter player 1 value");
scanf("%c", &a);
printf("\n Enter Player 2 value:);
scanf("%c", &b);
my problem is when i code it like this after entering the 1st value it ignores the second one and just goes on the if statements
and i found a solution which is putting space on %c on the second scanf which looks like this (found a similar program)
scanf(" %c", &b);
and it works but now i don't know why ??
can anyone explain to me why it was being ignored and why putting a space solves that problem ?? it will gladly help
thanks in advance
by adding a space you exclude the whitespaces created by the previous scanf.
scanf() stops as soon it finds a whitespace so if the string start with a whitespace you get nothing.
use scanf("%[^\n]", &variable) to get everything (space included) or even better
scanf("%30[^\n]", &variable) to get everything with a size limit on the input (in this case 30).
There is also another function that lets you read from a stream:
fgets(&variable, sizeof variable, stdin); check the doc out here(http://www.cplusplus.com/reference/cstdio/fgets/)
This question already has answers here:
Scanf skips every other while loop in C
(10 answers)
Closed 8 years ago.
Whenever the code is executed, the contents inside the for loop are not executing for the first time, i.e., when i=0. But the loop executes after i=0, i.e., for i=1,2,3,..n-1. Can anyone explain what's wrong here?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
char string[30][100];
int n,i;
scanf("%d",&n);
for(i=0;i<n;i++){
gets(string[i]);
printf("%s\n",string[i]);
}
getch();
return (EXIT_SUCCESS);
}
you can either try leaving a space after the scanf so the system pauses there instead of automatic increment the reason for this is after you press enter for the value of n , theres no system pause and one of the i values from loop is automatically considered as entered by the system :
scanf("%d ", &n);
or make the loop be from 0 till equal to n which will still skip a number but at the same time add 1 just to keep the loop equal to the input "n" u give
for(i=0;i<=n;i++)
bear in mind that the first option is much wiser to use since you might need to use the "n" in some other part of the program as well.
for(i=0;i<n;i++)
{
fflush(stdin); //Clears the buffer.insert this here.
gets(string[i]);
printf("%s\n",string[i]);
}
After execution of scanf("%d",&n); you are leaving '\n'(when you press Enter key at the end) character in buffer.Which should be cleared before execution of any other input operation.fflush(stdin) will clear the keyboard buffer. There are many other ways to do it.But using fflush(stdin) is easy for beginners.