details in the reply..
details in the reply..
Last edited by red463; 04-29-2010 at 05:09 PM. Reason: nonconformity
Hi all!
I cannot figure out why the output prints the printf line twice.. in this program.. thanks..
here is the code with the output:
Code:#include<stdio.h> main() { char ch; while (ch!='0') { printf("enter a char other than 0\n"); ch=getchar(); } }
enter a char other than 0
d
enter a char other than 0
enter a char other than 0
What happens after you press the d key?
Quzah.
Hope is the first step on the road to disappointment.
well, it prints "enter a char other than 0" twice...
what is the solution to this problem?
ok teacher, what is the right way to deal with this then?
It really depends on what the specs of the program are. If the specs are something like "print a prompt and read a character; if the character is not '0', then repeat" then we will say it for the fourth time in this thread: there is no error in your code and therefore there is nothing to fix. If the specs are something other than that, then you need to tell us what they are.
allright, my bad, the spec follows-
write a program, which allows users to enter characters until the input is '0'.
Without using system specific functions (kbdhit, getch), you can't do what your program specs, as written in your last post, request. There's nothing keeping you from entering: "1230321" and hitting enter. So unless you read keystrokes - not characters - you can't do what you want.
Quzah.
Hope is the first step on the road to disappointment.
well, thanks for all the help guys...
Your program fits the requirement as given by you, if you change the wording slightly. It will allow you to enter characters, and it will stop the loop when it hits '0'.
All the rest has to do with buffering, and the way input is actually typed versus how it is being handled.
Quzah.
Hope is the first step on the road to disappointment.