2013-10-10 94 views
0

嘿傢伙我試圖找出如何使用switch語句在我的代碼中調用函數。我曾嘗試尋找許多不同的參考資料,但無論如何,如果有人可以請我把它放在正確的道路上,那將是一個很大的幫助。下面的代碼:函數在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; 
} 
+0

wrongi語法,其中「字」被字符串替代你想傳遞。 –

回答

1

你是不是調用switch語句playGame功能,在「案例1」,稱「玩笑(字)」

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; 
+0

我一直在玩遊戲(單詞);詞似乎是不確定的? – user2857301

+0

單詞未定義,因爲原始代碼沒有聲明「單詞」變量。你需要把這個playGame(「單詞」); – huahsin68

+0

@ user2857301是。如果你需要單詞來保存不同的值,你需要定義它並從輸入中獲取它。 – billz

0
int playGame(string word); 

在您的switch語句可能是問題...嘗試:

int misses = playGame(word); 

你正試圖從你的瑣事方法返回未命中的數量,以便你有將返回數據放入一個變量中。