char c;
int i;
for(i=0;i<5;i++)
{
printf("Enter a character : ");`
scanf("%c",&c);
}
getch();
The above code doesn't work properly.It is getting input for only 3 times. I am not able to find solution for it.Please help with it.Thanks in advance !!
This is because of the new-line character \n left behind by the previous scanf is read by the scanf in the next iteration. Place a space before the %c specifier to consume the \n
scanf(" %c",&c);
^Notice the space
char c is a single character variable.
In your code, your are reading time and time again characters and storing it into c, effectively overwriting it each time. If you would like to input multiple characters, consider using a character array likewise:
#include <stdio.h>
int
main (void)
{
char c[5];
int counter;
for (counter = 0; counter < 5; counter++)
{
printf ("Enter a character: ");
scanf ("%c\n", &c[i]);
}
printf ("The string you input is %s\n", c);
return 0;
}
Suppose you enter a, b and c on the screen then it is actually taking 5 inputs
Input 1: a
Input 2: \n
Input 3: b
Input 4: \n
Input 5: \n
The newline is also taken as input
Related
#include <stdio.h>
int main()
{
char gh, hj;
printf("Enter the first letter of your name \n");
gh=getchar();
printf("Enter the last letter of your name\n");
hj=getchar();
getchar();
printf("The first and last leter of your name is %c%c", gh, hj);
return 0;
}
When I run this code the second %c doesn't get printed, unlike the first one. Why is this happening?
Since the second getchar peruses the newline character after the character you entered.
I have written a program to take input of two numbers and either add or subtract the numbers depending the operation specified.
Here is my code :
#include <stdio.h>
#include <stdlib.h>
int main()
{
float i, j, k;
char a;
printf("This is a program to add or subs two number.\n");
printf("Enter the first number : ");
scanf("%f", &i);
printf("Enter the second number : ");
scanf("%f", &j);
printf("Give your choice(+ or -): ");
scanf("%c", &a);
switch(a){
case '+' :
k = i + j;
printf("Sum = %f\n", k);
break;
case '-' :
k = i - j;
printf("Difference = %f\n", k);
break;
default:
printf("Cannot do this operation\n");
}
return 0;
}
This program takes input for the two numbers but skips input for operation and runs the default case. Please help!
(I am using gcc compiler).
The %c conversion specifier won't automatically skip any leading whitespace, so if there's a stray newline in the input stream (from a previous entry, for example) the scanf call will consume it immediately.
One way around the problem is to put a blank space before the conversion specifier in the format string:
scanf(" %c", &a);
The blank in the format string tells scanf to skip leading whitespace, and the first non-whitespace character will be read with the %c conversion specifier.
-Aditya
My program below isn't taking input using getchar(). Instead, it ends after printing, "want to continue??(press y for yes and press n to reenter)". It doesn't take input after typing n or N.
void main(){
int i,arr[]={55,10,23,11,35,8,9,20},n;
char a;
printf("Given array is:\n");
for(i=0;i<8;i++)
printf("%d ",arr[i]);
do{
printf("\nEnter position where you want to insert element:");
scanf("%d",&n);
printf("You entered position %d \n",n);
printf("want to continue ??(press y for yes and press n to reenter)");
a=getchar();
} while(a=='n' || a=='N');
}
according to reference:getchar()
Returns the next character from the standard input (stdin).
Note that the standard input is a buffered I/O, so getchar() reads the first char in input buffer
when in scanf("%d",&n); you will input an int and a newline, then getchar() just read the newline
use scanf("%d\n", &n); instead, or as #BLUEPIXY said, add a getchar(); before a = getchar();
more detailed explanations about input buffer: Flush the input buffer
You can add this before a = getchar();
getchar();
So this 'eats' the new line character from the buffer. Because after you entered a number e.g. 3 into scanf("%d",&n); there is still a '\n' in the buffer and if you only have this: a = getchar();, \n gets read in
I am new to C programming. I wrote a simple switch case but it is not executing as expected . Can some one tell me what is wrong here??
#include <stdio.h>
int main() {
int i;
char yes;
bool flag = true;
while(flag) {
printf("Enter the value");
scanf("%d",&i);
switch(i) {
case 1:
printf("Hi");
break;
case 2:
printf("Hello");
break;
}
printf("Enter Y or N to continue");
scanf("%c",&yes);
if (yes == 'N') {
flag = false;
}
}
return 0;
}
The result I am expecting is:
Enter the Value
1
Hi
Enter Y or N to continue
Y
Enter the Value
2
Hello
Enter Y or N to continue
N
But the result I am getting is :
Enter the value 1
HiEnter Y or N to continueEnter the value N
HiEnter Y or N to continue
When you hit Enter after typing in the first number, scanf read all numeric characters from the input stream except the newline character produced by that Enter hit. The newline character is not a part of the number. It is left in the input stream, unread, waiting for someone else to read it.
The next scanf("%c",&yes); discovered that pending newline charcter and it read it without waiting. The %c format specifier does not skip whitespace in the input, it just reads the first character it sees.
Replace your scanf with
scanf(" %c",&yes);
to make it skip whitespace. That way it will ignore that pending newline and actually wait for you to enter something.
In all your printf you need to add \n at the end.
For example on usage, see here: printf
This should work for you:
(You forgot all '\n' in your printf statements and add a space in your char scanf statements)
#include <stdio.h>
int main() {
int i;
char yes;
int flag = 1;
while(flag) {
printf("Enter the value\n");
scanf("%d",&i);
switch(i){
case 1:
printf("Hi\n");
break;
case 2:
printf("Hello\n");
break;
}
printf("Enter Y or N to continue\n");
scanf(" %c", &yes);
if (yes == 'N')
flag = 0;
}
return 0;
}
Output:
Enter the Value
1
Hi
Enter Y or N to continue
Y
Enter the Value
2
Hello
Enter Y or N to continue
N
It's not a problem with the switch statement. It's a problem with your output - there aren't line breaks ('\n'). For example, instead of printf("Hi"); you might want to have printf("Hi\n");, which adds a line space at the end.
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 4 years ago.
I want to know the reason why this code not run properly
#include<stdio.h>
main()
{
char a;
int n;
do
{
printf("enter the number");
scanf("%d",&n);
printf("the squre is %d",n*n);
printf("want any more so Y for yes N for no");
scanf("%c%[^\n]",&a);
}while(a=='Y');
}
Reasons are
1. scanf("%c%[^\n]",&a); needs two parameters. Remove %[^\n].
2. \n character left behind by the previous scanf on pressing Enter key. Next scanf will read this \n character in the buffer. You need to consume this \n. Use a space before %c specifier to eat up this \n.
Try this:
scanf(" %c",&a);
↑ A space before %c specifier
A space before %c specifier is able to eat ant number of newline characters.
Your code after modification:
#include<stdio.h>
int main(void)
{
char a;
int n;
do
{
printf("enter the number\n");
scanf("%d",&n);
printf("the squre is %d\n",n*n);
printf("want any more so Y for yes N for no\n");
scanf(" %c",&a);
}while(a=='Y');
return 0;
}
Here is a solution to your problem.
#include<stdio.h>
main()
{
char a;
int n;
do
{
printf("enter the number");
scanf("%d",&n);
a = getchar(); // <-- Here is a change.
printf("the squre is %d",n*n);
printf("want any more so Y for yes N for no");
while(a == '\n') a = getchar();
}while(a=='Y');
}
What actually is making you problem is this line.
scanf("%d",&n);
Suppose, you entered 10 then pressed 'Enter', now scanf() takes 10 and leaves a newline behind in the buffer. It is making problem. By getchar() you are eating up that new line each time after taking a input with scanf. Yes there are other solutions too with scanf() tricks but it seems simpler to me. So I shared it.
just use scanf(" %c",&a); and could be done.
Wrong code: scanf("%c%[^\n]",&a);
Right code: scanf(" %c%*[^\n]",&a);