How to enter 'quit' to close progrom

最后都变了- 提交于 2019-12-26 08:52:43

问题


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 a int.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!