I am trying to compare a vector of doubles to a string of numbers. In the code below I just copied a line from the txt file and put it in the vector, converted each number to a string and concantenated it. Then the program reads lines from the same txt files with the exact same format with the spacing. But, according to my code it cant find it.. anyone have any ideas? concanstring when printed is exactly the same as where i copied it from. If I cout newstring in the last loop before the if, it prints everything exactly like in the text file. which looks like:
5 0 4 0 2 6 0 1 5 1 4
-0.00021 -0.00321 0.00045 0.00089 0.00435 0.00065
1 5 8 3 0 1 4 8 9 7 2
and so on.
int main()
{
std::vector <int> momentum{ 5, 0 , 4 ,2 , 6 , 0 , 1 , 5 , 1 , 4 };
std::string newstring;
std::string concanstring;
std::ifstream datafile("thedata.txt");
for (int i = 0; i < momentum.size(); i++)
{
std::ostringstream newobj;
newobj << momentum[i];
concanstring += newobj.str();
concanstring += " ";
}
std::cout << concanstring;
while (std::getline (datafile, newstring))
{
int x = newstring.compare(concanstring);
if (x != 0) std::cout << "fail";
else std::cout << "success";
}
}
You have an extra space at the end of concanstring
because of which comparison fails, you have to remove that extra space like this
int main()
{
std::vector <int> momentum{ 5, 0 , 4 ,2 , 6 , 0 , 1 , 5 , 1 , 4 };
std::string newstring;
std::string concanstring;
std::ifstream datafile("thedata.txt");
for (int i = 0; i < momentum.size(); i++)
{
std::ostringstream newobj;
newobj << momentum[i];
concanstring += newobj.str();
concanstring += " ";
}
concanstring.pop_back();
std::cout << concanstring;
while (std::getline (datafile, newstring))
{
int x = newstring.compare(concanstring);
if (x != 0) std::cout << "fail";
else std::cout << "success";
}
}