You are here:

C++/C++ program


I want to make a simple program to ask a user two questions

what is your name?  
What is the name of your dog?

The user answers both questions and then it display

Your name is:(what he answered) and your dog is (what he answered)

how would I do this?

This is my code so fa

#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

int main()

cout << "What is your name?:";

cin >>

system ("pause");

return 0;

As this looks like it may be homework or coursework I am going to show you part of the solution and leave you to expand this to the full program. Even if it is not you would not learn much if I just presented you with the complete solution.

First, a quick question: why are you including <windows.h>?

This is for use if you are using the MS Windows API (Win32 or Win64), which you are not - you are only using standard C++ features. So start by removing this #include directive.

Now std::cin will read data from the console and place it into program accessible storage - called a variable or object.

In this case you require a string of characters to be entered, so the variable (or object) we wish to store the data in should be a std::string, for which you have already included the C++ standard library header <string>.

So first we declare and define a std::string object (or variable) (declaring an object merely states that it exists, defining it reserves storage for it. You can declare something as many times as you like; you can only define something once. In C++ most variable declarations are also definitions):

   string userName; // declare and define an empty string object local to the main function call
   cin >> userName; // read from console into the userName string object.

Next you need to write the entered string back to the console for the user to read:

   cout << "Your name is: " << userName << endl;

Or just writing an newline character directly at the end of the line:

   cout << "Your name is: " << userName << '\n';

Note the use of single quotes around \n - this indicates a single character literal value rather than a string of characters. Note also the \n represents a single newline character and the \ is the escape character used to indicate such awkward to type characters.

The difference between std::endl and '\n' is that the former outputs a newline character and flushes any buffered data whereas the '\n' just writes a newline to the stream which may buffer it and output it later in one go with other buffered characters.

This gives you enough information to prompt for, input and output the user's name. I shall leave it to you to work out how to expand this to prompt for, read in and output the user's dog's name. Do not be afraid to get things wrong a few times or not quite correct. You can always go back and change things and try again.

Note that this solution builds on your provided example code, so I elided the std:: namespace qualification from std::string, std::cin, std::cout and std::endl as all seen names in the standard namespace are brought into the default one we are using here by the using namespace std; directive.

Also as you started to read the answers using:

   cin >>

I have kept to this model. Be aware though that each read only reads a single group of characters between whitespace where whitespace is taken to mean spaces, tabs and newlines, hence entering Joe Bloggs will read Joe on the first read and Bloggs on the next read if there were one.

So please have a go at putting the pieces together from the information in this answer. If you get really stuck ask another question stating clearly exactly what it is you are having trouble understanding or getting to work. I will _not_ answer if you just ask me to finish the program for you.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.