compares

...
Last edited on
You botched up your code tags. Use them like this:

[code]
your code here
[/code]

C headers in C++ programs don't end in .h. Instead, they begin with a c. So instead of string.h it's cstring.

And you don't need (and should almost never need) <cstdio> since <iostream> is used instead.

Why are you opening text files in binary mode?

stricmp is non-standard.

And all you've said is "i have a problem" and "I have no idea how to make the output the way I want", which is not a very specific question.
Last edited on
Hello student0312,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Any reason you are opening a text file as binary?

If you are going to use "std::string" and "std::getline" you need the header file "<string>". "string.h" is a C header file and not the same as the C++ "string".

Also "stdio.h" and "string.h" should be "cstdio" and "cstring" to use the proper C++ version of the C header files. Also you should not need either for a C++ program.

while (!file1.eof()) does not work the way that you are think of. You are doing the read after you check for "eof", so "c1", (and "c2"), will end up 1 larger than it should be.

A thought here, instead of reading the file every time you need something consider reading the file into a vector or an array. This way you can use the array instead of reading the files each time you need something.

When I did get the program to run I found that you are reading each line of the file and comparing the two strings. This is not what you want. The idea would be to read line 1 of "file1" and compare it to each line of "file2". This is where the vector or array would work better. Also the number of lines in each file would not have to be the same.

That is what I see for the moment. The problem is that I do not know what you know. Can you use a vector?

Andy
What is the basis of the compare? By line, by word? This can be done simply using a set. Consider for a word compare:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <set>
#include <fstream>
#include <string>
#include <iostream>

int main()
{
	std::ifstream f1("file1.txt");
	std::ifstream f2("file2.txt");

	if (!f1 || !f2)
		return (std::cout << "Cannot open files\n"), 1;

	std::set<std::string> wrds;

	for (std::string str; f1 >> str; wrds.insert(str));
	for (std::string str; f2 >> str; )
		if (wrds.find(str) != wrds.end())
			std::cout << str << '\n';
}


which for the given files displays:


green


as required.

If a line compare is required, then just replace the stream extraction with stream getline().
Last edited on
Topic archived. No new replies allowed.