Category: C++

Added: 19th of December 2020

Viewed: 2,446 times

Basic user input and output using C++ in Linux, Learning C++

The following script in C++ asks the user to enter their name and age and then displays the results.
Two variables are created, string to store the name, and int (integer) to store the age.

Create a new file, then copy and paste the code below. Save the file as inputoutput.cpp

#include <iostream>
using namespace std;

int main()
string name;
int age;

cout << "What is your name? ";
cin >> name;
cout << "How old are your? ";
cin >> age;

cout << "Hello " << name << " you are " << age << " years old\n";
return 0;

Next we need to compile and create an executable file.
Open your terminal and enter the following command
g++ inputoutput.cpp -o inputoutput

To run the script