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.

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.

Member Avatar for iamthwee

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?

Thanks Dave Sinkula

Typing cin.get (); twice worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.