Параметры текстового приключения в выражениях if и else if и else в C++

Здравствуйте, я очень новичок в C++.

Я делаю тестовое текстовое приключение, но не могу получить приключение, чтобы выбрать опцию просмотра моего персонажа Кён.

Что я делаю не так?

Я пытаюсь заставить программу просмотреть Kyung с выражением else if, но программа завершается, когда я набираю «TURN», чтобы попытаться просмотреть kyung. Я не знаю, что я делаю неправильно.

#include <iostream>

using namespace std;



int main()
{

    string choice;
    cout << "                 |                                          " << endl;
    cout << "                 |                                          " << endl;
    cout << "                 |                                          " << endl;
    cout << "                 |                                          " << endl;
    cout << "                 |                                          " << endl;
    cout << "                 |__________________________________________" << endl;
    cout << "                /..........................................." << endl;
    cout << "               /............................................" << endl;
    cout << "              /............................................." << endl;
    cout << "             /.............................................." << endl;
    cout << "            /..............................................." << endl;
    cout << "           /................................................" << endl;
    cout << "          /................................................." << endl;
    cout << "         /.................................................." << endl;
    cout << "        /..................................................." << endl;
    cout << "       /...................................................." << endl;
    cout << "      /....................................................." << endl;
    cout << "     /......................................................" << endl;
    cout << "    /......................................................." << endl;
    cout << "   /........................................................" << endl;
    cout << "  /........................................................." << endl;
    cout << " /.........................................................." << endl;
    cout << "/..........................................................." << endl;
    cout << "You wake-up in a empty room, you are on the ground." << endl;
    stupid1:
    cout << "Choices: type STAND to stand." << endl;

    cin >> choice;
    if(choice == "STAND") {
    cout << " you stood up." << endl;
    stupid2:
    cout << "  |                ----------------------------             " << endl;
    cout << "  |                |  ----------------------  |             " << endl;
    cout << "  |                | |       _____          | |             " << endl;
    cout << "  |                | |      /     |         | |             " << endl;
    cout << "  |                | |      |     |         | |             " << endl;
    cout << "  |                | |      \    /          | |             " << endl;
    cout << "  |                | |     __|   |___       | |             " << endl;
    cout << "  |                | |    /          \      | |             " << endl;
    cout << "  |                | |___|____________|_____| |             " << endl;
    cout << "  |                |__________________________|             " << endl;
    cout << "  |                                                         " << endl;
    cout << "  |                                                         " << endl;
    cout << "  |                                                         " << endl;
    cout << "  |                                                         " << endl;
    cout << "  |                                                         " << endl;
    cout << "  |                                                         " << endl;
    cout << "  |                                                         " << endl;
    cout << "  |                                                         " << endl;
    cout << "  |                                                         " << endl;
    cout << "  |                                                         " << endl;
    cout << "  |                                                         " << endl;
    cout << "  |_________________________________________________________" << endl;
    cout << " /.........................................................." << endl;
    cout << "/..........................................................." << endl;
    cout << " You see a painting on the wall." << endl;
    } else {
    cout << "That is not a choice." << endl;
    goto stupid1;
    }
    cout << "Choices, type TURN to turn around, or type INSPECT to inspect painting." << endl;
    cin >> choice;
    if(choice == "INSPECT") {
    cout << "                                                            " << endl;
    cout << "     __________________________________________________     " << endl;
    cout << "    |                                                  |    " << endl;
    cout << "    |                                                  |    " << endl;
    cout << "    |                                                  |    " << endl;
    cout << "    |                                                  |    " << endl;
    cout << "    |                 _____________                    |    " << endl;
    cout << "    |                /             \                   |    " << endl;
    cout << "    |               |               |                  |    " << endl;
    cout << "    |               |  __    __     |                  |    " << endl;
    cout << "    |               |  O |   O      |                  |    " << endl;
    cout << "    |               |    |          |                  |    " << endl;
    cout << "    |               |    |_         |                  |    " << endl;
    cout << "    |               |               |                  |    " << endl;
    cout << "    |                \  -----      /                   |    " << endl;
    cout << "    |            _____|           |_______             |    " << endl;
    cout << "    |         __/                         \____        |    " << endl;
    cout << "    |       /                                  \       |    " << endl;
    cout << "    |      /                                    \      |    " << endl;
    cout << "    |     |                                      |     |    " << endl;
    cout << "    |     |                                      |     |    " << endl;
    cout << "    |_____|______________________________________|_____|    " << endl;
    cout << "                                                            " << endl;
    cout << "                                                            " << endl;
    cout << "You inspected the painting." << endl;
    stupid3:
    cout << "Type BACK to go back." << endl;
    cin >> choice;
    if(choice == "BACK") {
    cout << "You went back." << endl;
       goto stupid2;
    } else if(choice == "TURN"){
        cout << "                                                 |          " << endl;
        cout << "                                                 |          " << endl;
        cout << "                                                 |          " << endl;
        cout << "                                                 |          " << endl;
        cout << "                                                 |          " << endl;
        cout << "                                                 |          " << endl;
        cout << "                                                 |          " << endl;
        cout << "                      /-\/-\                     |          " << endl;
        cout << "                     |  |  |                     |          " << endl;
        cout << "                     |  |  |                     |          " << endl;
        cout << "                 _____\ | /__                    |          " << endl;
        cout << "                /            \                   |          " << endl;
        cout << "               | _____  _____ |                  |          " << endl;
        cout << "               |       W      |                  |          " << endl;
        cout << "                \____      __/                   |          " << endl;
        cout << "                   /       \                     |          " << endl;
        cout << "__________________/_       _\____________________|          " << endl;
        cout << "....................|     |.......................\         " << endl;
        cout << "....................|     |........................\        " << endl;
        cout << "....................|_/-\_|.........................\       " << endl;
        cout << ".....................................................\      " << endl;
        cout << "......................................................\     " << endl;
        cout << ".......................................................\    " << endl;
        cout << "........................................................\   " << endl;
        cout << ".........................................................\  " << endl;
        cout << "..........................................................\ " << endl;
        cout << "............................................................" << endl;
        cout << "You saw Kyung" << endl;
    } else {
      cout << "That is not a choice." << endl;
      goto stupid3;
    }
    }







    return 0;
}

person dawg    schedule 21.06.2019    source источник
comment
Вы можете опубликовать код здесь для ознакомления codereview.stackexchange.com.   -  person Adam    schedule 21.06.2019
comment
я понял но не знаю как тему удалить   -  person dawg    schedule 21.06.2019
comment
@dawg: Пожалуйста, посмотрите мой ответ ниже, чтобы изменить структуру вашего кода. мой совет: старайтесь избегать goto в своем коде.   -  person ישו אוהב אותך    schedule 21.06.2019


Ответы (1)


Это просто потому, что ваш TURN выбор находится внутри INSPECT выбора:

if(choice == "INSPECT") {

  ... 

  if(choice == "BACK") {
  cout << "You went back." << endl;
     goto stupid2;
  } else if(choice == "TURN"){
     ...
  }

}

вы должны переместить его из TURN из блока INSPECT if следующим образом:

if(choice == "INSPECT") {
  ...
}
... 

if(choice == "BACK") {
  cout << "You went back." << endl;
  goto stupid2;

} else if(choice == "TURN"){
     ...
}

Подробнее:

Вы должны избегать использования goto в своем коде, потому что очень сложно отлаживать код goto, пожалуйста, читайте больше на Spaghetti код. Вы можете реструктурировать свой код, используя цикл while для игрового цикла и function для каждого по вашему выбору.

Вот рабочая версия с циклом while:

#include <iostream>

using namespace std;

void startRoom()
{
    cout << "                 |                                          " << endl;
    cout << "                 |                                          " << endl;
    cout << "                 |                                          " << endl;
    cout << "                 |                                          " << endl;
    cout << "                 |                                          " << endl;
    cout << "                 |__________________________________________" << endl;
    cout << "                /..........................................." << endl;
    cout << "               /............................................" << endl;
    cout << "              /............................................." << endl;
    cout << "             /.............................................." << endl;
    cout << "            /..............................................." << endl;
    cout << "           /................................................" << endl;
    cout << "          /................................................." << endl;
    cout << "         /.................................................." << endl;
    cout << "        /..................................................." << endl;
    cout << "       /...................................................." << endl;
    cout << "      /....................................................." << endl;
    cout << "     /......................................................" << endl;
    cout << "    /......................................................." << endl;
    cout << "   /........................................................" << endl;
    cout << "  /........................................................." << endl;
    cout << " /.........................................................." << endl;
    cout << "/..........................................................." << endl;
    cout << "You wake-up in a empty room, you are on the ground." << endl;
}

void standChoice()
{
    cout << " you stood up." << endl;
    cout << "  |                ----------------------------             " << endl;
    cout << "  |                |  ----------------------  |             " << endl;
    cout << "  |                | |       _____          | |             " << endl;
    cout << "  |                | |      /     |         | |             " << endl;
    cout << "  |                | |      |     |         | |             " << endl;
    cout << "  |                | |      \    /          | |             " << endl;
    cout << "  |                | |     __|   |___       | |             " << endl;
    cout << "  |                | |    /          \      | |             " << endl;
    cout << "  |                | |___|____________|_____| |             " << endl;
    cout << "  |                |__________________________|             " << endl;
    cout << "  |                                                         " << endl;
    cout << "  |                                                         " << endl;
    cout << "  |                                                         " << endl;
    cout << "  |                                                         " << endl;
    cout << "  |                                                         " << endl;
    cout << "  |                                                         " << endl;
    cout << "  |                                                         " << endl;
    cout << "  |                                                         " << endl;
    cout << "  |                                                         " << endl;
    cout << "  |                                                         " << endl;
    cout << "  |                                                         " << endl;
    cout << "  |_________________________________________________________" << endl;
    cout << " /.........................................................." << endl;
    cout << "/..........................................................." << endl;
    cout << " You see a painting on the wall." << endl;
}

void notChoice()
{
    cout << "That is not a choice." << endl;
}

void inspectChoice()
{
    cout << "                                                            " << endl;
    cout << "     __________________________________________________     " << endl;
    cout << "    |                                                  |    " << endl;
    cout << "    |                                                  |    " << endl;
    cout << "    |                                                  |    " << endl;
    cout << "    |                                                  |    " << endl;
    cout << "    |                 _____________                    |    " << endl;
    cout << "    |                /             \                   |    " << endl;
    cout << "    |               |               |                  |    " << endl;
    cout << "    |               |  __    __     |                  |    " << endl;
    cout << "    |               |  O |   O      |                  |    " << endl;
    cout << "    |               |    |          |                  |    " << endl;
    cout << "    |               |    |_         |                  |    " << endl;
    cout << "    |               |               |                  |    " << endl;
    cout << "    |                \  -----      /                   |    " << endl;
    cout << "    |            _____|           |_______             |    " << endl;
    cout << "    |         __/                         \____        |    " << endl;
    cout << "    |       /                                  \       |    " << endl;
    cout << "    |      /                                    \      |    " << endl;
    cout << "    |     |                                      |     |    " << endl;
    cout << "    |     |                                      |     |    " << endl;
    cout << "    |_____|______________________________________|_____|    " << endl;
    cout << "                                                            " << endl;
    cout << "                                                            " << endl;
    cout << "You inspected the painting." << endl;
}

void turnChoice()
{
    cout << "                                                 |          " << endl;
    cout << "                                                 |          " << endl;
    cout << "                                                 |          " << endl;
    cout << "                                                 |          " << endl;
    cout << "                                                 |          " << endl;
    cout << "                                                 |          " << endl;
    cout << "                                                 |          " << endl;
    cout << "                      /-\/-\                     |          " << endl;
    cout << "                     |  |  |                     |          " << endl;
    cout << "                     |  |  |                     |          " << endl;
    cout << "                 _____\ | /__                    |          " << endl;
    cout << "                /            \                   |          " << endl;
    cout << "               | _____  _____ |                  |          " << endl;
    cout << "               |       W      |                  |          " << endl;
    cout << "                \____      __/                   |          " << endl;
    cout << "                   /       \                     |          " << endl;
    cout << "__________________/_       _\____________________|          " << endl;
    cout << "....................|     |.......................\         " << endl;
    cout << "....................|     |........................\        " << endl;
    cout << "....................|_/-\_|.........................\       " << endl;
    cout << ".....................................................\      " << endl;
    cout << "......................................................\     " << endl;
    cout << ".......................................................\    " << endl;
    cout << "........................................................\   " << endl;
    cout << ".........................................................\  " << endl;
    cout << "..........................................................\ " << endl;
    cout << "............................................................" << endl;
    cout << "You saw Kyung" << endl;
}

void gameLoop()
{

    string choice;

    startRoom();

    cout << "Choices: type STAND to stand." << endl;
    while(true)
    {

        cin >> choice;
        if(choice == "STAND")
        {
            standChoice();
            cout << "Choices, type TURN to turn around, or type INSPECT to inspect painting." << endl;
        }
        else if(choice == "INSPECT")
        {

            inspectChoice();
            cout << "Type BACK to go back." << endl;
        }
        else if(choice == "TURN")
        {
            turnChoice();
            cout << "Type BACK to go back." << endl;
        }
        else if(choice == "BACK")
        {
            cout << "You went back." << endl;
            standChoice();
            cout << "Choices, type TURN to turn around, or type INSPECT to inspect painting." << endl;
        }
        else if(choice == "EXIT")
        {
            break;
        }
        else
        {
            notChoice();
            cout << "Choices: type STAND to stand." << endl;
        }

    }

}

int main()
{
    gameLoop();
    return 0;
}
person ישו אוהב אותך    schedule 21.06.2019