Beginning C++ getting input from a user.

This tutorial will show you how to get input from a user and then display it on the screen.

Begin by starting a new c++ application and name it AddingTwoNumbers. So as the name implies we are going to get two numbers from the user and display them on the screen.

Now your project starts with code I want you to delete the code you have so that we can create this program line by line. Start by adding the following to main.cpp.

#include <iostream>
 

#include allows you to use preexisting code like libraries in the current file. We are including iostream which is a library that contains the code to use cout, which writes text to the command prompt, and cin, which gets an input from the user. Next lets make our main function.

int main()
{
        return 0;
}
 

The int right before main tells us that this function will return an integer before it exits. Then the name of the function is main. Every c++ program you make needs to have a main function, because that is the function that will be called at the start of your program. The empty parentheses indicates that we do not get any input from the function that called main. The {} tell the program what code belongs to main. Then we return to what ever called main 0; we must return an integer because we said we would at the beginning of main.

Now lets add the following right after the opening curly bracket {.

int addNum1;
int addNum2;
 

These two lines do the same thing, Instantiate an integer. We are using integers so we can only get whole numbers from the user so lets ask the users for some input.

std::cout << "Please enter one whole number and then press enter.\n";
std::cout << "Please enter another whole number and then press enter."<< std::endl;
 

cout is part of a namespace called std so we need to prefix std:: to the beginning of any code that is part of the std namespace. << is always used with cout statement. In the second statement you see two << every time you want to add a new variable to the cout statement you need to put << before it. In both of the cout statements I put in code that tells the computer to return. "\n" can be used only in quotation marks and std::endl (which all so belongs to the std namespace) is used without quotation marks. I believe that is the only difference between the two. When you run the code you will not be able to enter anything in yet so lets fix that. Change your code to look like the following.

std::cout << "Please enter one whole number and then press enter.\n";
std::cin >> addNum1;
std::cout << "Please enter another whole number and then press enter."<< std::endl;
std::cin >> addNum2;
 

Now we added cin statements which again belong to the namespace std. Cin statements will stop the program and wait for the user to enter in a value and then press enter. Once the user presses enter the program will store the input. In the case of the first cin statement the program stored the value in addNum1. Ok the last thing we need to do is output the sum of the two numbers, so add in this line.

std::cout << "The two numbers added together are "<<addNum1+addNum2<<".\n";
 

There you go your code should be complete and look like the following.

#include <iostream>

int main()
{
        int addNum1;
        int addNum2;
        std::cout << "Please enter one whole number and then press enter.\n";
        std::cin >> addNum1;
        std::cout << "Please enter another whole number and then press enter."<< std::endl;
        std::cin >> addNum2;
       
        std::cout << "The two numbers added together are "<<addNum1+addNum2<<".\n";
        return 0;
}
 

Now you know how to get input from a user in c++. Please check out my other tutorials.

This entry was posted in C++. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>