C++ 变量周围的MS studio 17堆栈已损坏
嗨,我一直得到一个关于变量nhour被破坏的错误的堆栈,我不知道为什么。我正在尝试输入一个时间,并检查它,以确保它是一个良好的输入。使阵列变大或变小似乎不起作用。我还添加了检入,在得到错误之前,输出似乎可以找到C++ 变量周围的MS studio 17堆栈已损坏,c++,runtime-error,corrupt,C++,Runtime Error,Corrupt,嗨,我一直得到一个关于变量nhour被破坏的错误的堆栈,我不知道为什么。我正在尝试输入一个时间,并检查它,以确保它是一个良好的输入。使阵列变大或变小似乎不起作用。我还添加了检入,在得到错误之前,输出似乎可以找到 void DEPARTURETIME(string& szdepartureTime) { bool berror; char cdeparture[9]; int nhour[2], nminute[2], nhourValue, nminuteValu
void DEPARTURETIME(string& szdepartureTime)
{
bool berror;
char cdeparture[9];
int nhour[2],
nminute[2],
nhourValue,
nminuteValue;
nhour[0] = 0;
do
{
berror = false;
system("CLS");
cout << "Enter departure time. (HH:DDAM/PM)" << endl;
cin >> cdeparture;
cin.ignore();
berror = ERRORCHECK();
for (int i = 0; i < 2 && berror != true; i++)
{
nhour[i] = CONVTOINT(cdeparture[i]);
}
//Beginning of testing hour for errors
if (nhour[0] > 1 && berror != true)
{
cout << "Error, can not exceed 12 hours." << endl;
system("pause");
berror = true;
}
else if (nhour[0] == 1)
{
nhour[0] *= 10;
}
cout << nhour[0] << nhour[1] << endl;
nhourValue = nhour[0] + nhour[1];
cout << nhourValue << endl;
if (nhourValue > 12 || nhourValue <= 0 && berror != true)
{
cout << "Error, can not exceed 12 hours and can not be
zero." << endl;
system("pause");
berror = true;
}
//End of testing hour for errors
//Beginning of testing for a colon
if (cdeparture[2] != ':' && berror != true)
{
cout << "Error, you need a colon inbetween hours and minutes." << endl;
system("pause");
berror = true;
}
//End of testing for a colon
for (int i = 3; i < 5 && berror != true; i++)
{
nminute[i] = CONVTOINT(cdeparture[i]);
}
//Beginning of testing for minute errors
if (nminute[3] > 6 && berror != true)
{
cout << "Error, minutes can not exceed 60." << endl;
system("pause");
berror = true;
}
else if (nminute[3] > 0 || berror != true)
{
nminute[3] *= 10;
}
nminuteValue = nminute[3] + nminute[4];
if (nminuteValue > 60 && berror != true)
{
cout << "Error, minutes can not exceed 60." << endl;
system("pause");
berror = true;
}
//End of testing for minute errors
cdeparture[5] = toupper(cdeparture[5]);
cdeparture[6] = toupper(cdeparture[6]);
//Beginning of testing for AM and PM errors
if (cdeparture[5] != 'A' && cdeparture[5] != 'P' || cdeparture[6] != 'M' && berror != true)
{
cout << "Error, must have AM or PM." << endl;
system("pause");
berror = true;
}
} while (berror != false);
//End of testing for AM and PM errors
for (int i = 0; i < 7; i++)
{
szdepartureTime += cdeparture[i];
}
return;
void DEPARTURETIME(string&szdepartureTime)
{
布尔·贝罗;
char-cdeparture[9];
int nhour[2],
n分钟[2],
nhourValue,
n分钟值;
nhour[0]=0;
做
{
berror=假;
系统(“CLS”);
不能分离;
cin.ignore();
berror=ERRORCHECK();
对于(int i=0;i<2&&berror!=true;i++)
{
nhour[i]=便利(cdeparture[i]);
}
//错误测试时间的开始
如果(nhour[0]>1&&berror!=true)
{
您可以将nminute
声明为一个包含两个元素的数组,但当您使用它时,您正在访问元素3和4(这要求数组包含5个元素)。由于您正在访问数组的末尾,因此会遇到未定义的行为。在这种情况下,编译器插入了检测此问题的代码,并正在告诉您有关此问题的信息
若要修复此问题,请将代码中使用的nminute
上的订阅更改为仅访问元素0和1。已修复谢谢,抱歉,我没有看到这样一个简单的错误
int CONVTOINT(char cchar)
{
int value = cchar - '0';
if (value < 0 || value > 9)
{
cout << "Error, Convertion failed" << endl;
system("pause");
return 0;
}
return value;
}