Need help with this adding an int to a struct call.

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
#include <cstdlib>
#include <iostream>

using namespace std;

struct height
{
	int feet, inches;


};

void input_height(height& h);
void grow(height& h);
void print_height(height& h);


int main(int argc, char *argv[])
{
	int inches,  // number of inches to grow, user entered 
		inch;    // counter for inches grown so far

	height p1, p2;

	cout << "Enter the first person's height: ";
	input_height(p1);
	cout << "\nEnter the second person's height: ";
	input_height(p2);

	cout << "\nPerson 1" << endl;
	print_height(p1);
	cout << "\n\nPerson 2" << endl;
	print_height(p2);

	cout << "\n\nEnter a number of inches for Person 1 to grow: ";
	cin >> inches;

	// grow user supplied number of inches
	for (inch = 0; inch < inches; inch++)
		grow(p1);

	cout << "\nPerson 1 after growing " << inches << " inches" << endl;
	print_height(p1);

	cout << "\n\n";

	system("PAUSE");
	return 0;
}

void input_height(height& h)
{

	//cout << "What is the height of the person in feet?";

	cin >> h.feet;

	//cout << "What is the height of the person in inches";

		cin >> h.inches;



}


void grow(height& h)
{
	
	h.inches++;

		if (h.inches > 11)
	{
		h.feet + 1;
		h.inches - 12;
	}
}


void print_height(height& h)
{

	cout << "feet:    " << h.feet << endl;
	cout << "inches:  " << h.inches << endl;



}

Sorry im new to this forum but will probably have many questions in the future. I've gotten a little confused when it comes to structs but i think i almost have it.

I'm getting a succeed when i try to compile but my grow function is throwing me an warning that the + and - don't work in regards to grow function.

Basically i want it to grow a certain amount of inches specified and format the foot and inches if it goes over a foot. Can i not just add 1 to feet as it's an int? The same goes with inches.

Not sure how to use the code format here for some reason it's not letting me. Sorry!
Last edited on
I seemed to have figured it out. all i did was h.feet = h.feet +1 and that worked. Not sure why the other way didn't work and would gladly love someone to tell me.
Lines like:

1
2
h.feet + 1;
h.inches - 12;

Don't actually do anything. They're just values. They're the equivalent of just writing lines like:

1
2
6;
1;


For that to have any meaning, you need to actually do something with those values, like assign them to a variable, or pass them into a function, or something.

Presumably, what you actually want to do is change the values of h.feet and h.inches the be the results of those calculations. Thus, you need to assign those values to the variables:

1
2
h.feet = h.feet + 1; // Assign the value (h.feet + 1) to the variable h.feet
h.inches = h.inches - 12; // Assign the value (h.inches + 1) to the variable h.inches  


EDIT: And please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/
Last edited on
That's exactly what I ended up doing after a little messing around. I appreciate it, and woops I tried doing <code> instead of [code]. Thanks! I'm sure you'll see more of me around! I'll help out where I can in this forum!
Topic archived. No new replies allowed.