-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
  • 22,200
  • 8
  • 54
  • 100

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,994
  • 1
  • 18
  • 36
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