How to Enter Compose Name in Prompt

Hello!
I am compiling in VS, when I type a compose name, for example, "Davis Jones", only the first name is inputted, looks like that spaces between words do not work. Could someone help me to understand my mistake?

1
2
3
4
5
6
7
8
9
#include <iostream>
int main() {
	
		std::string a;
		std::cout << "\Please enter a compose name: " << std::endl;
		std::cin >> a;
		std::cout << "\You entered: " << a << std::endl;
	
}


OUTPUT : Davis.
Last edited on
http://www.cplusplus.com/reference/string/string/operator%3E%3E/ says:
Notice that the istream extraction operations use whitespaces as separators; Therefore, this operation will only extract what can be considered a word from the stream. To extract entire lines of text, see the string overload of global function getline.

and refers to getline: http://www.cplusplus.com/reference/string/string/getline/
Last edited on
>> only obtains chars until a white space is found (space, tab, newline). To obtain all the data, use getline().

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>

int main() {
	std::string a;

	std::cout << "Please enter a composer name: \n";
	std::getline(std::cin, a);

	std::cout << "You entered: " << a << '\n';
}


Also, you're missing the <string> include. What is \P and \Y supposed to do? That isn't valid use of \
Thank so much Keskiverto and Seeplus! I understood. The \P and \Y I forgot to put the "n" = \n

Topic archived. No new replies allowed.