[英]C++ unhandled Exception while checking ARGV values
Can someone tell me why this code worked 10 minutes ago but keeps failing now?有人能告诉我为什么这段代码在 10 分钟前有效但现在一直失败吗?
I keep getting unhandled exception error.我不断收到未处理的异常错误。 In the debug menu I am entering 32 and 12.5.在调试菜单中,我输入了 32 和 12.5。
The code fails each time I try to check i > 0;每次我尝试检查 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;
}
Your CheckArgInputs()
function is coded to act like it is being given a pointer to an individual string, but main()
is actually giving it a pointer to a pointer to a string.您的CheckArgInputs()
function 被编码为就像被赋予指向单个字符串的指针一样,但main()
实际上是给它一个指向字符串指针的指针。 And then the function is not coded correctly to iterate the individual characters of just that string, it is actually iterating through the argv[]
array one string at a time until it goes out of bounds of the array.然后 function 没有正确编码以迭代该字符串的各个字符,它实际上是一次迭代argv[]
数组一个字符串,直到它超出数组的范围。
You are also not actually return
'ing anything from CheckArgInputs()
if the input string were valid.如果输入字符串有效,您实际上也不会从CheckArgInputs()
return
任何内容。
Try this instead:试试这个:
bool CheckArgInput(const char* input)
{
const char validChars[] = ".,+-eE0123456789";
for (int i = 0; input[i] != 0; i++)
{
for (int j = 0; j < sizeof(validChars)-1; j++)
{
if (input[i] != validChars[j])
{
return false;
}
}
}
return true;
}
int main(int argc, char* argv[])
{
bool validInput = true;
for (int i = 1; i < argc; i++)
{
validInput = CheckArgInput(argv[i]);
if (!validInput)
{
cout << "X" << endl;
return 0;
}
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.