23
C++ Input
If you are coming directly from my Hello World tutorial, which can be found HERE. Then you would of noticed that the previous program only dealt with output. There was no way for us to receive any input form the user. This tutorial will provide some insight on how we receive information from a user.
When dealing with input we need somewhere to store the input. In C++ we would say that we need somewhere to read into
. Somewhere is a place in our computer's memory and a we call it an object.In order to access an object we need to give it a name, a named object is called a variable. Bellow is an example of us creating a program that takes user input. I will paste the code and then we will walk through it line by line.
#include <iostream>
int main()
{
std::cout << "please enter your first name, followed by enter/n";
std::string first_name; //string
std::cin >> first_name;
std::cout << "Hello" << first_name;
return 0;
}
#include
is how we tell the compiler what we want to include in this file. Then we have the <iostream>
it is the standard library in C++ for dealing with input and output. I recommend that you actually delete #include <iostream>
from your code and notice the errors that you get. It is because of this line of code that we have access to the std(standard) namespace, which we use to access cin
and cout
.std
name space to access cout(see-out), which for all intense and purposes represents our console. Just a quick reminder that you will not be able to use cout if you delete std::
. Then we have out output operator <<
which is how we send data to the console. We then have a string literal, in C++ string literals are represented with ""
. Lastly we end with /n
, this newline character is how we insert a newline at the end of our string and tell the compiler to end that line.This line of code reads data from our keyboard and places it into the first_name variable. cin
(see-in) refers to the standard input stream defined inside the standard library which we have access through std::
. Then the input operator >>
specifies where the the value will go and for us it goes into the first_name variable.
when we run our program as a whole and type "bob" and then hit enter we will get an output of Hello bob
. The enter is very important because it will send a newline character and a newline is how the computer tells when to stop running the program. Without hitting enter(sending a newline character) the computer will simply continue to collect characters.
int nums = 39;
represents an integerdouble nums = 39.4;
represents floating point numbers.char single= 'm';
represents a single character int nums = 39;
represents a string of charactersint nums = 39;
represents a true/false valuedouble-precision floating point
. A floating point is a computers approximation for a real number. A real number being any number that can be represented on a number line. So that means every number except infinity and imaginary numbers.""
quotation and for char we use the single syntax ''
.std::
when we are using char. This means that they do not come from the same namespace.int main()
{
1) std::cout << "Please enter your name and age\n";
2) std::string first_name;
3) int age;
4)std::cin >> first_name >> age;
5) std::cout << "Hello "<<first_name << ", age :" << age;
6) return 0;
}
1)
is us accessing the standard output stream to print the desired text to the console. Line two is us declaring a variable of type string , in reality this is us reserving a chunk of our computer's memory for further use. Line 3 is where we define a variable of type int and call it age. Line 4 might seem a little strange but all we are doing is telling our machine to read a string and an integer into the first_name and age variables. If that syntax is unsettling to you, the equivalent would be this:
std::cin >> first_name;
std::cin >> age;
Whitespaces
. Whitespace being defined as a newline, tab or space characters. A whitespace terminates the reading of strings. So us hitting the space bar is actually what ends the string. Once we have ended the string the program will expect us to enter a integer value.22 Bob
into the promp, we would get Hello 22, age :0
. We get 22 because 22 is just a string of characters, but where did the 0 come from? Since Bob
is not a integer we can not read it into a integer variable. Instead we will get 0, which is called a Garbage value
. This is just a random number or 0,that happens to be memory when the variable is used. We get garbage values because unlike other programming languages, variables in C++ are not initialized when they are defined.