c++逻辑错误

C++ logic error

本文关键字:错误 c++      更新时间:2023-10-16

在我玩游戏时,我得到了158,1000和140的金钱输出。我最初投入的是100。运行时,总金额和用户输入的金额没有正确显示。有时,总金额和输入金额显示正确,应注意。但并非总是如此,这是个问题。有一些逻辑错误我弄不明白。帮助吗?

#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);
}

一个错误是在您的calculateAmountEarnedByPlaying函数:

double won;

您没有初始化这个变量,因此它包含一个不确定的值。

只有赢了才会设置,没赢就不设置。然后在函数末尾执行此操作:

total = (total + won);

如果won没有初始化,那么total也会被设置为一个不确定的值(或者发生未定义的行为)。

变量won应该初始化为0:

double won = 0;

如果像这样访问未初始化的变量,大多数编译器都会给出警告。请检查您是否打开了警告以检测这些问题。


另一个问题是您在switch语句中忘记了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语句。