944,111 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1012
  • C++ RSS
Jul 19th, 2006
0

Please help a beginner - window closes

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

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Simon268 is offline Offline
7 posts
since Jul 2006
Jul 19th, 2006
0

Re: Please help a beginner - window closes

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

Quote 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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 20th, 2006
0

Re: Please help a beginner - window closes

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?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jul 20th, 2006
0

Re: Please help a beginner - window closes

Thanks Dave Sinkula

Typing cin.get (); twice worked.
Last edited by Simon268; Jul 20th, 2006 at 8:47 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Simon268 is offline Offline
7 posts
since Jul 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: 2d-c++ graphics guide PLZ!
Next Thread in C++ Forum Timeline: C++ For Loop User Response





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


Follow us on Twitter


© 2011 DaniWeb® LLC