When I attempt to compare two strings in C++ (one read in from a file, the other assigned a value), I get an incorrect output:
ifstream pgmFile; pgmFile.open(fileName); string temp; string comp = "P5"; for(int i = 0; i < 2; i++) { pgmFile >> noskipws >> temp; cout << temp; } if(temp == comp) {} else cout << "File does not contain 'P5'. Please provide the correct type of file." << endl; In theory this SHOULD return "true" that these two strings are correct. The output for temp = "P5", so I don't understand why it hits my else case every single time. Any help is much appreciated thanks!
tempdoes indeed contain "P5" and not, say, " P5". See if this provides any insight:cout << '|' << temp << '|' << temp.size();