-2

I am want to enter name in array and then willing to print it on screen. Code is given below

char name[20];          
cout << "Please enter name: ";
cin >> name;          
cout << name << endl;

Then after compilation I entered name

Ali Waqas

After that Ali is printed on screen but Waqas doesn’t. I have replace my cout statement with this loop

for(int i=0; i<20; i++){
   cout << name[i];
}

Again Ali is printed but after that garbage is printing rather Waqas

Jongware
  • 21,685
  • 8
  • 47
  • 95

2 Answers2

2

This is because operator<< skips white space. You could use getline() to get the desired effect.

string str;
getline(cin, str);
ravi
  • 10,736
  • 1
  • 14
  • 33
0

You should use

getline()

to get desired results. You will not get correct output with current code because operator << ignores the white spaces. You have to use this type of code

string str;
getline(cin, str);

For the further more details you must go through this link