Strings

This program asks the user for their name. With cin, it only reads until the first space or a new line.

1
2
3
4
5
6
7
8
9
10
11
12
//Ask for name and say hello
#include <iostream>
#include <string>
using namespace std;
int main()
{ string name;
  cout<<"What is your name:";
  cin>>name;
  cout<<"Hello "<<name<<endl;
  system("pause");
  return 0;
} //main 
CODE
Output:
What is your name:Robin
Hello Robin
Press any key to continue . . .

When we run this program and respond by typing in "Robin", it says "Hello Robin"

However, if we respond with "Robin Banks" it still says "Hello Robin"

What is your name:Robin Banks
Hello Robin
Press any key to continue . . .

NEXT: Using getline