| | |
can't seem to make a yes/no question
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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
I can't seem to figure out what I'm doing wrong?
Thx
C Syntax (Toggle Plain Text)
#include <stdio.h> using namespace std; char answer; int main(int argc, char *argv[]) { //ask if there are new patients printf ("if new patients type 'y'.\n"); scanf ("%c", &answer); if (answer == 'y' || answer == 'Y') printf("something\n"); // ask if they want to see the list of patients printf ("wanna see list: type 'y'.\n"); scanf ("%c", &answer); if (answer == 'y' || answer == 'Y') printf("something else\n"); return 0; }
Last edited by flageolet; Jan 27th, 2007 at 2:07 pm. Reason: forgot translating -> same question
•
•
Join Date: Jul 2005
Posts: 1,688
Reputation:
Solved Threads: 265
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).
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).
•
•
•
•
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.
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.
C Syntax (Toggle Plain Text)
#include <stdio.h> static const char stream[BUFSIZ] = "Default initialization"; static size_t loc = 0; // Single character input int cget( void ); int main( void ) { int ch; while ( ( ch = cget() ) != EOF ) putchar( ch ); putchar( '\n' ); return 0; } int cget( void ) { if ( stream[loc] == '\0' ) return EOF; return stream[loc++]; }
C Syntax (Toggle Plain Text)
#include <stdio.h> static const char stream[BUFSIZ] = "A\n"; static size_t loc = 0; // Single character input int cget( void ); int main( void ) { int ch; while ( ( ch = cget() ) != EOF ) putchar( ch ); return 0; } int cget( void ) { if ( stream[loc] == '\0' ) return EOF; return stream[loc++]; }
C Syntax (Toggle Plain Text)
#include <stdio.h> static const char stream[BUFSIZ] = "y\ny\n"; static size_t loc = 0; // Single character input int cget( void ); int main( void ) { int answer; //ask if there are new patients printf ("if new patients type 'y'.\n"); answer = cget(); if (answer == 'y' || answer == 'Y') printf("something\n"); // ask if they want to see the list of patients printf ("wanna see list: type 'y'.\n"); answer = cget(); if (answer == 'y' || answer == 'Y') printf("something else\n"); return 0; } int cget( void ) { if ( stream[loc] == '\0' ) return EOF; return stream[loc++]; }

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'.
C Syntax (Toggle Plain Text)
void flush_stdin( void ) { int ch; do ch = getchar(); while ( ch != '\n' ); }
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.
Yeah, what Ravalon said. You can check out this series for deeper understanding of why we say "stop using
Also, there is absolutely nothing C++ about what you posted so why the
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
•
•
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
![]() |
Similar Threads
- Internet Explorer Favorites, custom website icons disappearing (Web Browsers)
- Compile PHP (PHP)
- MIPS Question (Assembly)
- New layout comments (DaniWeb Community Feedback)
- Clearification with information hiding (C++)
- Setting up Mac through Router (Networking Hardware Configuration)
- Requiring selection in form based on previous question selection (HTML and CSS)
- (dda) (C)
Other Threads in the C Forum
- Previous Thread: read file
- Next Thread: gets() problem in C
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include infiniteloop initialization input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation overwrite owf pdf pointer pointers posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming standard strchr string suggestions systemcall test testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






