string::compare is not working

I need to do some breathing exercises. I have no idea why it is saying these two strings are not identical. I have an array filled with movie titles and I am comparing them to a string in a file. If the result of the compare == 0, I want it to say "FINALLY IT WORKS" because I know the strings are identical. I don't know what is going on. Please help. Also, please refrain from telling me other reasons why my program is taking up too much memory or is inefficient, etc. It is far from complete but I am stuck here. You will be my hero tonight if you solve this simple question. Thank you!

Header file:
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

class Video
{
private:

	string title;
	int copies;
	string stars;

public:
	void setData(ifstream& fin, string array1[]);
	void printArray(string array1[]);
	void print();
	void adjustCopies();
	bool compareVideo(string array1[], ifstream& fin2, string title, char& temp);

};



void Video::printArray(string array1[])
{
	
	cout << "Video Title" << right << setw(69) << "Copies available to rent" << '\n' << '\n';

	for (int i = 0; i < 30; i+=3)
	{
		cout << left << setw(50) << setfill('-') << array1[i].append(" ");
		cout << setfill('-') << right << setw(6) << ' ' << array1[i + 1] << endl;
		cout << array1[i + 2] << '\n' << endl;

	}
}

void Video::setData(ifstream& fin, string array1[])
{
	for (int i = 0; i < 30; i++)
		getline(fin, array1[i], '\n');
}

bool Video::compareVideo(string array1[], ifstream& fin2, string title, char& temp)
{
	fin2.get(temp);
	getline(fin2, title, '\n');
	cout << title << endl;
	string x = "Harry Potter and the Sorcerer's Stone (2001)";
	for (int i = 0; i < 30; i++)
		if (x.compare(array1[i]) == 0)
			cout << "FINALLY IT WORKS";
		else
		{
			cout << x << endl;
			cout << array1[i] << endl;
			cout << title;
			return(true);
		}
}


source file:
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
36
37
38
#include "Header.h"

int main()
{
	ifstream fin("video_inventory.txt");
	ifstream fin2("video_rental_transactions.txt");
        ofstream fout("output.txt");
	string title = "";
	char temp = ' ';
	string x = "Harry Potter and the Sorcerer's Stone (2001)";
	string y = "Harry Potter and the Sorcerer's Stone (2001)";
	string videoObjects[30];
	
	Video test3;
	



	
	test3.setData(fin, videoObjects);
	test3.printArray(videoObjects);
	cout << endl << endl;

	test3.compareVideo(videoObjects, fin2, title, temp);
	fout << endl << videoObjects[0] << endl;
	fout << y << endl;

	if (videoObjects[0].compare(y) == 0)
		fout << "FINALLY IT WORKS";
	else
		fout << "this is not right" << endl;
		



	std::cin.get();
	return(0);
}


This is the output from the last few lines above, which I switched to an output file to show you.
Harry Potter and the Sorcerer's Stone (2001) 
Harry Potter and the Sorcerer's Stone (2001)
this is not right


All you need to know is the first object in the array videoObjects[0] = "Harry Potter and the Sorcerer's Stone (2001)". I have tested it over and over, and in multiple ways. I made it output the strings it is comparing, and THEY ARE IDENTICAL, yet is it saying they are not. My grade in this class is about to drop from an A to a C if I don't figure this out tonight. Yes, I should have started earlier but I am usually very good at these things and the homework is usually not a problem. I took today off work so I could write the program and I've been at it for over 10 hours. Thank you again.
Last edited on
How is the data in the files? Can you post a few lines of each?
How about I post them in their entirety.
"video_inventory.txt"
Harry Potter and the Sorcerer's Stone (2001)
20
Daniel Radcliffe, Rupert Grint
The Man (2005)
25
Samuel L. Jackson, Eugene Levy
Dogma
5
Ben Affleck, Matt Damon
Bubblegum Crisis Tokyo 2040
20
Michiko Nagasaki, Sumiko Ishikawa
Winged Migration (2001)
10
Jacques Perrin
The Exorcism of Emily Rose (2005)
3
Laura Linney, Tom Wilkinson
Troy (2004)
7
Brad Pitt, Eric Bana
Talk to Her (2002)
2
Javier Camara, Dario Grandinetti


"video_rental_transactions.txt"
BHarry Potter and the Sorcerer's Stone (2001)
RTalk to Her (2002)
SIn good Company (2005)
SThe Man (2005)
BBubblegum Crisis Tokyo 2040
RWinged Migration (2001)
SThe Exorcism of Emily Rose (2005)
RThe Exorcism of Emily Rose (2005)
BTroy (2004)
BTroy (2004)
BHarry Potter and the Sorcerer's Stone (2001)
BHarry Potter and the Sorcerer's Stone (2001)
BHarry Potter and the Sorcerer's Stone (2001)
BHarry Potter and the Sorcerer's Stone (2001)
BTalk to Her (2002)
BTalk to Her (2002)
SBubblegum Crisis Tokyo 2040


I used fin2.get() to get rid of the first char from the latter file (I will need to use it later, but for the purpose of compare, I needed to remove it).
Last edited on
The data read in by the file seems to have an extra space at the end. I can see it in the output file that's generated. I also threw in a couple of cout statements to check the length of the two strings you're comparing. Off by 1.

length of array videoobjects[0] is 45
length of string y is 44
I 100% believe you and you are my hero tonight, but how could this be? I erased all spaces after the end of the strings. If you look at both the input files, there are no spaces and I used getline with '\n' as the delimiter both times. Why is one longer than the other?
Last edited on
That being said, now I have to somehow erase a piece of whitespace every time I compare them. A whitespace that is non-existent in the input files, but somehow generated during the function getline of one, but not the other.
Last edited on
For now, I'm going to assume I've found an anomaly in string::compare, and I will instead use string::length so I can subtract that invisible char and call them even. Obviously this is not practical as a real-world solution, but I have to move on with the assignment. Thank you very much, wildblue. You are a lifesaver.
Glad you got it worked out. I'm not sure right now why the extra space. I don't think it has anything to do with compare - I printed out the lengths before the compare was called.

If you can use c++ 11, pop_back() on the string will take off the last element.
Thanks again. I tried using pop_back(), compare does not like it. Very clever idea, though.

Edit:

Actually, if I use the pop_back() function on the string in the array before I compare them, it works; it just won't accept it as part of the compare argument. Good to know ;) My word, thank you so much. If I become a millionaire, I'll send you $5.
Last edited on
Topic archived. No new replies allowed.