c - how to use scanf to get input to include space -
i new c have been confused let's have code want use char
char a, b, c; printf("input first character: "); scanf(" %c", &a); printf("input second character: "); scanf(" %c", &b); printf("input thrid character: "); scanf(" %c", &c);
how ever want able read in space well; noticed how read in non-space characters, if want read space c=' '; how scan space in;
now listening suggestion of using getchar() wrote :
#include<stdio.h> int main(void) { char a,b,c; printf("input first char:"); a=getchar(); printf("input second char:"); b=getchar(); printf("input third char:"); c=getchar(); return 0; }
how ever strange happens when compile , run program
the program output
input first char: input second char:input third char:
now never let me input second char jumped straight third request @ end asked enter 2 inputs strange because program asked 3 in code.
now here program wrote added suggested code block
int main(void) { int totalheight=0, floorwidth=0, amountofstories, amountwindowfortop, amountwindowformiddle, amountwindowforbottom, windowheight, middlewindowwidth, topwindowwidth, bottomwindowwidth, minimumheight, minimumwidth; int betweendistancetop, betweendistancemiddle, betweendistancebottom, edgedistancetop, edgedistancebottom, edgedistancemiddle; char topfloorwindowcontent, middlefloorwindowcontent, bottomfloorwindowcontent, windowborder, floorborder; int tempmax, tempvalue, tempsidedistance, tempbetweendistance; printf("please enter how many stories building have: "); scanf("%d",&amountofstories); minimumheight=amountofstories*6+1; while((totalheight<minimumheight)||((totalheight%amountofstories)!=1)) { printf("please enter totalheight (minimum %d): ",minimumheight); scanf("%d",&totalheight); } printf("please enter how many window building have top floor: "); scanf("%d",&amountwindowfortop); printf("please enter how many window building have middle floors: "); scanf("%d",&amountwindowformiddle); printf("please enter how many window building have bottom floor: "); scanf("%d",&amountwindowforbottom); tempmax=amountwindowfortop; if (tempmax<amountwindowformiddle) { tempmax=amountwindowformiddle; } if (tempmax<amountwindowforbottom) { tempmax=amountwindowforbottom; } while(floorwidth<tempmax) { printf("please enter width of building (minimum %d): ",tempmax*4+1); scanf("%d",&floorwidth); } char a, b, c; printf("a:"); a=getchar();getchar(); printf("b:"); b=getchar();getchar(); printf("c:"); c=getchar(); printf("a=%c, b=%c, c=%c", a, b, c); return 0; }
now here funny part if put block of code in big program doesn't work output
please enter how many stories building have: 2 please enter totalheight (minimum 13): 2 please enter totalheight (minimum 13): 2 please enter totalheight (minimum 13): 13 please enter how many window building have top floor: 2 please enter how many window building have middle floors: 2 please enter how many window building have bottom floor: 2 please enter width of building (minimum 9): 9 a: b:* c:a= , b= , c=
as can see b c read in \n instead of space * , c didn't read @ why ?
the problem code this: when read first char (a
), press enter (\n
) insert next char, on stdin
there \n
haven't readed. when try read next character, (b
) program read previous \n
stdin
, not allow read next char. so, when read char getchar()
, press enter on keyboard, need second getchar()
remove \n
. here sample code solve issue:
#include<stdio.h> int main(void) { char a, b, c; printf("a:"); a=getchar();getchar(); printf("b:"); b=getchar();getchar(); printf("c:"); c=getchar(); printf("a=%c, b=%c, c=%c", a, b, c); return 0; }
for edited posted, need put called "the stdin cleaner" before taking value a
,b
,c
:
while(getchar()!='\n');
it reomove characters till \n
. please, take note when programs 1 posted has lot of input keyboard, sometime issue because there chars in stdin
. general answer issue try figure out these chars (mostly there \n
somewhere) , use function 1 mentioned remove can continue reading stdin
.
Comments
Post a Comment