Jump to content
  • Advertisement
Sign in to follow this  
Fuji

Regarding finding a name

This topic is 3498 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to have the player enter their name and have it returned. I realized I just deleted the file containing this that my friend helped me make and I don't know what to do. Here's what I have so far. I have like no programming experience, but today I'm starting to work a little with c++.I probably need to declare the name as a variable, but I'm not really sure. I want the player to be able to type a name and have it displayed in the welcome at the bottom. #include <iostream> using namespace std; int main () { cout << "Welcome to the Freakishly Uncanny Maze Extravaganza!!!"; system("PAUSE"); return 0; } string name; cout << "What is your name?" << endl; getline(cin, name); cout << "Welcome to FUME, " << name << "." << endl; system("PAUSE"); return 0; [Edited by - Fuji on May 16, 2009 8:46:30 PM]

Share this post


Link to post
Share on other sites
Advertisement
I wouldn't use system("Pause"). The main reason is that it makes porting your program a nightmare. Instead, you can use

cin.ignore()

for one 'enter' key press and
cin.ignore().get()

for two.

And for your code:

//Just for your information, comments start with two '/'. They are ignore by the compiler.

#include <iostream> //Our input/output library
#include <string> //Since we are going to use strings

using namespace std;

int main ()
{
string name; //Here we declare a string called 'name'
cout << "Welcome to the Freakishly Uncanny Maze Extravaganza!!!";
cin.ignore(); //Pause until the user presses enter
cout << "What is your name?" << endl; //Output this string
cin >> name; //Here we get the name
cout << "Welcome to FUME, " << name << "." << endl;

cin.ignore().get(); //Wait until the user presses enter.
return 0;

}




Hope this helps. :)

Share this post


Link to post
Share on other sites
Thanks, now how do you allow the name to include spaces? My friend and I figured this out once, but once again I forgot and it was in the program I deleted.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!