can't seem to make a yes/no question

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2007
Posts: 27
Reputation: flageolet is an unknown quantity at this point 
Solved Threads: 0
flageolet's Avatar
flageolet flageolet is offline Offline
Light Poster

can't seem to make a yes/no question

 
0
  #1
Jan 27th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

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

 
0
  #2
Jan 27th, 2007
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).
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 27
Reputation: flageolet is an unknown quantity at this point 
Solved Threads: 0
flageolet's Avatar
flageolet flageolet is offline Offline
Light Poster

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

 
0
  #3
Jan 27th, 2007
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...
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 209
Reputation: Ravalon is on a distinguished road 
Solved Threads: 15
Ravalon's Avatar
Ravalon Ravalon is offline Offline
Posting Whiz in Training

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

 
1
  #4
Jan 27th, 2007
Originally Posted by flageolet View 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.
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.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

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

 
0
  #5
Jan 28th, 2007
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....
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #6
Jan 28th, 2007
Originally Posted by WaltP View Post
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 27
Reputation: flageolet is an unknown quantity at this point 
Solved Threads: 0
flageolet's Avatar
flageolet flageolet is offline Offline
Light Poster

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

 
0
  #7
Jan 28th, 2007
I use some C++ (not much), but not in the posted code.

Thx for the answers.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC