Category: C++

Added: 19th of December 2020

Viewed: 215 times

Basic user input and output using C++ in Linux

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
./inputoutput

Other Tips & Tutorials

C++

Basic user input and output using C++ in Linux

Posted: 19th of December 2020

Views: 215

C++

Install Visual Studio Code in Kubuntu, Ubuntu and Ubuntu based distributions. Compile and execute C++ script

Posted: 18th of October 2020

Views: 333

C++

Create a simple c++ script using Ubuntu and other Ubuntu based distributions

Posted: 17th of October 2020

Views: 293