[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2349
  • Last Modified:

How to flush cin for getline?

Hello, i'm trying to code up a little command interpreter for the terminal.

basically it's like this:

while(notDone)
{
      getline(cin, input);
      istringstream instr(input);
      switch(..
                ......
}

Suppose the first command that I enter is "create blah," then I hit enter.
The command is executed.

NOW i just hit enter again without typing anything else, and it executes the command again because the input was not flushed.

Any help? Thanks.
0
lapchern
Asked:
lapchern
  • 5
  • 3
  • 3
  • +1
1 Solution
 
hongjunCommented:
try this

fflush(stdin);


hongjun
0
 
lapchernAuthor Commented:
Thanks for the response but I got no luck. I plugged fflush(stdin); at the end of my loop. Here's a sample output to show what I'm talking about
-------------------------------------
begin input
cr a //typed in this command
a created.
[hit enter]
Duplicate file name.
[hit enter]
Duplicate file name.
[hit enter]
Duplicate file name.
cr b //typed in this command
b created.
-------------------------------------
it says duplicate file name because it invisibly executes "cr a" over and over again unless i explictly  type in a new command.
0
 
hongjunCommented:
make sure you have got
   fflush(stdin);
before getline statement.

hongjun
0
Technology Partners: 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!

 
lapchernAuthor Commented:
Here's an actual chunk of my code

      while (notDone)
      {
            //arg1,arg2,arg3,arg4,arg5,arg6 = "";
            
            getline(cin, input);

            istringstream instr(input);
            arg6 = "";
            instr >> arg1 >> arg2 >> arg3 >> arg4 >> arg5 >> arg6;
                           ...begin interpretation
                              .........
                 }
0
 
hongjunCommented:
try something like this

while (notDone)
     {
          fflush(stdin);
          //arg1,arg2,arg3,arg4,arg5,arg6 = "";
         
          getline(cin, input);

          istringstream instr(input);



hongjun
0
 
lapchernAuthor Commented:
yeah i just tried that, still no good =(.

here are the things i included

#include "stdafx.h"
#include <string>
using namespace std;
#include <sstream>


if that helps
0
 
bcladdCommented:
The original poster and hongjun are using the word "flush" to cross purposes.

hongjun is trying to flush the input stream using standard file flushing routines. In this context, flushing means to complete any buffered I/O operations. Flushing is typically used on output in anicipation of input from the user (cin and cout are related in this way by default; before cin blocks for input, cout is flushed to make sure your prompt is visible to the user).

The original question says that the second time through their command processing loop, if an empty string is provided as input, the input value is not changed by getline. lapchern wants to "flush" the previous value from the input value. This actually has nothing to do with cin and everything to do with the variable holding the command. My quick and dity solution is to insert the line

    input = "";

right before the getline in the loop. Clears out the historical values for a fairly low cost.

Give it a shot.

-bcl
0
 
mrwad99Commented:
when using getline, it is important to remember that the function will read into 'input' until it hits the default terminator, which is '\n'.  It will then throw away the '\n' from the 'input' thereby leaving it in the buffer.

It looks like when you are reading in the second line, the '\n' is still left over so the getline function assumes this is what you have entered.  Get rid of the stored '\n' by using cin.ignore(strlen(input), '\n').

This will hence remove the '\n' from the input.

Also something that will benefit you if you use MSVC++ 6.0 or less:

http://people.msoe.edu/~welch/courses/string.html

A bug in the getline method.

Scary !

0
 
bcladdCommented:
getline(istream, string, char) will read up to the given terminator character. As mrwad99 notes, the default value for the terminator is '\n'. mrwad99 is not correct, however, on what happens to the terminator character. It is NOT copied into the string but it IS consumed from the input stream. Thus it reads up to and INCLUDING the terminator from the input stream and puts everything up to and NOT INCLUDING the terminator into the string.

VC++ isn't the only compiler to have getline problems. Borland BCC5.5 before patch level 1 (or 2, I can't recall) counted the appended '\0' in the length of a string read using getline. Lots of fun to figure out what was "wrong" with the length member after that.

-bcl
0
 
mrwad99Commented:
bcladd,

Thanks for that fact about the Borland compiler.  I dont use it but it is reassuring to hear that Microsoft is not the only one with problems !

Now, about the getline function:

On goes my dunce hat !

I mistook the internals of getline() with those of get(); the *latter* leaves the '\n' in the input stream, the former does *not* but throws it away.

<Sits in corner looking sheepish>
0
 
lapchernAuthor Commented:
i plugged in some stuff at the top and i still have no luck =(. it really boggles the mind.

            input = "";
            arg1,arg2,arg3,arg4,arg5,arg6 = "";

            getline(cin, input);
            istringstream instr(input);
            arg6 = "";
            instr >> arg1 >> arg2 >> arg3 >> arg4 >> arg5 >> arg6;


i also tried pluggin in cin.ignore(strlen(input), '\n'). at the top but i get a cannot convert std:string to char* error.

by the way i'm using visual c++ .net.
0
 
bcladdCommented:
I can fix the final error: use c_str():
   strlen(input.c_str()) is the legal call to strlen. Better would be to use the length() function on the string: input.length()

Note that arg1...arg5 are NOT cleared before you go through the istringstream. I'll bet arg6 IS cleared. Other option is to have an if on input == "" and never parse the empty string.

-bcl
0
 
lapchernAuthor Commented:
damn what a dumb mistake i made...i thought that line would work!! that fixed it!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 5
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now