简体   繁体   中英

I don't understand why this function isn't working

So this is a simple text-based game I'm creating, starting with a menu. I want people to be able to play the game when they press 'p', but this play function isn't working.

#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;
}

The function for playing the game is void game() and the menu is void menu().

You haven't declared the function...

void game();

declare it before void menu() function

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

You need to give prototype of function before using it, for that we need to mostly use header files in C/C++.

So before using, if (menuResponse == 'p') { game(); }

You should, declare prototype.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM