944,025 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1629
  • C RSS
Jan 27th, 2007
0

can't seem to make a yes/no question

Expand Post »
The code below works for the first question (first half of the code), the program then proceeds to the second questin, but it never stops to await the answer.
I can't seem to figure out what I'm doing wrong?

Thx

  1.  
  2. #include <stdio.h>
  3. using namespace std;
  4. char answer;
  5. int main(int argc, char *argv[])
  6. {
  7. //ask if there are new patients
  8. printf ("if new patients type 'y'.\n");
  9. scanf ("%c", &answer);
  10. if (answer == 'y' || answer == 'Y')
  11. printf("something\n");
  12. // ask if they want to see the list of patients
  13. printf ("wanna see list: type 'y'.\n");
  14. scanf ("%c", &answer);
  15. if (answer == 'y' || answer == 'Y')
  16. printf("something else\n");
  17.  
  18. return 0;
  19. }
Last edited by flageolet; Jan 27th, 2007 at 2:07 pm. Reason: forgot translating -> same question
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
flageolet is offline Offline
27 posts
since Jan 2007
Jan 27th, 2007
0

Re: can't seem to make a yes/no question

When including iostream I expected to see cout and cin rather than printf() and scanf() which I always thought were defined in cstdio (or stdio.h if you have an old compiler).

I also don't see where you declare the variable antwoord and I doubt it's stuck somewhere in iostream.

You never use the value of answer when user input is stored in it by the first call to scanf().

Antwoord may mean answer in your native language, but they aren't the same in C/C++.

Try including cstdio, removing iostream, and changing all instances of antwoord to answer (or visa versa).
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Jan 27th, 2007
0

Re: can't seem to make a yes/no question

Oeps, you're very right. (I forgot to translate some variable names when posting on this english forum? Don't worry -> in my compiler it's all: antwoord)

I also include <stdio.h> (but i removed as much as possible for posting purposes). BTW: I use Devc++.

will edit post...
Reputation Points: 10
Solved Threads: 0
Light Poster
flageolet is offline Offline
27 posts
since Jan 2007
Jan 27th, 2007
1

Re: can't seem to make a yes/no question

Click to Expand / Collapse  Quote originally posted by flageolet ...
The code below works for the first question (first half of the code), the program then proceeds to the second questin, but it never stops to await the answer.
Even without looking at the code, I can guess what the problem is with good accuracy. It's probably the single most common problem when people who aren't used to stream input in C/C++ try to read single characters.

Here's how it works. A stream in C/C++ really isn't much more than an array of characters. When you read a character, you increment an index. The basics of this can be faked with a simple program in C.
  1. #include <stdio.h>
  2.  
  3.  
  4. static const char stream[BUFSIZ] = "Default initialization";
  5. static size_t loc = 0;
  6.  
  7.  
  8. // Single character input
  9. int cget( void );
  10.  
  11.  
  12. int main( void )
  13. {
  14. int ch;
  15.  
  16. while ( ( ch = cget() ) != EOF )
  17. putchar( ch );
  18. putchar( '\n' );
  19.  
  20. return 0;
  21. }
  22.  
  23.  
  24. int cget( void )
  25. {
  26. if ( stream[loc] == '\0' )
  27. return EOF;
  28. return stream[loc++];
  29. }
Here's where it gets interesting and where the problem comes from. Every character you type when filling the stream is saved in the stream. If you type "A" and then hit enter to send it to the program, the enter is saved as part of the stream too. In my little example program above, it would look like this.
  1. #include <stdio.h>
  2.  
  3.  
  4. static const char stream[BUFSIZ] = "A\n";
  5. static size_t loc = 0;
  6.  
  7.  
  8. // Single character input
  9. int cget( void );
  10.  
  11.  
  12. int main( void )
  13. {
  14. int ch;
  15.  
  16. while ( ( ch = cget() ) != EOF )
  17. putchar( ch );
  18.  
  19. return 0;
  20. }
  21.  
  22.  
  23. int cget( void )
  24. {
  25. if ( stream[loc] == '\0' )
  26. return EOF;
  27. return stream[loc++];
  28. }
Now, if you change the logic of the code to read two characters like your program tries to do, you get this.
  1. #include <stdio.h>
  2.  
  3.  
  4. static const char stream[BUFSIZ] = "y\ny\n";
  5. static size_t loc = 0;
  6.  
  7.  
  8. // Single character input
  9. int cget( void );
  10.  
  11.  
  12. int main( void )
  13. {
  14. int answer;
  15.  
  16. //ask if there are new patients
  17. printf ("if new patients type 'y'.\n");
  18. answer = cget();
  19.  
  20. if (answer == 'y' || answer == 'Y')
  21. printf("something\n");
  22.  
  23. // ask if they want to see the list of patients
  24. printf ("wanna see list: type 'y'.\n");
  25. answer = cget();
  26.  
  27. if (answer == 'y' || answer == 'Y')
  28. printf("something else\n");
  29.  
  30. return 0;
  31. }
  32.  
  33.  
  34. int cget( void )
  35. {
  36. if ( stream[loc] == '\0' )
  37. return EOF;
  38. return stream[loc++];
  39. }
The second test fails because you're looking at '\n' instead of 'y'. The enter stays in the stream and becomes the next character read. That's the problem, and here's why it happens.

When the stream is empty and you try to read from it, all execution stops and waits for you type something. This is a refill operation called a blocking read. If the stream isn't empty, whatever is in it will be used without blocking. Here's what happens when you run your program.

The first call to scanf() causes a blocking read and you type 'y'. But you also have to hit enter or nothing will happen because input in C/C++ waits for a newline character before returning. The stream at that point contains "y\n". scanf() then extracts the 'y' and uses it in the answer variable.

Now, the second call to scanf() doesn't cause a blocking read because there's still a '\n' in the stream. The '\n' is extracted and saved in the answer variable and used in the test right away. In the end it looks like scanf() is skipped when it's doing exactly what you told it to, but not what you really wanted.

All of this is why I hate C/C++ stream I/O. It's just not intuitive.

The way to fix the problem is to read complete lines from the stream including the '\n' and then parse the string with your program's logic. Another fix is to periodically empty the stream by reading and throwing away all of the characters that are there up to and including the next '\n'.
  1. void flush_stdin( void )
  2. {
  3. int ch;
  4.  
  5. do
  6. ch = getchar();
  7. while ( ch != '\n' );
  8. }
Last edited by Ravalon; Jan 27th, 2007 at 4:26 pm.
Reputation Points: 84
Solved Threads: 15
Posting Whiz in Training
Ravalon is offline Offline
209 posts
since Dec 2006
Jan 28th, 2007
0

Re: can't seem to make a yes/no question

Yeah, what Ravalon said. You can check out this series for deeper understanding of why we say "stop using scanf()"

Also, there is absolutely nothing C++ about what you posted so why the using namespace std?
Last edited by WaltP; Jan 28th, 2007 at 5:10 am. Reason: Oops -- thanks Dave....
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Jan 28th, 2007
0

Re: can't seem to make a yes/no question

Click to Expand / Collapse  Quote originally posted by WaltP ...
Yeah, what Ravalon said. You can check out this series for deeper understanding of why we say "stop using sscanf()"
sscanf?
Last edited by Dave Sinkula; Jan 28th, 2007 at 4:29 am.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jan 28th, 2007
0

Re: can't seem to make a yes/no question

I use some C++ (not much), but not in the posted code.

Thx for the answers.
Reputation Points: 10
Solved Threads: 0
Light Poster
flageolet is offline Offline
27 posts
since Jan 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: read file
Next Thread in C Forum Timeline: Archery Game ( Computer Graphics Project )





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC