What does getchar do with the second next character from stdin?
Example :
example = getchar();
User input :
ABC
Output :
A
Where does the BC goes?
What does getchar do with the second next character from stdin?
Example :
example = getchar();
User input :
ABC
Output :
A
Where does the BC goes?
It was left in the input buffer, waiting to be read by say, another two getchar() calls.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Stdin is line buffered, if you type ABC and hit enter getchar() will get 1 char from the buffer, the first one happens to be A.
Code:while( (example = getchar()) ) { putchar(example); }
Yes, sam. It will display the error, of course. Because the first character is 's'.
My point is, I entered ' YYYYYYYYYYYYYYYYYYYYYYYYYY ' , yes the first character is Y. But from what I see, I am not entering the character 'Y' only, but a string "YYYYYYYYYYYYYYYYYYYYYYYYYYY".
How do I fix that problem?
You need to define what is correct input and what is wrong input, i.e., std10093's question about your goal.Originally Posted by xeon321
Why is that a problem?Originally Posted by xeon321
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
In order the loop is infinite.Any nuber different that zero gives as a true statement,so the condition of while is satisfied,so our programm is going to enter the loop infinite times.
For example
is equivalent to (the message will be dispalyedCode:int a=5; if(a==5) { printf("entered the loop\n"); }
For example
Code:if(1) { printf("entered the loop\n"); }
It is a problem because I am seeing that I am not entering only Y but many Y, which may be Y23123u12839u1238u123uu2. A wrong answer.
Hmm, define what is correct input & wrong input?
while( example != 'Y'&& example != 'y')
I thought this line means , except 'Y' & 'y', the rest of the characters are not accepted?
Last edited by xeon321; 06-28-2012 at 11:00 AM.
Yes, but "the rest" refers to the range of characters that are not accepted. It has nothing to do with the characters that you have yet to read. If you want to discard them, then you need to read and ignore them until you read a new line (i.e., use a loop).Originally Posted by xeon321
a==5 evaluates to 1, and 1 is obviously not 0.Originally Posted by xeon321
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
The code that you wrote is going to check only the first letter of the word.If the 1st letter is different than Y or y ,then user inputs another(or the same word).Your code is going to ckeck again only the first letter of the word.
a is equal to 5
so a==5 is a true statement.True statements are equivalent as 1(boolean algebra) and false statements as 0.
a==6 is a false statement,it is equivalent to zero
Users are free to input anything(most times by mistake they give false inputs ) .Of course you can,by writing suitable code!