【发布时间】:2016-04-12 18:16:58
【问题描述】:
我最大的问题都在上面说了,无法跳转到标签fin(第27行错误),错误:从这里(第12和14行错误)和十字架初始化错误(第20行错误)请帮忙!
#include <iostream>
#include <string>
int main()
{
std::string name;
std::cout << "Please comply. y/n: ";
std::string answer;
std::cin >> answer;
if (answer == "y"){std::cout << "You were spared." << std::endl; goto fin;}
if (answer == "Miche"){std::cout << "The killers understood that you understood the prophecy, so they took you to their master" << std::endl; goto secret;}
if (answer == "n"){std::cout << "You were brutally killed." << std::endl; goto fin;}
else {std::cout << "You randomly babled " << answer << ", getting yourself killed."; goto fin;}
secret:
std::cout << "In order to fully find out if you are the legendary Miche, they took you to their leader."
<< " The master looked you over, and asked you one final question. The master asks you, fish?" << std::endl;
std::string fish; fish = "none";
std::cin >> fish;
if (fish == "fish."){std::cout << "You were put in the throne of the king, where you ruled your near killers and their species for eternity."
<< std::endl; goto fin;}
else {std::cout << "You failed and were immediately killed." << std::endl; goto fin;}
goto fin;
fin:
return 0;
}
【问题讨论】:
-
我们在您的帖子中看不到行号。请删除不相关的代码,并指向发生错误的地方。
-
Protip:不要使用goto;
-
在某些情况下,人们想使用
goto,但事实并非如此。您可以使用std::exit或仅使用return 0; -
@NathanOliver,我真的很想在 C++ 程序中看到
goto的合理案例。如果您发布一个作为答案,我会亲自投票(当然,前提是它是合理的!) -
@NathanOliver,是的,有点期待这个。但是适当的重构(例如将循环放入另一个函数并从中返回)通常会产生更好的结果。有些人主张
throw让你摆脱循环,但我不赞成。
标签: c++