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

#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;
}

Recommended Answers

All 6 Replies

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).

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...

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.

#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++];
}

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.

#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++];
}

Now, if you change the logic of the code to read two characters like your program tries to do, you get this.

#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++];
}

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'.

void flush_stdin( void )
{
  int ch;

  do
    ch = getchar();
  while ( ch != '\n' );
}
commented: amazin explanation +2

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 ?

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

sscanf ?

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

Thx for the answers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.