Link to home
Create AccountLog in
Avatar of EE_wanna_be
EE_wanna_be

asked on

Need help with getline() in C++

I have a read() funcion I am having problems with in a class I am writing...The problem appears to be with the getline() function of the string library. Here's the code:

inline void Event::readEvent(Date aDate)
{
      string theEvent;
      myDate = aDate;

      cout << "\nEnter an event for ";
      aDate.print(cout);
      getline(cin, theEvent);
      
      cout << theEvent << " Entered for ";
      aDate.print(cout);
      myEvent = theEvent;
      
      
}

The code is all compiling well, but when I use this program, the getline() function is skipped over completly.  I am unable to read in the code for theEvent.

thanks for your help
Avatar of Infinity08
Infinity08
Flag of Belgium image

What do you mean by "skipped over" ? Do you happen to have any cin's earlier in the code ? They might have left a newline on the stream, which would make the getline read an empty line, and thus seem to skip.
Have you tried checking the state of the input stream to see if the fail or bad flags are set?
http://www.cplusplus.com/reference/iostream/ios/bad.html
http://www.cplusplus.com/reference/iostream/ios/fail.html
http://www.cplusplus.com/reference/iostream/ios_base/iostate.html

It may be that previous code that accesses the input stream has caused a stream error.
Which compiler are you using? If it is VC6, there's an eror in it's STL implementation, see http://support.microsoft.com/kb/240015 ("The getline template function reads an extra character after encountering the delimiter")
Avatar of EE_wanna_be
EE_wanna_be

ASKER

jkr: I am using Visual Studio 2005....

infinity: I have a cin that reads differnent class object...maybe that is it, I will have to look

...
here's the code for the other read()
inline void Date::read(istream & in)
{
      cout << "\nEnter the Month:  ";
      string dateString;
      in >> dateString;

      if (dateString == "January" || dateString == "Jan")
            myMonth = January;
      else if (dateString == "February" || dateString == "Feb")
            myMonth = February;
      else if (dateString == "March" || dateString == "Mar")
            myMonth = March;
      else if (dateString == "April" || dateString == "Apr")
            myMonth = April;
      else if (dateString == "May" || dateString == "May")
            myMonth = May;
      else if (dateString == "June" || dateString == "Jun")
            myMonth = June;
      else if (dateString == "July" || dateString == "Jul")
            myMonth = July;
      else if (dateString == "August" || dateString == "Aug")
            myMonth = August;
      else if (dateString == "September" || dateString == "Sep")
            myMonth = September;
      else if (dateString == "October" || dateString == "Oct")
            myMonth = October;
      else if (dateString == "November" || dateString == "Nov")
            myMonth = November;
      else if (dateString == "December" || dateString == "Dec")
            myMonth = December;
      else
            cout << "\nTry Agian.. " << dateString << " not recognized!";

      cout << "\nEnter the Day:  ";
      int aDay = 0;
      in >> aDay;
      assert (aDay > 0 && aDay < 32);
      myDay = aDay;

      cout << "\nEnter the Year:  ";
      int aYear = 0;
      in >> aYear;
      myYear = aYear;
}

>> I have a cin that reads differnent class object...maybe that is it, I will have to look

Is it called just before the getline ?

Make sure that it properly removes the trailing part of the line (including the newline character) from the stream.
>> Is it called just before the getline ?

"just before" as in the previous read from the cin stream.
how do I clear out the cin stream....just asking, I am a beginnner..:)
My preferred solution is to not use cin at all, but use only getline. Ie. use getline to read a line of input into a string, and then parse the string for the values you need.

Of course, you could just add a dummy getline after the cin that will read all that's left on the line, and discard it.
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
std::cin.ignore >>> http://www.cplusplus.com/reference/iostream/istream/ignore.html

Extracts characters from the input sequence and discards them.

The extraction ends when n characters have been extracted and discarded or when the character delim is found, whichever comes first. In the latter case, the delim character itself is also extracted.

Seems like I didn't contribute then ... Funny heh