2016-04-12 5 views
0

私の最大の問題は、すべて上記のとおりです、ラベルフィン(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; 
} 
+0

あなたの投稿の行番号は表示されません。無関係なコードを削除し、エラーが発生した場所を指してください。 – user463035818

+7

Protip:gotoを使用しないでください。 – Borgleader

+2

'goto'を使いたい場合がありますが、これはそうではありません。 'std :: exit'や単に' return 0; 'を使うことができます – NathanOliver

答えて

1

問題は、基本的にこれです:whateverがtrueの場合

int main() { 
    if (whatever) 
     goto fin; 
    std::string fish; 
fin: 
    return 0; 
} 

、後藤はfishの建設を過ぎてジャンプします。コンパイラは、gotoが実行されたかどうかによって、fishを破棄するための合理的なコードを生成することができないため、許可されていません。

解決策:gotoは使用しないでください。

可能性:

int main() { 
    if (whatever) 
     goto fin; 
    { 
    std::string fish; 
    } 
fin: 
    return 0; 

ここでは、fishは、ブロックの最後に破壊されますので、後藤は(わきその固有の構造化されていない自然からの)問題が発生することはありません。

ベター:

int main() { 
    if (!whatever) { 
     std::string fish; 
    } 
    return 0; 
} 
0

あなたがはるかに簡単な機能を使用して問題を再現することができます。

void foo() 
{ 
    goto fin; 
    std::string fish = "none"; 
    std::cin >> fish; 

    fin: 
} 

なぜそれが問題ですか?実行がfin:にジャンプすると、fishを初期化するコードは実行されません。そのデストラクタは、関数が戻るときに呼び出されます。デストラクタは初期化されていないオブジェクトで呼び出されるため、プログラムは未定義の動作を示します。

問題の最も簡単な修正は、mainのほとんどすべてを別の関数に入れて、gotoの代わりにreturnを使用することです。

void do_stuff() 
{ 
    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; 
     return; 
    } 
    if (answer == "n") 
    { 
     std::cout << "You were brutally killed." << std::endl; 
     return; 
    } 

    if (answer == "Miche") 
    { 
     std::cout << "The killers understood that you understood the prophecy, so they took you to their master" << std::endl; 
    } 
    else 
    { 
     std::cout << "You randomly babled " << answer << ", getting yourself killed."; 
     return; 
    } 

    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 = "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; 
    } 
    else 
    { 
     std::cout << "You failed and were immediately killed." << std::endl; 
    } 
} 

int main() 
{ 
    do_stuff(); 
} 
関連する問題