在c++中尝试在循环(plink0)中对变量求和
trying to sum a variable in a loop (plink0) in C++
这是我的全部代码。问题是,当我尝试放弃多个筹码,然后打印出总奖金和每个筹码的平均奖金时,我得到的都是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的原因之一。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 将数组的地址分配给变量并删除
- 为"adjacent"变量赋值时出现问题
- enum是C++中的宏变量还是整数变量
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 用C++中的一个变量定义一个常量
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 你能重载对象变量名本身返回的内容吗
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 尝试通过多个向量访问变量时,向量下标超出范围
- 试图让变量检查数组中的某些内容
- Cpp-Tuple使用带有变量的get
- 将包含C样式数组的对象初始化为成员变量(C++)
- 当vector是tje全局变量时,c++中vector的内存管理
- 通过多个头文件使用常量变量
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- OpenCL 数据并行求和到一个变量中
- 在c++中尝试在循环(plink0)中对变量求和