Pass structure info. to another function

Hi all, I'm having yet another problem with my program. What I'm attempting to do is pass information stored in members of structures to a different function to print them to the screen. I can't quite figure out how to get them to pass back to main.. maybe you guys can help. I've included the code of the two functions here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
void enterCreatures(struct Creatures st, struct Cost sr)
{
   int choice;
   fstream file;
   char fileName[26];
   
   cout << "What do you want to do?" << endl;
   cout << "\t1. Load my creatures from a file." << endl; 
   cout << "\t2. Enter one creature manually." << endl;
   cout << "Choice: ";
   cin >> choice;
   cout << endl;
   
   if (choice == 1)
   {
      cin.ignore();
      cout << "What is the name of the file with your list of creatures? (ex: filename.txt)" << endl; 
      cout << "File name: "; 
      cin.getline(fileName,26);
      cout << endl << endl;
      
      file.open(fileName, ios::in);
      if(!file)
      {
         cout << fileName << " could not be opened." << endl << endl; 
      }
      
      else 
      {
         // Program needs to do all reading here
         cout << "All creatures from " << fileName << " have been added to the program." << endl << endl;
      }
        
   }
   
   bool addAnother = true;
   Creatures creatureInfo;
   Cost a;
   int x =0;
   char addMore;
   
   while (addAnother == true)
   {
   
   if (choice == 2)
   {
      cout << endl << endl;
      cin.ignore();
      cout << "Name: ";
      getline(cin, creatureInfo.name[x]);
      cout << endl << endl;
      
      cout << "Description: ";
      getline(cin, creatureInfo.description[x]);
      cout << endl << endl;
      
      cout << "Average length (in feet): ";
      cin >> creatureInfo.avgLength[x];
      cout << endl << endl; 
      
      cout << "Average height (in feet): ";
      cin >> creatureInfo.avgHeight[x];
      cout << endl << endl; 
      
      cout << "Location: ";
      cin.ignore();
      getline(cin, creatureInfo.location[x]);
      cout << endl << endl; 
      
      cout << "Is it a dangerous creature? (y or n): ";
      cin >> creatureInfo.dangerous[x];
      cout << endl << endl; 
      
      cout << "How many hours do you spend caring for the " << creatureInfo.name[x] << "?" << endl;
      cout << "Number of hours: ";
      cin >> a.numHours[x];
      cout << endl << endl;
      
      cout << "What is the cost per hour for caring for the " << creatureInfo.name[x] << "?" << endl;
      cout << "Cost per hour: ";
      cin >> a.costPerHour[x];
      cout << endl << endl;
      
      cout << "How much money do you spend on food for the " << creatureInfo.name[x] << "?" << endl;
      cout << "Food cost: ";
      cin >> a.foodCost[x];
      cout << endl << endl;
      
      cout << "How much money do you spend on grooming and medical supplies for the " << creatureInfo.name[x] << " ?" << endl;
      cout << "Supply Cost: ";
      
      cin >> a.supplyCost[x];
      cout << endl << endl; 
      
      cout << "The " << creatureInfo.name[x] << " has been added." << endl << endl;
      
      cout << "Want to add more creatures? (y or n) ";
      cin >> addMore;
      cout << endl;
      
      creatureInfo.total[x] = (a.numHours[x] * a.costPerHour[x]) + a.foodCost[x] + a.supplyCost[x];
      
      if (addMore == 'n' || addMore == 'N')
         addAnother = false; 
      
      x++;
      numCreatures++;
      
      
   } // End of if statement
   
   } // End while loop
   
         
}


From this function I want to pass the member creatureInfo.name[x] and creatureInfo.total[x] to this function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void printStatistics(struct Creatures creatureInfo, struct Cost sr, int numCreatures)
{
   float total;
   
   cout << "Cost of each creature for one week:" << endl << endl;
   cout << "Creature\t\t\tCost" << endl;
   for (int x = 0; x < numCreatures; x++)
   {
      cout << creatureInfo.name[x] << "\t\t\t$" << creatureInfo.total[x]; 
   }
   
   for (int x = 0; x < numCreatures; x++)
   {
      total += creatureInfo.total[x];
   }
   
   cout << "Total cost:\t\t\t$" << total << endl << endl;
   
   cout << "Current num of creatures: " << numCreatures << endl << endl;
}  


However, when it prints to the console I'm getting memory addresses, not what is actually stored in memory. Any help is appreciated.

Thanks in advance!
You could pass the struct in as a pointer and then access the members from there, for example:
void printStatistics(StructName* t) and then you can access the members like
t->member

Also, i don't see where you declared your struct, but if you do it like this:
1
2
3
typedef struct Sname {
//info
} Sname;


then you don't have to use struct Sname blah you can just say Sname blah

Last edited on
I've kind of got an idea of how that works.. my misunderstanding is that the first function is what stores information in the members of the structure, whereas the second one accesses it. So how do I go about returning it to the main function and pass into the second one? Maybe this is a silly question but I'm really struggling with it, and it is one of the last things left to complete this program.

Thanks again,

Zach
What does the creature struct look like?
That'd probably be helpful haha, sorry for not including. Here it is:

1
2
3
4
5
6
7
8
9
10
11
struct Creatures
{
   string name[50];
   string description[50];
   float avgLength[50];
   float avgHeight[50];
   string location[50];
   char dangerous[50];
   float total[50];
   int numCreatures;
};
Topic archived. No new replies allowed.