在c++中尝试在循环(plink0)中对变量求和

trying to sum a variable in a loop (plink0) in C++

本文关键字:变量 求和 plink0 循环 c++      更新时间:2023-10-16

这是我的全部代码。问题是,当我尝试放弃多个筹码,然后打印出总奖金和每个筹码的平均奖金时,我得到的都是0。我真的被困住了,对计算机科学和编程是一个全新的人,没有任何选择。我相信这很容易修复,也许我错过了什么或做错了什么。但我这辈子都想不出该怎么补救。请帮助!谢谢你! !

(我知道使用函数会更容易,但在这个赋值中我们被告知不要使用函数)

#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main()
{
    //input variables
    int menu_selection = 0;
    double slot_selection = 0;
    int number_of_chips_drop = 0;
    //output variables 
    double path_single_chip = 0;
    double reward_single = 0;
    double reward_total = 0;
    double reward_average = 0;
    //constant variables 
    const double REWARD_0 = 100.0;
    const double REWARD_1 = 500.0;
    const double REWARD_2 = 1000.0;
    const double REWARD_3 = 0.0;
    const double REWARD_4 = 10000.0;
    const double REWARD_5 = 0.0;
    const double REWARD_6 = 1000.0;
    const double REWARD_7 = 500.0;
    const double REWARD_8 = 100.0;
    //computation variables
    double position_chip = 0;
    double winning = 0;

    for (;;)
    {
        //menu display 
        cout << "Welcome to the Plinko Simulator!";
        cout << endl << endl;
        cout << "MENU: Please select one of the following options: ";
        cout << endl << endl;
        cout << "0 - Quit the program";
        cout << endl << endl;
        cout << "1 - Drop a single chip into one slot";
        cout << endl << endl;
        cout << "2 - Drop multiple chips into one slot";
        cout << endl << endl;
        cout << "Enter your selection now: ";
        cin >> menu_selection;
        cout << endl;
        while (menu_selection != 1 && menu_selection != 2 && menu_selection != 0) // error check menu
        {
            cout << "INVALID SELECTION Please enter 0, 1, or 2";
            cout << endl << endl;
            cout << "MENU: Please select one of the following options: ";
            cout << endl << endl;
            cout << "0 - Quit the program";
            cout << endl << endl;
            cout << "1 - Drop a single chip into one slot";
            cout << endl << endl;
            cout << "2 - Drop multiple chips into one slot";
            cout << endl << endl;
            cout << "Enter your selection now: ";
            cin >> menu_selection;
            cout << endl;
        }
        if (menu_selection == 0)
        {
            return 0;
        }
        if (menu_selection == 1) //simulate one chip drop
        {
            cout << "*** DROP SINGLE CHIP ***";
            cout << endl << endl;
            cout << "Which slot do you want to drop the chip in (0-8)? "; // this will be starting point 
            cin >> slot_selection;
            cout << endl;
            if (slot_selection >= 9 || slot_selection <= -1) // error check slot value
            {
                cout << "INVALID SLOT.";
                cout << endl << endl;
                cout << "MENU: Please select one of the following options: ";
                cout << endl << endl;
                cout << "0 - Quit the program";
                cout << endl << endl;
                cout << "1 - Drop a single chip into one slot";
                cout << endl << endl;
                cout << "2 - Drop multiple chips into one slot";
                cout << endl << endl;
                cout << "Enter your selection now: ";
                cin >> menu_selection;
                cout << endl;
            }
            cout << "*** DROPPING CHIP INTO SLOT " << slot_selection << " ***";
            cout << endl << endl;
            cout << "PATH: [";
            if (position_chip = slot_selection)
                for (int current_row= 1; current_row<= 12; current_row++)
                {
                    double plink = (rand() % 2) - 0.5;
                    position_chip = position_chip + plink;
                    if (current_row< 12)
                    {
                        cout << position_chip << fixed << setprecision(2) << " ";
                    }
                    if (current_row== 12)
                    {
                        cout << position_chip << fixed << setprecision(2) << "]";
                        cout << endl << endl;
                    }
                    if (position_chip > 8)
                    {
                        position_chip = position_chip - 1.0;
                    }
                    if (position_chip < 0)
                    {
                        position_chip = position_chip + 1.0;
                    }
                    if (current_row == 12 && position_chip == 0)
                    {
                        winning = REWARD_0;
                    }
                    else if (current_row == 12 && position_chip == 1)
                    {
                        winning = REWARD_1;
                    }
                    else if ( current_row == 12 && position_chip == 2)
                    {
                        winning = REWARD_2;
                    }
                    else if (current_row== 12 && position_chip == 3)
                    {
                        winning = REWARD_3;
                    }
                    else if (current_row== 12 && position_chip == 4)
                    {
                        winning = REWARD_4;
                    }
                    else if (current_row== 12 && position_chip == 5)
                    {
                        winning = REWARD_5;
                    }
                    else if (current_row== 12 && position_chip == 6)
                    {
                        winning = REWARD_6;
                    }
                    else if (current_row== 12 && position_chip == 7)
                    {
                        winning = REWARD_7;
                    }
                    else if (current_row== 12 && position_chip == 8)
                    {
                        winning = REWARD_8;
                    }
                }
            cout << "WINNINGS: $" << winning << endl << endl;
        }
        if (menu_selection == 2)
        {
            cout << "*** DROP MULTIPLE CHIPS ***"; 
            cout << endl << endl; 
            cout << "How many chips fo you want to drop (>0)? "; 
            cin >> number_of_chips_drop; 
            cout << endl; 
            cout << "Which slot do you want to drop the chip in (0-8)? "; 
            cin >> slot_selection;
            cout << endl; 
            if (slot_selection > 8 || slot_selection < 0)
                {
                cout << "INVALID SLOT.";
                cout << endl << endl;
                cout << "MENU: Please select one of the following options: ";
                cout << endl << endl;
                cout << "0 - Quit the program";
                cout << endl << endl;
                cout << "1 - Drop a single chip into one slot";
                cout << endl << endl;
                cout << "2 - Drop multiple chips into one slot";
                cout << endl << endl;
                cout << "Enter your selection now: ";
                cin >> menu_selection;
                cout << endl;
                }
            if (slot_selection >= 0 && slot_selection <= 8)
                {
                for (int current_chip = 0; current_chip < number_of_chips_drop; current_chip++)
                    {
                    for (int current_row = 1; current_row <= 12; current_row++)
                        {
                            position_chip = slot_selection;
                            double plink = (rand() % 2) - 0.5;
                            position_chip = position_chip + plink;
                                if (current_row == 12 && position_chip == 0)
                                {
                                    winning = REWARD_0;
                                }
                                else if (current_row == 12 && position_chip == 1)
                                {
                                    winning = REWARD_1;
                                }
                                else if (current_row == 12 && position_chip == 2)
                                {
                                    winning = REWARD_2;
                                }
                                else if (current_row == 12 && position_chip == 3)
                                {
                                    winning = REWARD_3;
                                }
                                else if (current_row== 12 && position_chip == 4)
                                {
                                    winning = REWARD_4;
                                }
                                else if (current_row== 12 && position_chip == 5)
                                {
                                    winning = REWARD_5;
                                }
                                else if (current_row== 12 && position_chip == 6)
                                {
                                    winning = REWARD_6;
                                }
                                else if (current_row== 12 && position_chip == 7)
                                {
                                    winning = REWARD_7;
                                }
                                if (current_row== 12 && position_chip == 8)
                                {
                                    winning = REWARD_8;
                                }
                        }
                    position_chip = slot_selection; 
                    }

                reward_average = reward_total / number_of_chips_drop; 
                cout << "Total Winnings on " << number_of_chips_drop << fixed << setprecision(2) << " chips: "; 
                cout << reward_total << endl << endl;
                cout << "Average winnings per chip: " << reward_average << fixed << setprecision(2);
                cout << endl << endl;

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

一个明显的问题是变量reward_total只在初始化时被设置,而不是在逻辑中。

我还没有调试这段代码,但我确信这至少是你得到的总奖励和平均奖励为0的原因之一。