簡體   English   中英

我不明白為什么這個功能不能正常工作

[英]I don't understand why this function isn't working

所以這是我正在創建的一個基於文本的簡單游戲,從菜單開始。 我希望人們在按下“ p”鍵時能夠玩游戲,但是此玩功能不起作用。

#include <iostream>
#include <string>

using namespace std;

void menu()
{
char menuResponse;
cout << "[P]lay [O]ptions [C]redits\n";
cin >> menuResponse;
if (menuResponse == 'p')
{
    game();
}
else if (menuResponse == 'o')
{
    cout << "There's actually nothing to see here, sorry.\n";
    char optionResponse;
    cout << "[B]ack\n";
    cin >> optionResponse;
    menu();
}
else if (menuResponse == 'c')
{
    cout << "Created by Crusader Studios, 2014\n";
    char creditResponse;
    cout << "[B]ack\n";
    cin >> creditResponse;
    menu();
}
else
{
    cout << "Please type a valid letter.\n";
    menu();
}
}

void game()
{
    cout << "\t\t\tPrologue\n\n";
}
int main()
{
    cout << "\t\tWelcome to my first game, 'A Hero's Journey!'\n\n";
    cout << "To navigate through menus, type the letters inside the brackets. For            example,\n";
    cout << "pressing 'p' in a menu with [P]lay and [O]ptions will let you play.\n\n";
    char beginResponse;
    cout << "Do you understand? If so, press any key:\n";
    cin >> beginResponse;
    cout << "Great! Now we can begin our game!\n";
    menu();
    return 0;
}

玩游戲的函數為void game(),菜單為void menu()。

您尚未聲明函數...

void game();

void menu()函數之前聲明它

void game();
void menu()
{
//... code
}

您需要在使用函數之前給出函數的原型,因為我們主要需要在C / C ++中使用頭文件。

因此,在使用前,如果(menuResponse =='p'){game(); }

您應該聲明原型。

void game();
....
if (menuResponse == 'p')
{
    game();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM