I enter the words in one line through the console. Next, displays everything except the first word. How to display the full line?

int main(int argc, const char * argv[]) { char str[100]; cout << "Enter the string: "; (cin >> str).get(); cin.getline(str, 100); cout << str; getch(); } 
  • cin reads up to space. - Alexey Sarovsky
  • I enter 5 words, but displays only 4. The first thing I told is that somewhere has disappeared. Are there alternative ways to enter text in one line and output? - Aleksey Tkachenko
  • (cin >> str).get(); What is this for? - Alexey Sarovsky

1 answer 1

Remove this strange construction :)

 (cin >> str).get(); 

It reads the first word in str , then reads the space. Then you re- read everything else in str , overwriting the first word.

  • If just cin >> str; the same effect - Aleksey Tkachenko
  • @AlekseyTkachenko you are offered to remove the entire line mentioned. Use cin with getline only. - αλεχολυτ