2016-04-25 29 views
-2

我正在創建一個猜謎遊戲。每次用戶再次猜測時,我都需要顯示用戶以前的猜測。我正在使用數組和指針。下面的代碼僅顯示用戶輸入的最新內容。我需要它顯示以前輸入的列表。包含指針的代碼部分似乎是我的問題所在的位置,但我不確切知道在哪裏?如何在用戶使用數組猜測新數字之前顯示以前的猜測列表?

#include "stdafx.h" 
#include <iostream> 
#include <time.h> 
using namespace std; 

void arrayTable(int[]); 
int reviewGuess(int Answer, int userGuess); 
int _tmain(int argc, _TCHAR* argv[]) 
{ 

int guessNum = 3; // Declares how many guesses the user can take. 
int userGuess = 0; 
int Answer; 
char Choice; 

int *pointer = NULL; 

srand(time(NULL)); 
Answer = rand() % 20 + 1; //Gives a random number ranging from 1 to 20 
do{ 

    int x; 
    cout << "   ____________________________________________________\n"; 
    cout << "   |             |\n"; 
    cout << "   | I want to play a game       |\n"; // Displays a welcome message 
    cout << "   | I am thinking of a number between 1 through 20. |\n"; 
    cout << "   | Can you guess the number in less than " << guessNum << " tries ? |\n"; 
    cout << "   |   Press '1' to play.     |\n"; 
    cout << "   |  Press '2' if you want to EXIT!!!   |\n"; 
    cout << "   |__________________________________________________|\n"; 
    cin >> x; 
    switch (x) 
    { 
    case 1: 
     for (int i = 0; i < guessNum; i++) //Counter, for loop, stops when condition is met. 
     { 
     cout << " This is guess# " << i + 1 << " : "; //Takes the user input 
     cin >> userGuess; 

     int guessNum = 1; 
     pointer = new int[guessNum]; 
     for (int i = 0; i < guessNum; i++) //Store the user guesses 
     { 

      *(pointer + i) = userGuess; 

     } 
     cout << "Here is a list of your guesses" << endl; 

     for (int i = 0; i < guessNum; i++)    // Display the user guesses. 
     { 

      cout << " Guess# " << i + 1 << " is " << *(pointer + i) << endl; 
     } 



     reviewGuess(Answer, userGuess); //Calls the function 
     } 

     cout << " ------------------>    <----------------------\n"; 
     cout << " ------------------> You Lost!!! <----------------------\n"; 
     cout << " ------------------>    <----------------------\n"; 
     cout << " You have exceeded the amount of guesses you were given.\n"; 
     cout << " The correct Answer is : " << Answer << endl; 

     break; 
    case 2: 
     cout << "-------------->Thank You.\n"; 
     cout << "-------------->Have a good day.\n"; 

     break; 

     system("pause"); 
     return 0; 
     } 

cout << "would you like to play again (y/n)?"; 
cin >> Choice; 
} while (Choice == 'y'); 
system("pause"); 
return 0; 
} 

int reviewGuess(int Answer, int userGuess) 
{ 
if (userGuess != Answer) 
{ 
    if (userGuess > Answer) 

     cout << " -----> 1 \n"; 
    else 
     cout << " -----> -1 \n"; 
} 
else 
{ 
    cout << " -----> 0 \n"; 
    cout << " Good JOb, you have guessed the right number. \n"; 

    system("pause"); 
    return 0; 
} 
} 
+1

歡迎計算器!這是很多代碼......你可以縮小到[MCVE](http://www.stackoverflow.com/help/mcve)嗎?有時候,這樣做的行爲會使你的答案顯而易見,最起碼,它使我們更容易找到答案。 :) – CodeMouse92

+0

^一個關鍵的技能,你將需要學習反正(「調試」!) –

+0

'pointer = new int [guessNum];'每次分配一個元素的數組。後面的兩個'for'循環用最近的猜測填充該數組,然後顯示它。在這裏有一個元素的數組是沒有意義的。 – Logicrat

回答

0

查看更改,不要複製和粘貼。

  1. 要訪問指針數組成員,可以使用index []。
  2. 你已經在for循環,所以你不想創建另一個for循環獲取輸入,這將是3 x 3的數據請求。
  3. 您正在將數據存儲到int中,而不是存儲到您使用指針創建的數組中。
  4. 指針不是一個很好的名字,當它是一個猜測數組時。嘗試調用它更可讀的東西。
  5. 一旦運行,如果用戶贏了,它仍然會觸發丟失的代碼。您需要按照您設置的方式訪問該代碼之前進行分解。我創建了一個bool,並且改變了你的reviewguess方法來返回一個bool,無論他們是否贏了。在退出時爆發。注意休息的雙重用途;它在一個開關中的for循環中。你必須跳出foor loop,然後跳出開關。你不能只是把它放在for循環之後,因爲如果他們贏了,他們會不斷詢問輸入,然後報告他們在最後贏了。
  6. 請注意使用全局常量來跟蹤有多少猜測。你在所有地方聲明和初始化相同的變量。在開始時用類型關鍵字初始化一次,如果你需要循環中的變量,在循環前初始化它,所以你不要試圖重新初始化每次迭代。
  7. 邏輯被顛倒過高或低與reviewGuess
  8. 不是一個糟糕的開始,保持你的頭,我希望我能幫助你! 這是我第一次回答問題。

代碼:

#include "stdafx.h" 
#include <iostream> 
#include <time.h> 
using namespace std; 


void arrayTable(int[]); 
bool reviewGuess(int, int); 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    const int MAX_NUMBER_GUESSES = 3;// Declares how many guesses the user can take. 
    int currentGuessNum = 0; 
    int userGuess = 1; 
    int Answer; 
    char Choice; 
    int x; 
    int *arrGuesses = NULL; 
    bool gameWon = false; 

    srand(time(NULL)); 
     //Gives a random number ranging from 1 to 20 
    do { 
     Answer = rand() % 20 + 1; //Gives a random number ranging from 1 to 20 
         //you want this in the loop otherwise you always have same answer 

     cout << "   ____________________________________________________\n"; 
     cout << "   |             |\n"; 
     cout << "   | I want to play a game       |\n"; // Displays a welcome message 
     cout << "   | I am thinking of a number between 1 through 20. |\n"; 
     cout << "   | Can you guess the number in less than " << MAX_NUMBER_GUESSES << " tries ? |\n"; 
     cout << "   |   Press '1' to play.     |\n"; 
     cout << "   |  Press '2' if you want to EXIT!!!   |\n"; 
     cout << "   |__________________________________________________|\n"; 
     cin >> x; 
     switch (x) 
     { 
     case 1: 
      userGuess = 1; 
      arrGuesses = new int[MAX_NUMBER_GUESSES]; 
      for (int i = 0; i < MAX_NUMBER_GUESSES; i++) //Counter, for loop, stops when condition is met. 
      { 
       cout << " This is guess# " << userGuess << " : " << endl; //Takes the user input 
       cin >> arrGuesses[i]; 
       cout << "Here is a list of your guesses" << endl; 

       for (int i = 0; i < userGuess; i++)    // Display the user guesses. 
       { 

        cout << " Guess# " << i+1 << " is " << arrGuesses[i] << endl; 
       } 

       gameWon = reviewGuess(Answer, arrGuesses[i]); //Calls the function 
       if (gameWon) 
       { 
        break; 
       } 
      userGuess++; 
      } 
      if (gameWon) 
      { 
       break; 
      } 
      cout << " ------------------>    <----------------------\n"; 
      cout << " ------------------> You Lost!!! <----------------------\n"; 
      cout << " ------------------>    <----------------------\n"; 
      cout << " You have exceeded the amount of guesses you were given.\n"; 
      cout << " The correct Answer is : " << Answer << endl; 

      break; 
     case 2: 
      cout << "-------------->Thank You.\n"; 
      cout << "-------------->Have a good day.\n"; 

      break; 


      system("pause"); 
      return 0; 
     } 


     cout << "would you like to play again (y/n)?"; 
     cin >> Choice; 
    } while (Choice == 'y'); 
    system("pause"); 
    return 0; 
} 

bool reviewGuess(int Answer, int userGuess) 
{ 

    if (userGuess != Answer) 
    { 
     if (userGuess < Answer) 

      cout << " -----> 1 \n"; 
     else 
      cout << " -1 <----- \n"; 
     return false; 


    } 
    else 
    { 
     cout << " -----> 0 \n"; 
     cout << " Good JOb, you have guessed the right number. \n"; 
     system("pause"); 
     return true; 

    } 
} 
+0

我剛剛意識到這段代碼實際上有內存泄漏;我們繼續聲明一個新的數組arrGuesses = new int [MAX_NUMBER_GUESSES];循環內;應該放在do {} while循環之上。每次迭代我們創建一個新的數組,放棄舊數組,並且該內存不被釋放。你需要在每次迭代之後使用delete [] arrGuesses來避免內存泄漏,或者正如我最初所說的那樣,將創建移動到do while循環之前。您可以保持重寫數組中的數據而不是創建新陣列 –

+0

非常感謝您的幫助。我相信我做錯了,當我真的需要一個數組時,我使用了一個指針。我對編程充滿熱情,這是一個學校任務。其中兩個要求是使用'const int'和'pointer'。在我提交之前,我已經在StackOverflow上提交了大約一週前的作業。它讓我感到困擾,程序沒有給我想要的結果,所以我在這裏提交了,所以我可以從你們所有的好人那裏獲得一些輸入。所以再一次,謝謝。 – Vutana

+0

在頂部我看到「Int currentGuessNumber = 0」,但我沒有看到它在代碼中的任何地方使用。我錯過了什麼,有沒有理由呢? – Vutana