Solved

C++ equivalnet of fflush(stdin); ?

Posted on 2003-11-19
3
1,266 Views
Last Modified: 2008-03-17
This is a newbie program we had to write for our college test. The program asks for user to input a string, and then tells the user the number of characters in the string.

Here is the program
===================================

#include <iostream>
#include <string>
#include <conio.h>
#include <iomanip>
#include <stdio.h>

using namespace std;

struct text
{
      string input_text;
      int num_letters;
};

int main()
{
      text count;
      char responce = 0;

      do
      {
            cout << "Please enter a string: ";
            getline(cin, count.input_text);

            count.num_letters = count.input_text.length();

            cout << "The string " << count.input_text << " had "
                  << count.num_letters << " characters." << endl << endl;


            cout << "Would you like to enter another string (y or n) ?: ";
                                cin >> responce;
            cout << endl << endl;

      } while(tolower(responce) != 'n');

      getch();
      return 0;
}

===============================================
The program works on the first run. But after the question "Do you want to enter another string ..." the program no longer works properly. The program does not give me a chance to enter another string if I answer 'y'. It's like the program automatically enters the string for me and gives me the answer. Here is what I am talking about:

http://server4.uploadit.org/files/191103-error.png

I first thought that a newline character was stuck somewhere in the input buffer. I rewrote the program as follows:
============================================

#include <iostream>
#include <string>
#include <conio.h>
#include <iomanip>
#include <stdio.h>

using namespace std;

struct text
{
      string input_text;
      int num_letters;
};

int main()
{
      text count;
      char responce = 0;

      do
      {
            cout << "Please enter a string: ";
            getline(cin, count.input_text);

            count.num_letters = count.input_text.length();

            cout << "The string " << count.input_text << " had "
                  << count.num_letters << " characters." << endl << endl;


            cout << "Would you like to enter another string (y or n) ?: ";
            scanf("%c", &responce);
            fflush(stdin);
            cout << endl << endl;

      } while(tolower(responce) != 'n');

      getch();
      return 0;
}

==========================================================
Works like a charm.

http://server4.uploadit.org/files/191103-no_error.png

'fflush(stdin);' did all the magic. Something did get stuck in the input buffer. My questions are: Can I replace fflush(stdin); or whatever else with something of C++ so that the program works? Why does this problem occur in the first place?



Thanks!
0
Comment
Question by:krupini
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 4

Expert Comment

by:dhyanesh
ID: 9784705
Hi

Use can use cin.ignore() to skip 1 character in the input.

Dhyanesh
0
 
LVL 4

Accepted Solution

by:
dhyanesh earned 100 total points
ID: 9784716
Hi again

The problem occurs because when you enter a string in the first run you press the enter key after the string.

This results in the string being stored count.input_text. However the '\n' for the enter key is still there in the input buffer.

Thus if you use cin.ignore(); it will skip that one character of the input buffer.

More generally cin.ignore(n) will ignore 'n' characters in the input buffer.

Dhyanesh
0
 
LVL 15

Expert Comment

by:efn
ID: 9784724
Dhyanesh beat me to the answer.  I will just add that you can skip up to n characters or up to a newline character, whichever comes first, with cin.ignore(n, '\n');

--efn
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
IdTelnet1DataAvailable handler freezes the Application 4 104
Getting IP address 8 126
c++ reading data from file into two dimensional array 3 150
No module found pypyodbc, 3 58
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question