Please Help Calling Function, If then

Hello. This is not a homework question but was a lab that I have constructed myself. I am having a difficult time and pretty confused at this point. I had written the code a little different before, and I got it to run almost correctly, but, I don't remember how I did it. Basically I need to get user input for age and height. Then I need to call the functions in the main functions and I believe pass by value or reference which I think I did correctly. My problem is returning it and getting the program to work. Any help or advice would be greatly appreciated as this is very frustrating and I'm sure it is really simple. Thanks.


#include <iostream>
using namespace std;


void get_values()
{
int age, height;


cout << "What is your age?\n";
cin >> age;
cout << "What is your height?\n";
cin >> height;

// COULD BE RETURN get_values()
}
void compare_values(int age, int height)

{


if(height == age)
{
cout << "Your height is equal to your age!";
}
else if(height < age)
{
cout << "Your height is less than your age!";
}
else if(height > age)
{
cout << "Your height is greater than your age!";
}
else
{
cout << "Something has gone seriously wrong here!";
}

}

void compare_age(int age, int height)

{


if(age >=13 && age <= 19)

{
cout << "You are a Teenager" << endl;
}
else if(age <10 || age>19)
{
cout << "You are not a Teenager" << endl;
}
else if (age >50)
{
cout << "You are more than 50 years" << endl;
}

}

int main()
{


return (0);


}
I would have made classes, then calling the functions inside the class(es) by using main, for example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class getAge {
public:
int age;
    void age() {
cout << "Age: ";
cin >> age;
cout << age;
//If functions
}
};

int main() {
      getAge callage;
       callage.age();
      return 0;
}

Edit: the concept is right, maybe not the actual code though...
Last edited on
@olsarets

IMO there is no real need to use classes for this simple code. And you have used terrible names for everything, so this is just confusion for the OP.

@ianS

Welcome to cplusplus :+)

First up, can we ask you to always use code tags:
http://www.cplusplus.com/articles/z13hAqkS/


I know there is a problem with the code tag button not working when one makes a new post, but one can edit the post afterwards, or manually put the code tags in to start with.

There are many advantages for us if you do this, some are that we get line numbers, and we can compile your code right here with cpp.sh - a small gear icon appears which allows anyone to do this.

So with your code, you need to do several things:

Call the functions from main(), at the moment nothing happens.

1
2
3
4
5
6
7
8
9
10
int main() {

    unsigned int AgeInYears = 0;   // unsigned type is important, avoid negative values
    unsigned int HeightInCentimetres = 0; // could use double here if in metres

    get_values(AgeInYears, HeightInCentimetres);  
    // do something with values

return 0;
}


Pass some arguments to the function as reference:

1
2
3
4
5
6
7
8
9
// pass by reference, the values of these will change in main()
void get_values(unsigned int& AgeInYears, unsigned int& HeightInCentimetres)
{
// int age, height;  /// not needed any more

// your code

// nothing to be returned
}


You could also do some validation for the values, that could be in it's own function. Validation is a very important idea in coding: it should always be in your mind. Some of the biggest problems in coding comes from either not initialising variables, or the data not being valid.

There is a tutorial, reference material and articles at he top left of this page.

Good Luck, don't hesitate to ask questions :+)
Thanks a lot! I really appreciate it, I was pretty confused as I have some gaps in my knowledge and I am still very new to programming. Also, sorry for the late reply. I have pasted my new code below.
I have one question. The program seems to run fine in Mac Terminal, but, when when I went to run it in Xcode it initially said I had an error and then the error went away.(I can't remember or find where the error was exactly) It runs the code all the way to the end, then it won't print out the final output stating whether your height is the same/or different from your age. For example, it will say:
What is your age?
20
What is your height?
5

...then it just stops there. I have looked endlessly and can't figure it out, though like I said it runs fine in mac terminal. At the bottom of the get_values function (my first function after main) on the line where I return, Xcode has highlighted this line in green and it says " Thread 1: breakpoint 1.1"
Any Ideas? Thanks for all the help! Also, TheIdeasMan, thanks for letting me know about the code tags and just generally helping me out.

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
#include <iostream>
using namespace std;


//prototype int get_values();
void get_values(int&, double&);
void compare_values(int, double);
void compare_age(int);



//main funciton
int main()
{
    int AgeInYears = 0;
    double HeightInCentimeters = 0;
    get_values(AgeInYears, HeightInCentimeters);
    compare_values(AgeInYears, HeightInCentimeters);
    compare_age(AgeInYears);
    return 0;
}

void get_values(int& AgeInYears, double& HeightInCentimeters)
{
    cout << "What is your age?\n";
    cin >> AgeInYears;
    cout << "What is your height?\n";
    cin >> HeightInCentimeters;

    // COULD BE RETURN get_values()
    return;
}

void compare_values(int AgeInYears, double HeightInCentimeters)
{
if(HeightInCentimeters == AgeInYears)
    {
    cout << "Your height is equal to your age!";
    }
else if(HeightInCentimeters < AgeInYears)
    {
        cout << "Your height is less than your age!";
    }
else if(HeightInCentimeters > AgeInYears)
    {
        cout << "Your height is greater than your age!";
    }
else
    {
        cout << "Something has gone seriously wrong here!";
    }
return;
} //end of function


   
void compare_age(int AgeInYears)
{
if(AgeInYears >=13 && AgeInYears <= 19)
    
    {
        cout  <<  "You are a Teenager"  << endl;
    }
else if(AgeInYears <10 || AgeInYears>19)
    {
        cout << "You are not a Teenager" << endl;
    }
else if (AgeInYears >50)
    {
        cout  <<  "You are more than 50 years"  << endl;
    }
return;
} // end of the if

   
    
    

    


    
Hi,

The code works fine for me in cpp.sh.

You have HeightInCentimeters as a double, this might not work so well on line 36. Even though double has 15 significant figures it is not stored exactly. So equality comparisons almost never work. For this exercise you could change the type to unsigned int.

With your function prototypes, put a variable name in for the parameters, the same as the function definition:

5
6
7
8
9
10
//prototype int get_values();
void get_values(unsigned int& AgeInYears, 
                      unsigned int& HeightInCentimeters);
void compare_values(const unsigned int AgeInYears, 
                         const unsigned int HeightInCentimeters);
void compare_age(const unsigned int AgeInYears);


I made some of them const , this is a really good practise - one is promising not to change that value in the function; the compiler will enforce that. Remember to update the function definitions.

When there are multiple parameters, I put each one on it's own line. It's easier to read that way, one can put in comments if necessary.

There is no need to put return; in void functions, unless you need to exit the function early.

With the braces, it's general consensus for the braces to line up with whatever statement they are attached to. That is easier on the eye, and is helpful when there are multiple levels of indentation.

If all that works, you could do a validation function :+)
Thanks a lot! I have learned a lot just from posting this and receiving all your feed back, I really appreciate you helping me out. Look forward to seeing you around the site!
No worries, pleased to help. Awesome that you sent thanks, often that doesn't happen :+D
Topic archived. No new replies allowed.