本文介绍了如何输入“quit”关闭progrom的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助,通过输入退出来关闭程序。

I need help with closing the program by entering 'quit'

例如。

while(true)
{
  cout << "enter a name" << endl;
  std::getline (std::cin,input);
  if(input =='quit')
  {
    break;
  }
}

它不会突破或退出,你不能比较一个字符串到一个int?

it is not breaking out or quiting, also how come you can't compare a string to a int?

ie :while(input!='quit')<< - 这将不会工作。

i.e. : while (input != 'quit') <<-- that won't work also.

推荐答案

quit 需要用双引号括起来成为字符串

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;
}

因为此行为未由标准。 1.0等于 1

Because this behaviour isn't defined by the c++ standard. Would "1.0" be equal to 1?

这篇关于如何输入“quit”关闭progrom的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 23:06