c++ - switch 语句中的函数调用。

标签 c++

大家好,我正在尝试研究如何使用 switch 语句在我的代码中调用函数。我试图寻找许多不同的引用资料,但无论如何似乎都没有用,如果有人可以请我走上正确的道路,那将是一个很大的帮助。这是代码:

#include <iostream>
#include <string>

using namespace std;

int playGame(string word); 

int main() 
{
int choice;
bool menu = true;
do{
cout <<"Please select one of the following options:  \n";

cout << "1: Play\n"
    "2: Help\n"
    "3: Config\n"
    "4: Quit\n";

cout << "Enter your selection (1, 2 and 3): ";
cin >> choice;
//*****************************************************************************
// Switch menu to display the menu.
//*****************************************************************************
    switch (choice)
 {
      case 1:
        cout << "You have chosen play\n";
        int playGame(string word); 
        break;
     case 2:
        cout << "You have chosen help\n";
        cout << "Here is a description of the game Hangman and how it is    played:\nThe      word to guess is represented by a row of dashes, giving the number of letters, numbers and category. If the guessing player suggests a letter or number which occurs in the word, the other player writes it in all its correct positions";
        break; 
         case 3:
        cout << "You have chosen Quit, Goodbye.";
        break;
    default:
        cout<< "Your selection must be between 1 and 3!\n";

    }

}while(choice!=3);    
getchar();
getchar();


cout << "You missed " << playGame("programming");
cout << " times to guess the word programming." << endl;
}






int playGame(string word) //returns # of misses

{
    //keep track of misses
    //guess is incorrect
    //repeated guess of same character
    //guess is correct 

    int misses = 0;
    int exposed = 0; 
    string display = word;
    for(int i=0; i< display.length(); i++)
        display[i] ='*';


    while(exposed < word.length()) {
        cout << "Miss:" << misses << ":";
        cout << "Enter a letter in word ";
        cout << display << " : ";
        char response;
        cin >> response; 

        bool goodGuess = false; 
        bool duplicate = false;
        for(int i=0 ; i<word.length() ; i++) 
            if (response == word[i]) 
            if (display[i] == word[i]) {
                cout << response << " is already in the word.\n";
                duplicate = true; 
                break;
            } else {
                display[i] = word[i];
                exposed++; 
                goodGuess = true; 
            }
            if (duplicate)
                continue;

            if (!goodGuess){
                misses ++;
            cout << response << " is not in the word.\n";

            }
    }
    cout << "Yes, word was " << word << "." << endl;
    return misses;
}

最佳答案

您没有在 switch 语句中调用 playGame 函数,

switch (choice)
 {
      case 1:
        cout << "You have chosen play\n";
        //int playGame(string word); // this does not call playGame,  
                                     // it re-declare playGame function again
        playGame("word");              // this will call playGame with word parameter
      //^^^^^^^^^^^^^^^
        break;

关于c++ - switch 语句中的函数调用。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19291328/

相关文章:

c++ - Qmake给我错误

c++ - 在 "loop unrolling"中,所有展开的表达式都被执行了吗?

c++ - 构造函数之间的继承

c# - 如何向 C# 库添加服务引用?

c++ - gcc 中的指针导致段错误

c++ - 如何将 unique_ptr 的 vector 声明为类数据成员?

C++ 剪切字符指针

c++ - 成员初始化列表真的更有效吗?

c++ - std::is_assignable 是如何工作的?

c++ - 搜索节点 - Libxml