This comparison between a vector of numbers converted to a string and a string from a text file is not working?

48 Views Asked by At

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";        
}      
}
3

There are 3 best solutions below

1
On BEST ANSWER

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";        
}      
}
0
On

Found a way that works, and its less code so i'm happy :D if anyone thinks this is a bad way of doing it, please let me know :)

int main()
{
std::vector <int> momentum{ 5,  0 , 4  ,2 , 6 , 0 , 1 , 5 , 1 , 4 };
std::vector <int> newmomentum{};
std::string newstring;
std::ifstream datafile("thedata.txt");
int moment{};

while (std::getline (datafile, newstring))
{
    std::stringstream nextstring(newstring);
    for (int i = 0; i < momentum.size(); i++)
    {
        nextstring >> moment;
        newmomentum.push_back(moment);
    }
    if (newmomentum == momentum)
    {
        std::cout << "success";
    }
    else newmomentum.clear();        
} 
}
4
On

Instead concatenating strings read int and compare that.

int findFileNrWith(std::istream& in, const std::vector<int>& data)
{
    int lineNr = 0;
    std::string l;
    while (getline(in, l)) {
        std::istringstream line{l};
        auto stream_begin = std::istream_iterator<int>{line};
        auto stream_end = std::istream_iterator<int>{};

        auto result = std::mismatch(data.begin(), data.end(), 
                                    stream_begin, stream_end);

        if (result == std::make_pair(data.end(), stream_end))
           return lineNr;
        ++lineNr;
    }
    return -1;
}