954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Mystery with getline, very simple program

I ran the following simple program 10 minutes ago and it worked as expected, i.e. it took input "monster" which contained spaces. But few minutes later I copied the program exactly the same to save as a new program, and it doesn't take the spaces in "monster" input any more--if the input for monster is "John Doe", then it's displayed as only "J". What's happening? I even restarted my computer and still doesn't fix it! Exactly this program:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string monster;
    cout << "Enter monster: ";
    getline(cin, monster);
    cout << "You entered: " << monster << endl;
    cin.get();
    return 0;
}


Update: Well, I retyped the exact same program again, saved as yet another new file name, and this time it worked! The previous program (exactly the same statements) still doesn't work! What's going on??? So strange!

sgw
Junior Poster in Training
59 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

Must be some other problem -- your program works perfectly for me using VC++ 2008 Express on Vista Home.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Yeah, really strange. If you noticed my update in the previous post, I retyped the exactly same program and it worked again. But the one that did not work still does not work! Exactly the same! I appreciate your reply :)

sgw
Junior Poster in Training
59 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

Delete that file and re-copy and paste.
That is VeRy StRaNgE huh! :)
May be taking it to "computer mental hospital" can help recognize what is wrong ...........hehehe :)

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

I compiled sucessfull with DevC++,i run the program,i wrote Bill Gates and it printed "You entered: Bill Gates" :)

XTRobot
Newbie Poster
15 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

@XTRobot

I know your problem's solved, but I thought I felt like saying that I'd advise against using DevC++. I used to use it, but it's been dead since 2005, so I'd use something like GCC or CodeBlocks now to keep the compiler up to date.

shadwickman
Posting Pro in Training
497 posts since Jul 2007
Reputation Points: 186
Solved Threads: 77
 

You better mark this is solved or your gonna start getting some spamming... But if you ask me it's probably a glitch with windows.. :/ But yes i agree it is VERY strange. normally when that happens it happens once then works after that..

u8sand
Junior Poster
131 posts since Dec 2008
Reputation Points: 78
Solved Threads: 15
 

What you can also try is just recompiling the source of the file which is behaving incorrectly.
(I compiled this source successfully using MinGW).

What compiler are you using ?

Could you just attach the whole file (the source of the file which doesn't work correctly) to this thread?
(and if possible: the executable file as well)
So I can check it by running it on my computer.

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

He said he was running it on Dev C++ in his post a few up. Hence my suggestion to use MinGW or a non-dead compiler to keep up to date.

shadwickman
Posting Pro in Training
497 posts since Jul 2007
Reputation Points: 186
Solved Threads: 77
 
He said he was running it on Dev C++ in his post a few up.


I think you're confusingXTRobot with the OP (sgw)

Hence my suggestion to use MinGW or a non-dead compiler to keep up to date.


Actually Dev-C++ uses MinGW as it's compiler but it's an older version, so I'd also recommend him to upgrade his compiler.
Normally you can configure Dev-C++ to work with the new MinGW compiler, so he can continue to use Dev-C++, but with the new MinGW compiler, this shouldn't be a problem.

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You