Arguments and Functions

Alright, I'm supposed to be writing a program that does this:

Write a program that demonstrates your understanding of functions, conditions and loops. The program should ask the user for three first names and each person's age, then calculate the following information for all three people:

Taylor is 2 and has blown out 3 birthday candles.
Raj is 5 and has blown out 15 birthday candles.
Austin is 12 and has blown out 78 birthday candles.


The total of all ages is 19.
The average age is 6.

There should be three methods. One for calculating the average, one for calculating the total age and one for calculating the total number of candles that person has blown out. This is assuming that every year each person blew out the number of candles on their cake (which is equal to their age that year.)

For this assignment I am stipulating that you:
•Use a loop to calculate the number of birthday candles blown out.
•Pass appropriate arguments to methods and return values from them.


I am having a very hard time with this, but here is the code I have gotten so far.

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

//Fucntion Prototypes

int totalOfAges(int& firstAge, int& secondAge, int& thirdAge, int& total);
int averageOfAges(int& firstAge, int& secondAge, int& thirdAge, int& average);
int candlesBlownOut(int& firstAge, int& secondAge, int& thirdAge, int& candles);


int main()
{
	string firstName, secondName, thirdName;
	int firstAge, secondAge, thirdAge;
	int total, average, candles;

	cout << "Input first name of first person. " ;
	cin >> firstName;
	cout << "Input first person's age. ";
	cin >> firstAge;

	cout << "Input first name of second person. ";
	cin >> secondName;
	cout << "Input second person's age. ";
	cin >> secondAge;

	cout << "Input first name of third person. ";
	cin >> thirdName;
	cout << "Input third person's age. ";
	cin >> thirdAge;


	cout << firstName << "is " << firstAge << "and has blown out " << candles << " birthday candles. " << endl;
	cout << secondName << "is " << secondAge << "and has blown out " << candles << " birthday candles. " << endl;
	cout << thirdName << "is " << thirdAge << "and has blown out " << candles << " birthday candles. " << endl;

	cout << "The total of all ages is " << total << "." << endl;
	cout << "The average age is " << average << ".";


	return 0;

}

int totalOfAges(int& firstAge, int& secondAge, int& thirdAge, int&total)
{
	total = firstAge + secondAge + thirdAge;
	return total;
}

int averageOfAges(int& firstAge, int& secondAge, int& thirdAge, int &average)
{
	average = (firstAge + secondAge + thirdAge)/3;
	return average;
}

int candlesBlownOut(int& firstAge, int& secondAge, int& thirdAge, int &candles)
{
	candles = 2;
	return candles;
}


Any help would be appreciated, as I do not understand most of this very well.
the total candles blown should be calculated one person at time, then a possible definition can be this:
1
2
3
4
5
6
7
8
9
10
11
12
int candlesBlownOut(int age){
    int count=0;
    for(int i=age;i>0;i++)
        count+=i;
    return count;
}

// in the main
cout << firstName << "is " << firstAge << "and has blown out " << candlesBlownOut(firstAge) << " birthday candles. " << endl;
	cout << secondName << "is " << secondAge << "and has blown out " << candlesBlownOut(secondAge) << " birthday candles. " << endl;
	cout << thirdName << "is " << thirdAge << "and has blown out " << candlesBlownOut(thirdAge) << " birthday candles. " << endl;

You must call the function 'averageOfAges()' and 'totalOfAges()' in the main too.
While I know that you are right, it does not give me the proper number back. Also, it says its overloaded, and the othe functions are not working either.

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

//Fucntion Prototypes

int totalOfAges(int&, int&, int&, int&);
int averageOfAges(int&, int&, int&, int&);
int candlesBlownOut(int age);


int main()
{
	string firstName, secondName, thirdName;
	int firstAge, secondAge, thirdAge, total, average;
	int totalofAges();
	int averageOfAges();
	
	cout << "Input first name of first person. " ;
	cin >> firstName;
	cout << "Input first person's age. ";
	cin >> firstAge;

	cout << "Input first name of second person. ";
	cin >> secondName;
	cout << "Input second person's age. ";
	cin >> secondAge;

	cout << "Input first name of third person. ";
	cin >> thirdName;
	cout << "Input third person's age. ";
	cin >> thirdAge;


	cout << firstName << " is " << firstAge << " and has blown out " << candlesBlownOut(firstAge) << " birthday candles. " << endl;
	cout << secondName << " is " << secondAge << " and has blown out " << candlesBlownOut(secondAge) << " birthday candles. " << endl;
	cout << thirdName << " is " << thirdAge << " and has blown out " << candlesBlownOut(thirdAge) << " birthday candles. " << endl;

	cout << "The total of all ages is " << totalOfAges(firstAge, secondAge, thirdAge, total) << "." << endl;
	cout << "The average age is " << averageOfAges() << ".";

	system("pause");
	return 0;

}

int totalOfAges(int &firstAge, int &secondAge, int &thirdAge, int &total)
{
	total = firstAge + secondAge + thirdAge;
	return total;
}

int averageOfAges(int &firstAge, int &secondAge, int &thirdAge, int &average)
{
	average = (firstAge + secondAge + thirdAge)/3;
	return average;
}

int candlesBlownOut(int age)
{
    int count=0;
    for(int i=age;i>0;i++)
        count+=i;
    return count;
}
i've noticed that there's an error here, sorry my fault:
1
2
3
4
5
6
7
int candlesBlownOut(int age)
{
    int count=0;
    for(int i=age;i>0;i--)  //changed in 'i--'
        count+=i;
    return count;
}

for the 'averageOfAges()' it should return a 'float' value because the 'int' type operation trunc the decimal part:
1
2
3
4
5
float averageOfAges(int &firstAge, int &secondAge, int &thirdAge)
{
	float average = (float)(firstAge + secondAge + thirdAge) / 3.0; // type-cast from int values to 'float'
	return average;
}

and 'totalOfAges()' can have just 3 parameters
1
2
3
4
5
int totalOfAges(int &firstAge, int &secondAge, int &thirdAge)
{
	total = firstAge + secondAge + thirdAge;
	return total;
}
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
int main()
{
	string firstName, secondName, thirdName;
	int firstAge, secondAge, thirdAge, total, average;
	//int totalofAges();  // remove or comment out this line - this is what gives you overloaded warning I think
	//int averageOfAges(); // remove or comment out this line
	
	cout << "Input first name of first person. " ;
	cin >> firstName;
	cout << "Input first person's age. ";
	cin >> firstAge;

	cout << "Input first name of second person. ";
	cin >> secondName;
	cout << "Input second person's age. ";
	cin >> secondAge;

	cout << "Input first name of third person. ";
	cin >> thirdName;
	cout << "Input third person's age. ";
	cin >> thirdAge;


	cout << firstName << " is " << firstAge << " and has blown out " << candlesBlownOut(firstAge) << " birthday candles. " << endl;
	cout << secondName << " is " << secondAge << " and has blown out " << candlesBlownOut(secondAge) << " birthday candles. " << endl;
	cout << thirdName << " is " << thirdAge << " and has blown out " << candlesBlownOut(thirdAge) << " birthday candles. " << endl;

	cout << "The total of all ages is " << totalOfAges(firstAge, secondAge, thirdAge, total) << "." << endl;
	cout << "The average age is " << averageOfAges() << "."; // you did not give averageOfAges any arguments

	system("pause");
	return 0;

}



for the 'averageOfAges()' it should return a 'float' value because the 'int' type operation trunc the decimal part:


The average for the input data is given as an integer so probably not necessary to cast to float.
Last edited on
Thanks very much...I figured it out now!
Topic archived. No new replies allowed.