问题
I need help with closing the program by entering 'quit'
for example.
while(true)
{
cout << "enter a name" << endl;
std::getline (std::cin,input);
if(input =='quit')
{
break;
}
}
it is not breaking out or quiting, also how come you can't compare a string to a int?
i.e. : while (input != 'quit') <<-- that won't work also.
回答1:
quit
needs to be in double quotes to be a string
:
#include <iostream>
int main()
{
std::string input;
while (true)
{
std::cout << "enter a name: ";
std::getline(std::cin, input);
if (input == "quit")
{
break;
}
}
std::cout << "Broken" << std::endl;
}
See it run.
also how come you can't compare a
string
to aint
.
Because this behaviour isn't defined by the c++ standard. Would "1.0"
be equal to 1
?
来源:https://stackoverflow.com/questions/19276439/how-to-enter-quit-to-close-progrom