while loop and checking for uniqueness

http://www.cplusplus.com/forum/beginner/98118/

that was an earlier post that I made, and I fixed the suggestions and my original problem, however, my program still isn't running successfully. I know the problem is in a while loop specifically this one in my GetIPs function

1
2
3
4
5
6
7
8
9
10
11
12
13
if(sortedWeblog.is_open())
	{
		int i = 0;
		while(std::getline(sortedWeblog, line))
		{
			IP = line.substr(0,14);
			if(IP != IPaddress[i-1])
			{
				IPaddress.push_back(IP);
			}
			i++;
		}
	}


I believe the problem has to do with the variable "i"

I have tried altering my loop countless times but I can't seem to fix it. Any help would be appreciated!
Try the following condition

if( i == 0 || IP != IPaddress[i-1] )
I am fairly new to programming... could you explain what

if( i == o || IP != IPaddress[i-1])

would do?

also I tried changing the whole if statement to
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if(sortedWeblog.is_open())
	{
		std::getline(sortedWeblog, firstline);
		std::string firstlineIP = firstline.substr(0,firstline.find(" "));
		IPaddress.push_back(firstlineIP);
		
		std::cout << IPaddress[0] << std::endl;
		system ("pause");

		for(std::size_t i = 1; i < vectorsize; i++)
		{
			std::string lineIP = IPaddress[i].substr(0, IPaddress[i].find(" "));
			if(lineIP != IPaddress[i-1])
			{
				IPaddress.push_back(lineIP);
				std::cout << IPaddress[i] << std::endl;
				system ("pause");
			}
		}	
	}


which runs through a vector i created instead of the file and its individual lines. it isn't working either.
I am sorry but I will not explain the code because it looks very strange that you are using standard containers and at the same time do not know elementary operators.
I think you have to read some book on C++ for beginners.
It must be the way that my C++ class is being taught. I would just like to know what the "||" part of your if statement means.
|| is logical OR. The expression means push_back IP either when it is the first record (there is no yet any previous IP) or when the current IP is not equal to the previous IP.
Last edited on
Topic archived. No new replies allowed.