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

First and Last name compilation problem. Noob.

This is my first time ever programming in C++ so please talk me through this step by step. This is my second compilation after the classic Hello World! program and I am running into a problem I cannot figure out.

The code executes just fine but when I run it, it gives me two lines 'at the same time' asking me to enter my first and last name. I want there to be a pause where it asks me for my first name first ... press enter, THEN asks me for my last. Here's the code.

//Programmer:Ace
//This program read and writes first_name
//Such a message is typically called a prompt because it prompts
//the user to take an action
//program written on 04/26/10
#include <iostream>
#include <string>
int main ()
{
//A string variable may be initialized at the point of declaration
std::string myfirst_string=" a message which is typically called a prompt";
std::cout <<"This is "<<myfirst_string <<":\n";
std::cout <<"Please enter your first name (followed by 'enter'):\n";
std::string first_name ; // first_name is a variable of type string
std::cin >> first_name; // read characters into first_name
std:cout <<"Please enter your last name (followed by 'enter'):\n";
std::string last_name; // last_name is a variable of type string
std::cin >> last_name; // read characters into last_name
std::cout <<"Hello, you are "<<first_name<<" " <<last_name<< "!\n";
return 0;

}


Thanks.

acerious
Newbie Poster
9 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

The only problem I found was line 16: you have just one colon instead of two after std. Otherwise it worked ok for me with Code::Blocks on Ubuntu.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Woot, it worked! I have no idea how I could have missed that after so many revisions. Programming sure is tricky! I love it.

acerious
Newbie Poster
9 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

A little piece of advice: do put all your variable declarations at the beginning of the function
It helps a lot in terms of readability and program maintenance in future.

chiwawa10
Junior Poster
156 posts since Jul 2005
Reputation Points: 88
Solved Threads: 27
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You