By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,192 Members | 1,276 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,192 IT Pros & Developers. It's quick & easy.

why getchar is not executed??

P: n/a
yky
in this programme,if i input a number 100,the program will be over ,and i
donot have the chance to input a char to z;
# include<stdio.h>
main()
{
int x;
char z;
do
{printf("please input a number between 1 and 100\n");
scanf("%d",&x);
if(x==58)
{printf("exellent\n");
exit();}
else if(x-58>-5&&x-58<0)
printf("it is a little smaller\n");
else if(x-58<5&&x-58>0)
printf("it is a little bigger\n");
else if(x-58<-5||x-58>5)
printf("it is much away\n");
}while(x<100&&x>1);
puts("press any key");
z=getchar();
putchar(z);
}

Sep 21 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"yky" <ya************@yahoo.com.cnwrites:
in this programme,if i input a number 100,the program will be over ,and i
donot have the chance to input a char to z;
# include<stdio.h>
main()
{
int x;
char z;
do
{printf("please input a number between 1 and 100\n");
scanf("%d",&x);
if(x==58)
{printf("exellent\n");
exit();}
else if(x-58>-5&&x-58<0)
printf("it is a little smaller\n");
else if(x-58<5&&x-58>0)
printf("it is a little bigger\n");
else if(x-58<-5||x-58>5)
printf("it is much away\n");
}while(x<100&&x>1);
puts("press any key");
z=getchar();
putchar(z);
}
The getchar() was executed. If you typed "42" followed by <returnat
the prompt, then scanf() call read "42" and getchar() read the
newline.

The comp.lang.c FAQ is at <http://www.c-faq.com/>. I call your
attention to questions 12.18a, 12.18b, and 12.20. For that matter,
you should probably read all of section 12, then the entire FAQ as you
find the time.

Some other coments:

"main()" is more or less acceptable, but "int main(void)" is
preferred.

Your code would be a lot easier to read if you added some white space.
For example, rather than
}while(x<100&&x>1);
you could write:
} while (x < 100 && x 1);

Proper indentation would also be helpful.

getchar() returns an int, but you assign its result to a char. That
probably doesn't matter much here, since you're about to terminate the
program, but in general you need to store the result in an int so you
can check for EOF.

You call exit() with no arguments. exit() takes a single argument of
type int; this is *not* optional. The portable argument values are 0,
EXIT_FAILURE, and EXIT_SUCCESS; the latter two are macros defined in
<stdlib.h>. <stdlib.halso declares the exit() function; your
failure to add the required "#include <stdlib.h>" at the top of your
program is probably why your compiler didn't complain about the
missing argument.

You should add a "return 0;" at the end of your program.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 21 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.