In the previous post, we talked about user input. Well, there’s no post about user input until now.
A very simple user input program is given below:
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Enter your name: ";
string name;
cin >> name;
cout << "Your name is " << name << "." << endl;
return 0;
}
This program will get the input from us and then, outputs our input. There are many new things to notice:
- This time, we also included string in our program. String is nothing special, just a group of characters with some useful functions(we will discuss it later, in detail). The only point to get in our minds is that, string is not a primitive type. Just like including string allows us to use strings in our program, including iostream allows us to use
cout
,endl
andcin
.endl
is not important for usingcout
.endl
can be used anywhere aftercout <<
and is used to skip the present line.cin
is the standard method for getting input from user. Once you typed the input, you need to press enter to proceed. For output, we use<<
and for input,>>
.
In the same way, you can input integer. An integer is not a group of characters so, trying to assign an integer, group of characters will give us nothing.
Are you facing some problems? Try to input more than one words like: ‘Robo Tex’. Is there some problem with the output? Here’s the output:
Your name is Robo.
Where’s Tex? The problem is that cin ends when space comes in its way! Instead of cin
, we can use getline
:
cout << "Enter your name: ";
string name;
//cin >> name;
getline(cin, name);
cout << "Your name is " << name << "." << endl;
This will solve the problem. cin
takes a word from input buffer whereas getline
takes a line from the input buffer. In getline
, you have to specify at least two things. First one is the input method which is cin
and the second one is the variable to be filled with input.
Have you noticed that //cin >> name;
is not doing anything in the code? This is a single-line comment. Whatever is after double slashes, it will not be checked by the compiler and will only affect that line. For making multi-line comments, use /*
and */
. The code between /*
and */
is not considered by the compiler.
Commenting never influences the performance of a program. It is used in order to make notes with the code, describe the code for an easy understanding and removing the codes temporarily from the program. It is very useful and so, we will use it so much!
Example of a single line and multi-line commenting:
/*This is a
multi-line comment.
Following code takes a line from input buffer:
*/
cout << "Enter your name: ";
//Getting input.
string name;
//cin >> name;
getline(cin, name);
//Outputting the input.
cout << "Your name is " << name << "." << endl;
cin
and getline
requires pressing enter to shift input to the variable. You can have input without pressing enter by using getch
which takes a character from the input buffer and also removes that character from the screen unlike cin
and getline
. We must include conio.h to use getch
in our program just like string.
With other including (e.g iostream), #include <conio.h>
In the main function, (i.e in the flower brackets below int main()
)
cout << "Enter a character..." << endl;
char input;
//cin >> input;
input = getch();
cout << "Character: " << input << endl;
We can do many fun stuff with user input and conditional statements together. But for now, this is it!
Note: We can use string type without including string. This is because it is also in the iostream. But we can’t use it with cout
unless we include string.