Dynamic containers with arrays. Help!!!

Im trying to create a function that searches my array for a specific string, and then displays that string and the other content associated with it. I'm basically searching for a keyword within an array that has multiple strings with in each element. If that makes sense?
Last edited on
What have you written so far?
This is what i have so far for the function. What my program does is that it grabs a weblog file that has ip address, the date the website was accessed, and the website name, and it stores that information in each element of a dynamic array. What i want this function to do is take in an ip address, search the array for the specific ip address, and then show all that matched with that ip address.



void weblog::lineSearch(std::string key)
{

std::cout << "Here are the items that match your search: " << std::endl;
for(int i = 0; i < size(); i++)
{

if (key == arr[i])
{
std::cout << arr[i] << std::endl;
}

}

}
It's a very bad idea to have an array structured like that. Instead, you should make a class/struct to store your data, and then have the array contain instances of that class/struct instead.
I wish i could, but this is what was instructed by my teacher. I figured it out though, but now I have ran into another problem and maybe you could help me: at the end of my program, when it runs, the program breaks with an error.

Heres the error message:

Unhandled exception at 0x00C388B6 in Project 2.exe: 0xC0000005: Access violation reading location 0x01463000.

I think its because my array is exceeding its boundaries.

Here is what my function looks like now:

void weblog::lineSearch(std::string key)
{
std::string ip;
int length = key.length();
bool IsIpSame = false;
int i = 0;
std::cout << "Here are the items that match your search: " << std::endl;

while (IsIpSame || i < size() - 2)
{

ip = arr[i].substr(0, length);

if (key == ip)
{
std::cout << key << std::endl << std::endl;
std::cout << arr[i].substr(length + 1) << std::endl;
std::cout << std::endl;
IsIpSame = true;
}
if (i == size())
{
std::cout << "None Found" << std::endl;
}
i++;
}

}
Topic archived. No new replies allowed.