Влияние noskipws на cin >>
Как я понимаю, оператор извлечения пропускает пробел в начале и останавливается при встрече с пробелом или концом потока. noskipws можно использовать, чтобы перестать игнорировать начальные пробелы.
У меня есть следующая программа, где я использовал noskipws.
#include <iostream>
using namespace std;
int main()
{
char name[128];
cout<<"Enter a name ";
cin>>noskipws>>name;
cout<<"You entered "<<name<<"\n";
cout<<"Enter another name ";
cin>>name;
cout<<"You entered "<<(int)name[0]<<"\n";
return 0;
}
Мои запросы:
If I enter "John" as the first input, then the second cin>> operation does not wait for input and does not copy anything to the destination i.e. the name array. I expected second cin>> to transfer at-least a newline or end of stream, instead of just setting the destination string to empty. Why is this happening ?
The same thing is observed when I enter "John Smith" as the input for first cin>> statement. Why doesn't the second cin>> statement copy the space or "Smith" to the destination variable ?
Ниже приводится вывод программы:
Enter a name John
You entered John
Enter another name You entered 0
Enter a name John Smith
You entered John
Enter another name You entered 0
Спасибо!!!