Print items in structure with quantity < 10

Feb 22, 2021 at 12:57am
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
Feb 22, 2021 at 3:48am
Hello Razgriz,


I know how to print the data to a new text file


So instead of printing to a file print to "cout". Inside the for loop use an if statement to print only those that are < 10.

If I had more to look at I could be more specific.


Andy
Feb 22, 2021 at 4:47am
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
Feb 22, 2021 at 1:37pm
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
Feb 22, 2021 at 2:04pm
If you still have problems, post your code.
Topic archived. No new replies allowed.