Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 19th, 2003, 11:29 PM
Registered User
 
Join Date: Jul 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default C++ equivalent of fflush(stdin); ?

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

Code:
#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:

Code:
#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!


Reply With Quote
  #2 (permalink)  
Old December 17th, 2003, 05:47 AM
Authorized User
 
Join Date: Jul 2003
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:I first thought that a newline character was stuck somewhere in the input buffer.
That's exactly right. This is a problem that everyone encounters at some point along the C++ learning curve. The problem occurs when you use getline() after you use cin>> to read input. When you answer yes to continue, you type this:

y\n

The '\n' is entered into the input stream when you hit return. cin>> is defined to skip all leading "whitespace characters" and stop reading input when it encounters a whitespace character. Whitespace characters are spaces, as well as carriage returns and tabs. cin>> actually stops before reading in a trailing whitespace character.

[edit]
The getline() function, unlike cin>>, does not skip leading whitespace characters, but similar to cin>> it stops when it encounters a whitespace character, but unlike with cin>> when it encounters a whitespace character, like a '\n', the '\n' is read in before getline() stops.
[/edit]

So, after reading the input:

y\n

cin>> stops at the '\n', and the '\n' is next up to be read, and your subsequent use of getline() doesn't cause the program to pause for input because there is still a '\n' to be read in, and the getline() function is happy to do that. That makes it seem like those lines weren't executed.

To solve the problem, all you have to do is use cin.ignore() after using cin>> and before using getline().

cin.ignore(); //will skip one character of the input.

Other forms of cin.ignore():

cin.ignore(10); // will skip ten characters(1 is the default if you don't specify a size)

cin.ignore(50, '\n'); // will skip 50 characters or until it encounters a '\n', whichever occurs first.
Reply With Quote
  #3 (permalink)  
Old December 17th, 2003, 12:03 PM
Registered User
 
Join Date: Jul 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ahh, very nice explanation. Thanks!

Reply With Quote
  #4 (permalink)  
Old January 9th, 2008, 03:55 PM
Registered User
 
Join Date: Jan 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

this seems to be working for me right now in Ubuntu 7.10:
void flush()
{
char nextChar;
while( nextChar != '\n' && nextChar != EOF)
{nextChar = cin.get();}
}
may have to play around with where you call the function...

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
description != '' equivalent XMLUser XSLT 3 March 1st, 2008 12:58 PM
"HtmlAnchor" equivalent arunagottimukkala ASP.NET 1.0 and 1.1 Professional 0 November 5th, 2007 06:14 AM
Putting and input in <STDIN> ZORCH Perl 1 June 14th, 2007 09:58 AM
executing STDIN on a WinXP Pro machine crmpicco Perl 1 October 24th, 2006 06:57 AM
ASP Equivalent U.N.C.L.E. Classic ASP Basics 3 July 8th, 2003 03:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.