【问题标题】:Unhandled exception at 0x012B1CA90x012B1CA9 处未处理的异常
【发布时间】: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 开始。
  • 在调试器中单步执行代码向您展示了什么?

标签: c++ arrays random


【解决方案1】:

您在这里有一个多字符常量...并且行为未按预期进行...

改变这一行

const int MAX = '100';

const int MAX = 100;

注意删除的单引号。

其次,我会建议您从 for 循环中删除 C 随机生成器的种子,因为如果您总是在播种后立即调用它,您可能会从 rand() 获得相同的值...

但最好使用C++'s random header的算法

这是您原始代码的更正版本....

#include <iostream>
#include <ctime>
using namespace std;

int main()
{

int userSelection = 1;
const int MAX = 100;     // <---changed
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;


    srand(time(NULL));    //< moved to here
    for (int i = 0; i < MAX; i++)     // <-- modified starting index
    {
        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;
};

【讨论】:

  • 他也是从 1 开始索引他的第一个循环,他可能想要更正。
  • 感谢您解决了错误。我注意到数组似乎没有存储所有数字。你知道为什么吗?
  • @kfsone 如果我的 rand 是 0-1 而不是 1-2 会更好吗?
  • @CameronAbel 我指的是这个:for (int i = 1; i &lt; MAX; i++)。这意味着您不会填充 sel (sel[0]) 的第一个元素。就我个人而言,我不会将 +1 添加到 rand 结果中,我会做出 0 左和 1 右。但这是意见而非政策:)
  • @kfsone 这是有道理的,并且会在我从 0 开始的重播部分中引起如下问题。但是这仍然无法正常工作。它似乎只向数组写入一项。
【解决方案2】:

我认为阅读更多关于 C 数据类型和声明的内容基本上是个好主意。你的错误:

const int MAX = '100' 应该是const int MAX = 100,不带任何引号。 C++ 进行从字符文字到int 的隐式转换。

【讨论】:

    猜你喜欢
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 2010-09-29
    相关资源
    最近更新 更多