Identical strings that aren't identical

I have two strings one was retrieved from an input file using getline(name) and the other is at a position in an array of structs, Struct[n].type.

I have output both strings (the struct at n = 0) to see if they are identical and indeed they are but if I try to use an if statement to compare them:
if(name == Struct[0].type) it doesn't return true. Is there another way I should be comparing them because that's the only thing I can think of. They are exactly identical. Thanks for the help.
name is a string, right?
http://cplusplus.com/reference/string/string/compare/

-Albatross
If you are using the std::string == operator, then the two strings are not identical. Check the sizes of them too, it could be that one or the other has some characters that aren't printed or are printed in a hard to see way (like '\0' or maybe '\n')
firedraco, I have checked for that and there are no additional spaces or extra characters. yes, name is the string name that I used in getline(); I've tried compare as well : if (name.compare(Stuct[n].type) == 0)
Last edited on
Ok so I just did string.length() on both and for some reason 'name = 0'. Why is that?

When I output 'name' it's IDENTICAL to struct[0].type. I'm so confused.
Please post the code where name and Struct are declared, defined, and populated.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <fstream>
#include <iomanip>
#include <iostream>
#include <climits>
#include <string>
#include <ctype.h>
using namespace std;

struct Protein {
	string Name;
	string Species;
	string Sequence;
}data, Datalist;

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
while (In) {

		getline(In, name, '\t');
		getline(In, species, '\t');
		getline(In, sequence);

		seqread++;
	}

	In.clear();
	In.close();
	In.open("sequences.txt");

	const int MAXLINES = 25;
	Protein Datalist[MAXLINES];

	for (numLoops = 0; numLoops < (seqread - 1); numLoops++) {
		getline(In, name, '\t');
		getline(In, species, '\t');
		getline(In, sequence);

		sequence = RLEdecode(sequence);

		data.Name = name;
		data.Species = species;
		data.Sequence = sequence;	

		Datalist[numLoops] = data;
	}
    loop continues....

Topic archived. No new replies allowed.