why getchar is not executed??

Y

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);
}

 
K

Keith Thompson

yky said:
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 <return> at
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.h> also 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top