2016-07-05 175 views
0

我在玩遊戲時獲得了158,1000和140的貨幣輸出。 我輸入的原始金額爲100. 運行時,用戶輸入的總金額和金額未正確顯示。 有時,輸入的總數和金額顯示正確,應該注意。 但並非總是如此,這是一個問題。 我找不到一些邏輯錯誤。幫幫我?C++邏輯錯誤

#include <iostream> 
#include <cstdlib> 
#include <ctime> 
#include <string> 
#include <stdlib.h> 
using namespace std; 

void switchStatementsCalculations (int &slot1, int &slot2, int &slot3, string cherries, string 
            oranges, string plums, string bells, string melons, string bars); 

void calculateAmountEarnedByPlaying (double &money, int slot1, int slot2, int slot3, 
            double &total); 

int main() 
{ 
    int slot1; 
    int slot2; 
    int slot3; 

    double money=0; 
    double total=0; 
    double amountOfMoneyEnterd=0; 
    int count; 

    string cherries = "cherries"; 
    string oranges = "oranges"; 
    string plums = "plums"; 
    string bells = "bells"; 
    string melons = "melons"; 
    string bars = "bars"; 
    string doAgain; 

    cout << "We are going to be playing a slot machine game today." << endl; 
    srand(time(0)); 

    do 
    { 

     cout << "Please enter the amount of money you'd like to insert into the slot machine. We will pull the lever for you." << endl; 
     cin >> money; 
     cout << "You put in $" << money << endl; 




     slot1=rand()%6+1; 
     slot2=rand()%6+1; 
     slot3=rand()%6+1; 

     switchStatementsCalculations(slot1, slot2, slot3, cherries, oranges, plums, bells, melons, bars); 

     calculateAmountEarnedByPlaying(money, slot1, slot2, slot3, total); 

     amountOfMoneyEnterd=(amountOfMoneyEnterd+money); 





     cout << "Would you like to play again? Please type yes if so." << endl; 
     cin >> doAgain; 
     if(doAgain!= "yes") 
     { 
      cout << "The total amount of money you put in the slot machine is " << amountOfMoneyEnterd << endl; 
      cout << "The total amount of money you won is $" << total << endl; 
     } 

    } 
    while(doAgain=="yes"); 


    system("Pause"); 
    return 0; 
} 

void switchStatementsCalculations(int &slot1, int &slot2, int &slot3, string cherries, string 
            oranges, string plums, string bells, string melons, string bars) 
{ 
    switch (slot1) 
    { 
    case 1: 
     cout << "You got " << cherries << endl; 
    case 2: 
     cout << "You got " << oranges << endl; 
     break; 
    case 3: 
     cout << "You got " << plums << endl; 
     break; 
    case 4: 
     cout << "You got " << bells << endl; 
     break; 
    case 5: 
     cout << "You got " << melons << endl; 
     break; 
    case 6: 
     cout << "You got " << bars << endl; 
    } 

    switch (slot2) 
    { 
    case 1: 
     cout << "You got " << cherries << endl; 
     break; 
    case 2: 
     cout << "You got " << oranges << endl; 
     break; 
    case 3: 
     cout << "You got " << plums << endl; 
     break; 
    case 4: 
     cout << "You got " << bells << endl; 
     break; 
    case 5: 
     cout << "You got " << melons << endl; 
     break; 
    case 6: 
     cout << "You got " << bars << endl; 

    } 

    switch (slot3) 
    { 
    case 1: 
     cout << "You got " << cherries << endl; 
     break; 
    case 2: 
     cout << "You got " << oranges << endl; 
     break; 
    case 3: 
     cout << "You got " << plums << endl; 
     break; 
    case 4: 
     cout << "You got " << bells << endl; 
     break; 
    case 5: 
     cout << "You got " << melons << endl; 
     break; 
    case 6: 
     cout << "You got " << bars << endl; 

    } 
} 

void calculateAmountEarnedByPlaying(double &money, int slot1, int slot2, int slot3, double &total) 

{ 
    double won=0; 

    if(slot1==slot2 || slot1==slot3 || slot2==slot3) 
    { 
     cout << "Congratulations! You won." << endl; 
     won=(money * 2); 
     cout << "You won " << won << endl; 
    } 


    else if ((slot1==slot2 && slot1==slot3) || (slot2==slot1 && slot2==slot3) || (slot3==slot1 && slot3==slot2)) 
    { 
     cout << "Congratulations! You won." << endl; 
     won=(money*3); 
     cout << "You won " << won << endl; 
    } 
    else 
    { 
     cout << "You didn't earn any money." << endl; 
    } 

    total=(total+won); 
} 
+7

您應該嘗試使用調試器逐步執行代碼。 – NathanOliver

+0

我用了一個在線的,但它沒有給我結果。我以爲我沒有一個,直到我手動添加了所有東西。 – Kikuo

+1

'srand(time(0));'這應該只在外部和循環之前完成,而不是在循環中重複完成。此外,現在是學習數組的好時機,因爲你有相同的代碼重複6次以上。 – PaulMcKenzie

回答

0

錯誤之一是這個在您的calculateAmountEarnedByPlaying功能:

double won;

你沒有初始化這個變量,因此它包含一個不確定的值。

只有當您贏得了某項纔會被設置,但如果您沒有獲勝則不會設置。然後,你這樣做你的函數的末尾:

total = (total + won);

如果won沒有初始化,然後total也將被設置爲一個不確定的值(或出現未定義行爲)。如果你訪問一個未初始化的變量類似這樣的

double won = 0;

大多數編譯器發出警告:

won變量應該被初始化爲0。請檢查您是否有警告,以檢測這些問題。


的另一個問題是,你忘了你的開關語句break

switch (slot1) 
{  
    case 1: 
     cout << "You got " << cherries << endl; // no break statement 
    case 2: 
     cout << "You got " << oranges << endl; 
    break; 

如果slot11,將打印兩組輸出的情況下爲1和2的情況下

如果您使用數組而不是6個單獨的變量和6個單獨的輸出行,則這些都不是必需的:

std::string slot_items[] = {"cherries", "oranges", "plums", "bells", "melons", "bars"}; 
    //... 
    int slots[3]; 
    //... 
    for (int i = 0; i < 3; ++i) 
     slots[i] = rand()%6+1; 
    //... 
    // inside the function... 
    //... 
    for (int i = 0; i < 3; ++i) 
     cout << "You got " << slot_items[slots[i]] << endl; 

兩行for循環替換60行switch和case語句。

+0

那麼,我認爲這解決了我的問題。我將編輯並顯示我編輯的代碼。謝謝! 如果您看到其他內容,請留下您的評論! – Kikuo

+0

我還有一個問題。你是否看到它在我第一次插入貨幣時首先顯示四個水果,然後在我第二次插入貨幣時顯示兩個水果?我覺得這是我的代碼中的一個錯誤。因爲只有三臺老虎機,每次我投入金錢時,它不應該只顯示三個結果嗎? http://prntscr.com/bp9okf @PaulMcKenzie – Kikuo

+0

看看你的第一套'switch'語句 - 你忘記了case 1的break語句。 – PaulMcKenzie