Console Input in C++

The statement used for taking user input from the console is ‘cout’ statement.

#include <iostream>
#include <string>
using namespace std;

int main() {

string name;

int age;

// prompt user for his/her name

cout << ”Enter your name: “;

// read name

cin >> name;

// prompt user for his/her age

cout << ”Enter your age: “;

// read age

cin >> age;

// print a message

cout << ”Hello “ << name << ” of age “ << age << ”.” << endl;

return 0;

}

Output for the Program

Enter your name: Ali
Enter your age: 18
Hello Ali of age 22.

Advertisement

About Ali Turab Gilani
BS Computer Sciences from G.C University Lahore. Served as Teaching Assistant for Programming Fundamentals... Vice President IEEE Society Student Chapter...

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.