Hi all,
This is probably quite a simple problem but when I run the following code (shown below) to compare two files, File.txt and dic.txt. I get the following output:
If they are the same I get
If the files are different I get
I can't work out why the "They are the same" is getting printed twice tho. Any help would be appreciated.
Oh im running Debian and using the following command to compile.
This is probably quite a simple problem but when I run the following code (shown below) to compare two files, File.txt and dic.txt. I get the following output:
If they are the same I get
They are the same
They are the same
If the files are different I get
They are not the same
They are the same
I can't work out why the "They are the same" is getting printed twice tho. Any help would be appreciated.
Oh im running Debian and using the following command to compile.
g++ -Wall -W -pedantic compare.cpp -o compare
Code:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main( )
{
ifstream in("File.txt"); //open input file File.txt
ifstream in2("dic.txt"); //open input file dic.txt
while (!in.eof()) //continue until you get to the of in
while (!in2.eof()) //continue until you get til the end of in2
{
string (line);
string (line2);
getline(in,line); //get lines
getline(in2,line2); //get lines
{
if(line == line2) //are the strings equal?
cout << " They are the same " << endl;
else //the strings are not equal
cout << " They are not the same " << endl;
}
}
in.close(); //close the input file in
in2.close(); //close the input file in2
return (0); //finish program
}