繁体   English   中英

运行时检查失败#2-变量'numberchoices'周围的堆栈已损坏

[英]Run-Time Check Failure #2 - Stack around the variable 'numberchoices' was corrupted

每次我完成调试程序时,都会遇到此终端错误。

我在做什么:

[该程序是一个简单的彩票号码比较,用户输入号码与非重复随机彩票号码之间。 例如,如果得到4的6对,则使用该值]

但事实证明该程序无法正常运行,或者至少是稳定的。

这是我的代码:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <time.h>
#include <ctime>
#include <algorithm>
using namespace std;

int main()

{
cout << "[La Loteria Electronica]\n";
cout << "Escoge 6 n" << char(163) << "meros del (1 al 49): \n";
int numberchoices[] = { 0 };

for (int w = 1; w < 7; w++)
{
    cout << "N" << char(163) << "mero #" << w << ": ";
    cin >> numberchoices[w];
} // user numbers



    //lottery numbers
    int i, j, k, nums[51];
    srand((int)time(0));
    for (i = 1; i < 50; i++) nums[i] = i;
    for (i = 1; i < 50; i++)
    {
        j = (rand() % 49) + 1;
        k = nums[i]; nums[i] = nums[j]; nums[j] = k;
    }
    cout << "The lottery numbers are:  ";
    for (i = 1; i < 7; i++) cout << nums[i] << " ";

    if (numberchoices[i] = nums[i])
    {
        cout << "gud\n";
    }

    if (numberchoices == nums)
    {
        cout << "gud 2";
    }
  /**/




cout << "\n\n";
system("pause");

请 ?

int numberchoices[] = { 0 };

for (int w = 1; w < 7; w++)
{
    cout << "N" << char(163) << "mero #" << w << ": ";
    cin >> numberchoices[w];
} // user numbers

您要声明一个大小为1的数组,然后将其使用到位置6?

每次我完成调试程序时,都会遇到此终端错误。

我很惊讶您每次启动调试时都没有终端错误。

在1到6位置上的numberchoises的访问是UB(未定义行为)。 那就是:一切皆有可能。

解决方案:尝试

int numberchoices[7] = { }; // initialize all elements to zero!

另一点

if (numberchoices == nums)

不确定您能得到什么期望。

你想将对应于整数指针numberchoices (一int[1]建议int[7]与相对应的一个nums (一int[51]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM