[英]Run-Time Check Failure #2 - Stack around the variable 'numberchoices' was corrupted
I am having this terminal error every time I finish the debug program. 每次我完成调试程序时,都会遇到此终端错误。
What I am doing: 我在做什么:
[this program is a simple Lottery Numbers Comparison between user input numbers with the non-repeated random lottery numbers. [该程序是一个简单的彩票号码比较,用户输入号码与非重复随机彩票号码之间。 eg using what if it got 4 right of 6] 例如,如果得到4的6对,则使用该值]
but it turns out that the program is not working or at least, be stable. 但事实证明该程序无法正常运行,或者至少是稳定的。
Here's my code: 这是我的代码:
#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");
Please ? 请 ?
int numberchoices[] = { 0 };
for (int w = 1; w < 7; w++)
{
cout << "N" << char(163) << "mero #" << w << ": ";
cin >> numberchoices[w];
} // user numbers
You're declaring an array of size 1 and then you use it up to position 6 ? 您要声明一个大小为1的数组,然后将其使用到位置6?
I am having this terminal error every time I finish the debug program. 每次我完成调试程序时,都会遇到此终端错误。
I'm surprised that you're not having a terminal error every time you start debug. 我很惊讶您每次启动调试时都没有终端错误。
The access of numberchoises
at positions from 1 to 6 are UB (Undefined Behavior). 在1到6位置上的numberchoises
的访问是UB(未定义行为)。 That is: all can happens. 那就是:一切皆有可能。
Solution: try with 解决方案:尝试
int numberchoices[7] = { }; // initialize all elements to zero!
Another point 另一点
if (numberchoices == nums)
not sure that you get what do you expect. 不确定您能得到什么期望。
Do you want compare the integer pointer corresponding to numberchoices
(a int[1]
, suggested int[7]
) with the one corresponding to nums
(a int[51]
) ? 你想将对应于整数指针numberchoices
(一int[1]
建议int[7]
与相对应的一个nums
(一int[51]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.