This question already has answers here:
Scanf skips every other while loop in C
(10 answers)
Closed 6 years ago.
#include<stdio.h>
int main()
{
int n,i;
scanf("%d",&n);
char vote[n];
for(i=0;i<n;i++)
{
scanf("%c",&vote[i]);
}
for(i=0;i<n;i++)
{
printf("%c",vote[i]);
}
return 0;
}
It doesn't get second value, after getting first value it prints the first value .
If I give 3 for n it have to get three char values and it have to print that three char values , but code does not work properly for that.
Buffer problem in scanf. Adding a space in the scanf because %c does not skip white space and terminate.
scanf(" %c",&vote[i]); instead of scanf("%c",&vote[i]);
Related
This question already has answers here:
C scanf() problem
(2 answers)
Closed 2 months ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Original close reason(s) were not resolved
The integer and character code work seperately, but don't work together in the same code.
#include<stdio.h>
int main() {
int base;
char num1,num2,num3,num4;
scanf("%i",&base);
printf("%i\n",base);
scanf("%c",&num1);
scanf("%c",&num2);
scanf("%c",&num3);
scanf("%c",&num4);
printf("%c",num1);
return 0;
}
Input:
8
m n o p
Expected output:
8
m
Real output:
8
My understanding is that when you are reading in the characters, it is taking whitespace instead of the actual number. To fix this, you can add a whitespace character in front of the %c like so:
#include<stdio.h>
int main() {
int base;
char num1,num2,num3,num4;
scanf("%i",&base);
printf("%i\n",base);
scanf(" %c",&num1);
scanf(" %c",&num2);
scanf(" %c",&num3);
scanf(" %c",&num4);
printf("%c",num1);
return 0;
}
Hope this helps!
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 answers here:
C - trying to read a single char from stdin (and failing) w/ scanf / getchar
(4 answers)
C: Multiple scanf's, when I enter in a value for one scanf it skips the second scanf [duplicate]
(7 answers)
Closed 5 years ago.
trying to learn C i got stuck with this: I'm trying to write a function squared which should print on screen a filled square with size and filling character choosen by the user. So i wrote this:
#include <stdio.h>
int squared (int side, char fillCharacter);
int main(){
int side;
char fillCharacter;
printf("Enter the side ");
scanf("%d", &side);
printf("Enter the character ");
scanf("%c", &fillCharacter);
printf("%d%c",squared(side, fillCharacter));
}
int squared (int side, char fillCharacter){
for (int row=1 ; row <= side; row++){
for (int i =1; i <= side; i++){
printf("%c", fillCharacter);
}
puts("");
}
}
But sadly the output of this doesn't even scan for the character and just print a blank space
Enter the side 2
Enter the character
I tried change things but i made it worse so if someone could give me a hint i really would appreciate it. Thanks.
This is linked to this problem. This answer provides a fix but you could just fix it by reversing the calls to scanf: first the character and then the number.
This question already has answers here:
scanf won't ask for input the second time [duplicate]
(5 answers)
Packing two characters to an integer
(2 answers)
Closed 6 years ago.
Here is the code
//WAP in Co convert a binary or octal to a decimal number depending on user choice.
#include <stdio.h>
#include <math.h>
int conversion(int number,int base);
int main(void){
int number,base,decimal;
char choice;
printf("Enter the number:");
scanf("%d",&number);
printf("Enter 'b' for binary or 'o' for octal:");
scanf("%c",&choice); //Problem occuring here
if(choice=='b')
base=2;
base=8;
decimal=conversion(number,base);
printf("Decimal number:%d",decimal);
return 0;
}
int conversion(int number,int base){
int reminder;
int decimal=1;
int i=0;
while(number>=0){
reminder=number%base;
decimal=(decimal*reminder)*pow(base,i);
number/=10;
i++;
}
return decimal;
}
You have two scanf() and after the one some character is left out to read. you can use getchar() before the next scaf() it should take the next input
printf("Enter 'b' for binary or 'o' for octal:");
getchar();
scanf("%c",&choice);
Inside the conversion function you have while loop with a condition causes it go in infinite loop.Dividing a positive number by 10 minimum you can get is 0. use the below code:
while(number>0)
Also, if condition you have after the second input is useless. since you assign 8 to base anyway.
This question already has answers here:
My program skip getting input data? [duplicate]
(7 answers)
Closed 8 years ago.
I am having problem scanning char variable , my code is
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int a;
float b;
char c;
printf("Enter value for int variable \n");
scanf("%d",&a);
printf("Enter value for float variable \n");
scanf("%f",&b);
printf("Enter value for char variable \n");
scanf("%c",&c); //scanning is automatically skipped !!!
getch();
}
Please tell me , why is this happening and what can i do to solve it !
because of the stored enter key press [considered as character input]. use one getch(); before the third scanf().
alternatively, use (scanf(" %c",&c);) [mind the space before %c] which will get rid of any number of whitespace [buffered] character present before actual input.