0

This bit of code below is reading in a stringstream and adding each piece to a struct, and once its done its adds the whole struct to a list of structs.board is the temporary struct and then

while (getline(ss, word, ',')){
             if (wordIndex==0){
                board.item = word;
                }
             else if (wordIndex==1&&word==" for sale"){
                board.forSale = false;
                }
             else if (wordIndex==1&&word==" wanted"){
                board.forSale = true;
                }
             else if (wordIndex==2){
                board.price = atoi(word.c_str());
                }
             wordIndex++;


        }
        index ++;
        messageBoard.push_back(board);

I also have this function im trying to create that compares the contents of board, with the contents of array. Not sure if I need to compare it with an iterator or just compare the items individually.

bool compare(struct messageBoard* one, struct messageBoard* two){

return(one.item==two.item&&one.forSale!=two.forSale&&one.price<=two.price)

}

Thats as far as I've gotten. The goal is if Item matches and the forSale values are opposites(True vs False) and the price of the for sale is <= to the wanted item, then the bool function returns True, else it returns false. I want to run it right before the

arr.push_back(board);
Kelvin Davis
  • 355
  • 1
  • 3
  • 10
  • Save yourself a bit of hassle with [`std::find`](http://en.cppreference.com/w/cpp/algorithm/find). You provide it a function that compares two `struct`s and it does the rest. – user4581301 Jan 25 '17 at 04:33
  • Come to think of it, swap `vector` for `list` and this is a near match: [Vectors, structs and std::find](http://stackoverflow.com/questions/589985/vectors-structs-and-stdfind) – user4581301 Jan 25 '17 at 04:36
  • Ill look into that. Gracias – Kelvin Davis Jan 25 '17 at 04:38

0 Answers0