I'm trying tocreate a marketplace simulation that puts item data into a struct and then matches and
Question:
I'm trying tocreate a marketplace simulation that puts item data into a struct and then matches and deletes those matches based on:
1. Name matches - Makes sure it is the same object. (i.e. "Chicken")
2. Type matches - (i.e. "Wanted" goes with "For Sale")
3. Price Matches ("For Sale" price is less than or equal to "Wanted" price)
Within "garageSale.txt" are lines of data such as:
bike,for sale,100 bike,for sale,50 bike,wanted,60 microwave,wanted,40 bike,for sale,200 microwave,for sale,50 dresser,for sale,500 chicken,for sale,10
I've successfully split apart this data and stored into struct item, I just need help with the for loop that actually matches them, not too sure what to do.
Here is my code right now (C++):
struct item { string type; bool status; int price; };
void read_input(string f_path) { struct item itemArray[100]; string input_line; ifstream f(f_path); int x = 0; if (f.is_open()) { cout << "Reading lines from " << f_path << "..." << endl;
while (getline(f, input_line)) { stringstream ss(input_line); string token; int i = 0;
while (getline(ss, token, ',')) {
/* for loop that runs through what's already in the array, checking for matches and deletes matching elements.
for (i = 0; i < x; i++) {
}*/
if (i == 0) itemArray[x].type = token; if (i == 1 && token == "for sale") itemArray[x].status = false; else itemArray[x].status = true; if (i == 2) itemArray[x].price = stoi(token); i++; } x++; } f.close(); } else { cout << "File was not opened!" << endl; } }
int main() {
read_input("garagesale.txt");
}
In the end, I'd like to print out the items left in the array without matches and then print out:
# of Items leftover:
# of Items sold: