Making use of Structures

Ok so first off this is not a class assignment in any case. I am trying to figure out how to make good use of my structures since I recently learned to use them.

So what I would like to know is how to use a function to receive inputs for all of the given data pieces in a structure under a given name. And then be able to give the same function another name of one of the variables of the Structure type and do the same again. Sadly I believe this requires pointers, but after reading the Data Structures article I am still left confused on this mark.

So what would be the proper way to fill the various parts of Watermelon and Strawberry, using the fill function?

1
2
3
4
5
6
7
8
9
10
11
12
//Example

struct Fruits{
   double Price;
   int Weight;
   char Grade;
}Watermelon,Strawberry;

fill(Fruits /* pointer reference I assume*/){
    cin >> /*pointer reference again*/.price
   // repeat similar steps.
};
You don't need pointers for this, but a reference would help. This is without the error correcting of course to make sure that data is being inputed properly (no strings or bad numbers).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct Fruits
{
     double Price;
     int Weight;
     char Grade;
}

Fruits fill()
{
     Fruits f;
     cout << "Price" << endl;
     cin >> f.Price;
     cout << "Weight" << endl;
     cin >> f.Weight;
     cout << "Grade" << endl;
     cin >>  f.Grade;
     return f;
}


Not sure if this is what you are looking for, but this is how you would use cin to get the data and put it directly in the struct. If you want to use only one cin to do it all, then you would need to se a string and parse it like so

1
2
3
4
5
6
7
Fruits f;
string s;
cout << "Price Weight Grade" << endl;
getline(cin, s);
istringstream i(s);
i >> f.Price >> f.Weight >> f.Grade;
return f;


There is just a few ways you can go about doing what I believe you are trying to do, hope it helps.
Yeah, that's the basics, and I had managed to figure out how to make use of what you just explained by the time I posted this question.

So to be more specific, How do you pass the variable of type fruits into the fill function such that I could use fill for both Watermelon and Strawberry[ to reference the example].

My problem was when I used the fill function before, I tried to display Watermelon later and and I couldn't get any of the entered data that way...[Everything was 0]

Which is why I asked about pointers and how to make use of those. Thank you for your help, but I was hoping for a bit more.
Topic archived. No new replies allowed.