I have for example the following piece of code:
string str;
int i;
cout<<"input:";
cin>>str;
cout<<"integer:";
cin>>i;
There is actually nothing wrong with this code, but if I paste some text into the command prompt in “input”, it automatically fills some of the copied text into “integer”. How can I solve this?
Edit: I can type texts as long as I want, but when I copy texts it goes wrong. I don’t know why.
I guess, your text contains white spaces. So,
std::cinwill not work here. Usestd::getlineinstead.There’s a third parameter of
std::getline– delimiter. By default, it’s the new line char.If your text does contain new line chars, than this will not work. You have 2 options:
std::getlinestd::getline) from the user input (std::cin) and look for some special string, that will tell your program where the text ends. There’s no other way to know where does theinteger start (unless the text is with fixed size, but I doubt that)