[英]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. 我希望人们在按下“ 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;
}
The function for playing the game is void game() and the menu is void menu(). 玩游戏的函数为void game(),菜单为void menu()。
You haven't declared the function... 您尚未声明函数...
void game();
declare it before void menu()
function 在void menu()
函数之前声明它
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++. 您需要在使用函数之前给出函数的原型,因为我们主要需要在C / C ++中使用头文件。
So before using, if (menuResponse == 'p') { game(); 因此,在使用前,如果(menuResponse =='p'){game(); } }
You should, declare prototype. 您应该声明原型。
void game();
....
if (menuResponse == 'p')
{
game();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.