【发布时间】:2016-04-24 23:04:07
【问题描述】:
我是 C++ 新手,我正在尝试构建一个简单的程序,该程序通过用户输入继续生成随机左或右。我让程序正常工作,直到我添加到数组中以尝试存储每个项目,因为我必须尽快输出它们并且用户想要退出循环。该程序似乎编译得很好,但在运行时我收到“0x012B1CA9 处的未处理异常”任何帮助将不胜感激。
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int userSelection = 1;
const int MAX = '100';
int randNum(0);
int one (0);
int two (0);
int total(0);
int sel[MAX];
do
{
cout << "Press 1 to pick a side or 0 to quit: ";
cin >> userSelection;
for (int i = 1; i < MAX; i++)
{
srand(time(NULL));
sel[i] = 1 + (rand() % 2);
if (sel[i] == 1)
{
cout << "<<<--- Left" << endl;
one++;
total++;
}
else
{
cout << "Right --->>>" << endl;
two++;
total++;
}
}
} while (userSelection == 1);
cout << "Replaying Selections" << endl;
for (int j = 0; j < MAX; j++)
{
cout << sel[j] << endl;
}
cout << "Printing Statistics" << endl;
double total1 = ((one / total)*100);
double total2 = ((two / total)*100);
cout << "Left: " << one << "-" << "(" << total1 << "%)" << endl;
cout << "Right: " << two << "-" << "(" << total2 << "%)" << endl;
system("pause");
return 0;
};
【问题讨论】:
-
我想知道为什么你的编译器没有抱怨
const int MAX = '100'; -
我不确定我使用的是 Visual Studio 2015。语法应该是什么?
-
const int MAX = 100; -
另外,你的第一个 for 循环从 1 而不是 0 开始。
-
在调试器中单步执行代码向您展示了什么?