I have 2 strings: in
, which contains a link originally entered by the user (assume it to be "http://google.com/test"), and found
, which has already been found by an extraneous part of this program.
The goal of this function is to compare the section of string between http://
and /test
, ie: "google.com"
The problem I'm encountering is that the loop is not adding to the compare string, thus the later attempt to compare the 2 strings in the program at runtime.
bool isLinkExternal(string in, string found)
{
string compare = "";
bool isExternal = false;
//Get domain of original link
for (int i = 6; in[i] != '/'; i++)
{
compare += in[i];
}
system("pause");
//Compare domains of original and found links
if (found.compare(7,compare.length(),compare) != 0)
isExternal = true;
return isExternal;
}
The error:
An unhandled开发者_JAVA技巧 exception of type 'System.Runtime.InteropServices.SEHException' occurred in ParseLinks.exe
The line of code it points to:
if (found.compare(7,compare.length(),compare) == 0)
Fixed code (working):
bool isLinkExternal(string in, string found)
{
const int len = found.length();
int slashcount = 0;
string comp;
//Parse found link
for (int i = 0; i != len; i++)
{
//Increment when slash found
if (found[i] == '/')
{
slashcount++;
}
if (slashcount < 3)
{
comp += found[i];
}
}
//Compare domains of original and found links
if (in.compare(0,comp.length(),comp) == 0)
return false;
else
return true;
}
(int i = 6; in[i] != '/'; i++)
{
compare += in[i];
}
Did you mean this?
for (int i = 6; in[i] != '/'; i++)
{
compare += in[i];
}
At present, your string does not have seven characters in it, so your compare
is invalid. In fact, you will need to add some bounds checking there in any case.
Also, the exception text implies that you're actually writing C++/CLI, not C++. Am I right?
Does this work better:
for (int i = 7; in[i] != '/'; i++)
{
compare += in[i];
}
精彩评论