Ending a while loop with changing variables

Hello, I have created a program that takes a wordlist and outputs it into a .sql database with the md5 of the word in question. I have a problem with being able to terminate the program once the wordlist has ended. As of now I have an if statement that terminates the program with the word 'xit' when it is found in the text file. I need to make it so that the program terminates whenever the variable is the same 2 times in a row. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include "md5.h"
#include "md5.cpp"
using namespace std;
string line;
string storage;
char finito = 'z';

int main()
{
	ifstream myfile ("wordlist.txt");
	ifstream sqlformat ("sqlformat.txt");
	ofstream md5file ("md5file.sql");
	getline (sqlformat,storage,finito);
	md5file << storage;
	if (myfile.is_open())
	{
		while (line!="xit")
		{
			getline (myfile,line);
			cout << line << endl;
			md5file << "('" << line << "', '" << md5(line) << "')," << endl;
		}

		if (line=="xit")
		{
			md5file << "('" << line << "', '" << md5(line) << "');" << endl;
		}

	}
	myfile.close();
}

In other words, how do I terminate the program if string 'line' has the same content 2 times in a row?
Last edited on

You need to keep a record of the previous line and compare it to the current line. Something like this. Don't forget to ensure they're different at the start before you start reading from file.

1
2
3
4
5
while (previous_line != current_line)
{
  previous_line = current_line;
  getline (myfile,current_line);
}

Thanks a bunch. That worked out great! A few small things I may need to iron out, however this solved it. If anyone needs an md5 database hit me up :-)

New code snippet:

1
2
3
4
5
6
7
8
9
10
11
12
while (line!=lineend)
		{
			line = lineend;
			getline (myfile,line);
			cout << line << endl;
			md5file << "('" << line << "', '" << md5(line) << "')," << endl;
		}

		if (line==lineend)
		{
			md5file << "('" << line << "', '" << md5(line) << "');" << endl;
		}
Last edited on
Topic archived. No new replies allowed.