Please help a beginner - window closes

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2006
Posts: 7
Reputation: Simon268 is an unknown quantity at this point 
Solved Threads: 0
Simon268 Simon268 is offline Offline
Newbie Poster

Please help a beginner - window closes

 
0
  #1
Jul 19th, 2006
Hi all

Any help would be much appreciated.

Im following the about.com tutorial, and one of the exercises says i should make this program;

  1.  
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main ()
  7.  
  8. {
  9.  
  10. string name;
  11. int ID;
  12.  
  13. cout<<"enter your name\n";
  14. cin>> name;
  15.  
  16. cout<<"enter your id\n";
  17. cin>> ID;
  18.  
  19. cout <<"hello" <<name <<", or should i say" <<ID <<endl;
  20.  
  21. return 0;
  22.  
  23. }

It compiles fine; when I run it, it asks me for my name, and then id, but when i press return it just closes the window.

Ive tried adding in a cin.get() , which resolved the problem with the hello world programme i did, but not this.

I dont understand why it wont display the text afterwards?!?

Help please for my sanity (why dont think work like on the tutorials - did people try out the programmes before putting them on?)

P.S As a an afterfthough what purpose does the endl tag serve? it doesn't seem to make a difference.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Please help a beginner - window closes

 
0
  #2
Jul 19th, 2006
One cin.get() grabs the leftover newline (which remains after entering a number), and a second cin.get() will actually then wait for a keypress.

PS http://www.parashift.com/c++-faq-lit....html#faq-15.7

Originally Posted by Simon268
(why dont think work like on the tutorials - did people try out the programmes before putting them on?)
Don't be rude when it is you that doesn't understand the simple things.
Last edited by Dave Sinkula; Jul 19th, 2006 at 10:05 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Please help a beginner - window closes

 
0
  #3
Jul 20th, 2006
You might also be entering your full name with a space.

Spaces give problems. So you might want to try using cin.getline() instead of just cin?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 7
Reputation: Simon268 is an unknown quantity at this point 
Solved Threads: 0
Simon268 Simon268 is offline Offline
Newbie Poster

Re: Please help a beginner - window closes

 
0
  #4
Jul 20th, 2006
Thanks Dave Sinkula

Typing cin.get (); twice worked.
Last edited by Simon268; Jul 20th, 2006 at 8:47 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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