Feb 22, 2021 at 12:57am UTC
Hi, i'm stuck trying to iterate through a structure, what im trying to do is print to a file items whose quantity is < 10.
My file looks like this:
1 2 3 4 5
1 apple 10
2 pineapple 5
3 banana 9
5 kiwi 6
6 pear 8
And my structure looks like
1 2 3 4 5 6
struct Inventory
{
int id;
string name;
int quantity;
}
I know how to print the data to a new text file but i'm confused as to how only print those that have less than 10 items.
Last edited on Feb 22, 2021 at 12:57am UTC
Feb 22, 2021 at 4:47am UTC
pseudo code:
for(all the items)
if (item.quantity < 10) //this is how it really looks in c++ or maybe if(array[index].quantity < 10)
file << item.id << " " << item.name << " " << item.quantity << endl;
the ones that are 10 or larger won't print, because of the condition.
Last edited on Feb 22, 2021 at 4:48am UTC
Feb 22, 2021 at 1:37pm UTC
Hi, i just noticed i didn't include more info sorry my bad.
I tried doing what Jonnin said before writing the original post and it only printed the first item on the file.
EDIT: I have managed to get it to work the way i was doing before, i don't know what i was doing wrong.
Last edited on Feb 22, 2021 at 1:46pm UTC
Feb 22, 2021 at 2:04pm UTC
If you still have problems, post your code.