2016-07-05 143 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