943,950 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1170
  • C++ RSS
Feb 8th, 2009
0

Mystery with getline, very simple program

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. string monster;
  8. cout << "Enter monster: ";
  9. getline(cin, monster);
  10. cout << "You entered: " << monster << endl;
  11. cin.get();
  12. return 0;
  13. }

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!
Last edited by sgw; Feb 8th, 2009 at 11:43 pm.
sgw
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
sgw is offline Offline
58 posts
since Jan 2008
Feb 8th, 2009
0

Re: Mystery with getline, very simple program

Must be some other problem -- your program works perfectly for me using VC++ 2008 Express on Vista Home.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Feb 8th, 2009
0

Re: Mystery with getline, very simple program

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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
sgw is offline Offline
58 posts
since Jan 2008
Jun 27th, 2009
-3

Re: Mystery with getline, very simple program

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
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Jun 27th, 2009
0

Re: Mystery with getline, very simple program

I compiled sucessfull with DevC++,i run the program,i wrote Bill Gates and it printed "You entered: Bill Gates"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
XTRobot is offline Offline
15 posts
since Jun 2009
Jun 29th, 2009
0

Re: Mystery with getline, very simple program

@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.
Last edited by shadwickman; Jun 29th, 2009 at 6:20 am.
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007
Jun 29th, 2009
0

Re: Mystery with getline, very simple program

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..
Reputation Points: 78
Solved Threads: 15
Junior Poster
u8sand is offline Offline
131 posts
since Dec 2008
Jul 2nd, 2009
0

Re: Mystery with getline, very simple program

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.
Last edited by tux4life; Jul 2nd, 2009 at 10:58 am.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jul 2nd, 2009
0

Re: Mystery with getline, very simple program

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.
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007
Jul 2nd, 2009
0

Re: Mystery with getline, very simple program

He said he was running it on Dev C++ in his post a few up.
I think you're confusing XTRobot 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.
Last edited by tux4life; Jul 2nd, 2009 at 3:18 pm.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Path to the .exe Being Run
Next Thread in C++ Forum Timeline: Want to save and load info to and from a txt file thats in a listbox





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC