Mystery with getline, very simple program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 42
Reputation: sgw is an unknown quantity at this point 
Solved Threads: 0
sgw sgw is offline Offline
Light Poster

Mystery with getline, very simple program

 
0
  #1
Feb 8th, 2009
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Mystery with getline, very simple program

 
0
  #2
Feb 8th, 2009
Must be some other problem -- your program works perfectly for me using VC++ 2008 Express on Vista Home.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 42
Reputation: sgw is an unknown quantity at this point 
Solved Threads: 0
sgw sgw is offline Offline
Light Poster

Re: Mystery with getline, very simple program

 
0
  #3
Feb 8th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,389
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 127
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: Mystery with getline, very simple program

 
-3
  #4
Jun 27th, 2009
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
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 14
Reputation: XTRobot is an unknown quantity at this point 
Solved Threads: 0
XTRobot's Avatar
XTRobot XTRobot is offline Offline
Newbie Poster

Re: Mystery with getline, very simple program

 
0
  #5
Jun 27th, 2009
I compiled sucessfull with DevC++,i run the program,i wrote Bill Gates and it printed "You entered: Bill Gates"
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: Mystery with getline, very simple program

 
0
  #6
Jun 29th, 2009
@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.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 117
Reputation: u8sand is on a distinguished road 
Solved Threads: 15
u8sand's Avatar
u8sand u8sand is offline Offline
Junior Poster

Re: Mystery with getline, very simple program

 
0
  #7
Jun 29th, 2009
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..
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Mystery with getline, very simple program

 
0
  #8
Jul 2nd, 2009
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.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: Mystery with getline, very simple program

 
0
  #9
Jul 2nd, 2009
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.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Mystery with getline, very simple program

 
0
  #10
Jul 2nd, 2009
Originally Posted by shadwickman View Post
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)

Originally Posted by shadwickman View Post
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.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC