C++ 如何在else-if中使用(或不使用?)goto

C++ 如何在else-if中使用(或不使用?)goto,c++,C++,每次我使用if和goto绕过一个进程并直接转到quit,它不会用命令绕过某个内容,而是直接转到末尾。以下是一个例子: #include <iostream> #include <string> using namespace std; int main() { string k; cout <<"What would you like to do ?\n"<<"Continue or Exit\n"; cin >> k;

每次我使用
if
goto
绕过一个进程并直接转到quit,它不会用命令绕过某个内容,而是直接转到末尾。以下是一个例子:

#include <iostream>
#include <string>
using namespace std;
int main()
{
  string k;
  cout <<"What would you like to do ?\n"<<"Continue or Exit\n";
  cin >> k;
  if (k = "exit","Exit") goto s;
  else (k = "Continue","continue");
  cout << "You continued...";
  s:
  return 0
}
#包括
#包括
使用名称空间std;
int main()
{
串k;
CUT< P> <强> 1)< /强>不要使用<代码> Goto :这里在现代C++代码中使用它几乎是不合适的。
2)使用
=
比较字符串(
=
用于
字符串
赋值)

3)
不是“and”运算符,请使用:

if (k == "exit" || k == "Exit")

4)如果使用了无效的大括号,请缩进代码以正确查看真正需要的块。

如果
始终为真,则第一个
,因为在赋值之后,逗号运算符的RHS测试将为

您的简单程序不能保证使用
goto
。不过,我更倾向于使用函数表来处理此类任务

我完成了一个示例。我们从一个简单的回调类开始。回调接口附带了一个默认实现,该实现抱怨输入

struct Callback {
    virtual ~Callback () {}
    virtual void operator () (std::string k) const {
        std::cout << "Unrecognized input: " << k << '\n';
    }
};
每个回调实现都执行示例程序中提供的简单操作:

class ExitCallback : public Callback {
    void operator () (std::string) const { std::exit(0); }
    CONST_SINGLETON(ExitCallback);
};

class ContinueCallback : public Callback {
    void operator () (std::string) const {
        std::cout << "You continued...\n";
    }
    CONST_SINGLETON(ContinueCallback);
};

您需要学习如何使用调试器,还需要学习更多关于C语言的知识++

if (k = "exit","Exit") goto s;   //VERY WRONG
并不意味着你想要什么:

  • k=“exit”
    =
    被理解为分配
  • 逗号被理解为
如果(k==“exit”| | k==“exit”)并且可能的话,至少编码

另外,启用所有警告(和调试信息)。也许您的编译器可以警告您


<>我建议你从(例如,在其他地方)读一些源代码。这会给你很多启示。

你的代码反映了你的意图,但是这些意图不是用C++编写的。它们是一个有效的C++程序——它当然是编译的,但是你的意思不是你写的。 它在这里,固定做你想做的事

int main()
{
  string k;
  cout << "What would you like to do ?\nContinue or Exit" << endl;
  cin >> k;
  if (! (k == "exit" || k == "Exit")) {
    if (k == "Continue" || k == "continue") {
      cout << "You continued...";
    }
  }
  return 0
}
intmain()
{
串k;
库特k;
如果(!(k==“退出”| k==“退出”)){
如果(k==“继续”| | k==“继续”){

cout适当的缩进和样式将帮助您看到您的一些努力

首先,我强烈建议将字符串转换为小写或大写。这样就无需检查字符串中所有大写字母和小写字母的排列:

  cout << "What would you like to do ?\n"
       << "Continue or Exit\n";
  cin >> k;
  std::transform(k.begin(), k.end(), k.begin(), tolower);
记住,'='是赋值,'='是比较

不需要比较“continue”,因为如果响应不是“exit”,它会“自动”进行比较

使用
转到

在这种情况下,不建议使用
goto
如果你真的必须使用goto,你将需要不止一个标签

int main(void)
{
  string k;

top_of_loop:  // Label for your goto
  cout << "What would you like to do ?\n"
       <<"Continue or Exit\n";
  cin >> k;
  std::transform(k.begin(), k.end(), k.begin(), tolower);

  if (k == "exit")
  {
    goto end_of_program;
  }
  // Execution continues here automatically.

  else
  {
    if (k == "continue")
    {
       goto program_continuation;
    }
    else
    {
       cout << "Unknown response, try again.\n";
       goto top_of_loop;
    }
  }
program_continuation:
  cout << "You continued...";
  goto top_of_loop;

end_of_program:
  return 0
}

使用
break
而不是
goto
你为什么使用
goto
-在80年代的节目中你有没有编译过这个?你可以将字符串转换成小写,然后每个单词只进行一次比较:如果(k==“exit”| k==“exit”)
,并且没有不匹配的随机大括号。“不要使用goto”更好的说法是:goto在这里不合适,它只在最罕见的情况下有用。是的,我同意,我澄清。如果(…)
现在不是作业,那不应该是
吗?
if (k = "exit","Exit") goto s;   //VERY WRONG
int main()
{
  string k;
  cout << "What would you like to do ?\nContinue or Exit" << endl;
  cin >> k;
  if (! (k == "exit" || k == "Exit")) {
    if (k == "Continue" || k == "continue") {
      cout << "You continued...";
    }
  }
  return 0
}
int main()
{
  string k;
  cout << "What would you like to do ?\nContinue or Exit" << endl;
  cin >> k;
  if (k == "exit" or k == "Exit") return 0;
  if (k == "Continue" or k == "continue") {
    cout << "You continued...";
  }
  return 0
}
  cout << "What would you like to do ?\n"
       << "Continue or Exit\n";
  cin >> k;
  std::transform(k.begin(), k.end(), k.begin(), tolower);
  if (k == "exit")
  {
    return EXIT_SUCCESS;
  } // No goto's necessary
  // The code continues here.
int main(void)
{
  string k;

top_of_loop:  // Label for your goto
  cout << "What would you like to do ?\n"
       <<"Continue or Exit\n";
  cin >> k;
  std::transform(k.begin(), k.end(), k.begin(), tolower);

  if (k == "exit")
  {
    goto end_of_program;
  }
  // Execution continues here automatically.

  else
  {
    if (k == "continue")
    {
       goto program_continuation;
    }
    else
    {
       cout << "Unknown response, try again.\n";
       goto top_of_loop;
    }
  }
program_continuation:
  cout << "You continued...";
  goto top_of_loop;

end_of_program:
  return 0
}
  cout << "What would you like to do ?\n"
       <<"Continue or Exit\n";
  cin >> k;
  std::transform(k.begin(), k.end(), k.begin(), tolower);
  while (k != "exit")
  {
    // Your program stuff here.
    cout << "Program continued.\n";

    // Prompt the User again.
    cout << "\n"
         << "What would you like to do ?\n"
         << "Continue or Exit\n";
    cin >> k;
    std::transform(k.begin(), k.end(), k.begin(), tolower);
  }
  return 0; // Exit main().