M
M
michadimin2021-11-28 16:16:17
C++ / C#
michadimin, 2021-11-28 16:16:17

How to accept space into string from user input?

stringword;
cout << "Enter words: ";
cin >> word;
cout << word;

If I enter "Da 56 99" - only "Da" will be displayed.

How to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nowm, 2021-11-28
@michadimin

C++ has getLine, and it reads the entire line:

#include <string>
#include <iostream>

int main()
{
   std::string words;
   
   std::cout << "Введите слова: ";
   std::getline(std::cin, words);
   
   std::cout << "Слова: " << words << "\n";
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question