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

Please help a beginner - window closes

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;

#include <iostream>
#include <string>
using namespace std;
 
int main ()
 
{ 
 
string name;
int ID;
 
cout<<"enter your name\n";
cin>> name;
 
cout<<"enter your id\n";
cin>> ID;
 
cout <<"hello" <<name <<", or should i say" <<ID <<endl;
 
return 0;
 
}


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.

Simon268
Newbie Poster
7 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

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-lite/input-output.html#faq-15.7

(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.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Thanks Dave Sinkula

Typing cin.get (); twice worked.

Simon268
Newbie Poster
7 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You