Console Input in C++
September 13, 2010 Leave a Comment
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.
Recent Comments