так что это весь мой код Проблема в том, что, когда я пытаюсь сбросить несколько фишек, а затем распечатать общий выигрыш и средний выигрыш на фишку, все, что я получаю, равняется 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 за общее и среднее вознаграждение.
Других решений пока нет …