C++在检查argv值时未经手的异常
有人可以告诉我为什么这个代码在10分钟前有效,但现在一直在失败?
我不断遇到异常错误。在调试菜单中,我将进入32和12.5。
每次我尝试检查I> 0;
bool CheckArgInputs(char* inputs[], int numInputs)
{
const char validChars[] = ".,+-eE0123456789";
bool tester = false;
for (int i = 0; *inputs[i] != 0; i++)
{
tester = false;
for (int j = 0; j < sizeof(validChars); j++)
{
if (*inputs[i] == validChars[j])
{
tester = true;
}
}
if (tester == false)
return false;
//else
// cout << "Good Input" << endl;
}
}
int main(int argc, char* argv[])
{
bool validInput = true;
for (int i = 1; i < argc; i++)
{
validInput = CheckArgInputs(&argv[i], argc);
if (validInput == false)
{
cout << "X" << endl;
return 0;
}
}
return 0;
}
Can someone tell me why this code worked 10 minutes ago but keeps failing now?
I keep getting unhandled exception error. In the debug menu I am entering 32 and 12.5.
The code fails each time I try to check i > 0;
bool CheckArgInputs(char* inputs[], int numInputs)
{
const char validChars[] = ".,+-eE0123456789";
bool tester = false;
for (int i = 0; *inputs[i] != 0; i++)
{
tester = false;
for (int j = 0; j < sizeof(validChars); j++)
{
if (*inputs[i] == validChars[j])
{
tester = true;
}
}
if (tester == false)
return false;
//else
// cout << "Good Input" << endl;
}
}
int main(int argc, char* argv[])
{
bool validInput = true;
for (int i = 1; i < argc; i++)
{
validInput = CheckArgInputs(&argv[i], argc);
if (validInput == false)
{
cout << "X" << endl;
return 0;
}
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
checkArginputs()
函数被编码以表明其指向单个字符串的指针,但是main()
实际上是给它的指针细绳。然后,该函数无法正确编码以迭代该字符串的单个字符,实际上它是通过argv []
数组一次的一个字符串迭代的,直到它超出数组的范围。如果输入字符串有效,则实际上也没有
返回
'从chode> chodarginputs()
中使用任何内容。而是尝试一下:
Your
CheckArgInputs()
function is coded to act like it is being given a pointer to an individual string, butmain()
is actually giving it a pointer to a pointer to a string. And then the function is not coded correctly to iterate the individual characters of just that string, it is actually iterating through theargv[]
array one string at a time until it goes out of bounds of the array.You are also not actually
return
'ing anything fromCheckArgInputs()
if the input string were valid.Try this instead: