Need help with Functions

I have this homework that requires me to have five names and five heights that goes with the name. With that I'm suppose to do that with functions. My question is why do i get several error when I use a "" variable without being initialized.

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

void pause(void);
void average(void);
void input(string &a, string &b, string &c, string &d, string &e);
void Input(int &f, int &g, int &h, int &i, int &j);
float calculateAverage(int a, int b, int c, int d, int e);
void output(float average);
void flushInput(void);
void printTable();

int main(void)
{
	try
	{
		average();
	}
	catch (int exceptionValue)
	{
		return exceptionValue;
	}
	pause();
	return 0;
}

void flushInput(void)
{
	cin.ignore(999,'\n');
}

int getInt(string prompt)
{
	cout << prompt;
	int userInput;
	cin >> userInput;
	flushInput();
	if ( cin.fail())
	{
		cin.clear();
		flushInput();
		cout << "Error: Input does not qualify as integer."
			<<"Press ENTER to finish..." << endl;
		pause();
		throw 1;
	}
	return userInput;
}

void input(string &name1, string &name2, string &name3, string &name4, string &name5, int &height1, int &height2, int &height3, int &height4, int &height5)
{
	name1 = getInt("Enter name	01: ");
	height1 = getInt("Enter height	01: ");
	name2 = getInt("Enter name	02: ");
	height2 = getInt("Enter height	02: ");
	name3 = getInt("Enter name	03: ");
	height3 = getInt("Enter height	03: ");
	name4 =getInt("Enter name	04: ");
	height4 = getInt("Enter height	04: ");
	name5 = getInt("Enter name	05: ");
	height5 = getInt("Enter height	05: ");
}

void printTable()
{

}
void average(void)
{
	string a, b, c, d, e;
	int f, g, h, i, j;

	float average
		= calculateAverage(f, g, h, i, j);
	input(a, b, c, d, e, f, g, h, i, j); 

	output(average);
}

void output(float average)
{
	cout << "Average height is " << average
		<< "inches." << endl << endl;
}

float calculateAverage(int a, int b, int c, int d, int e)
{
	float sum = a + b + c + d + e;
	return sum / 5;
}

void pause(void)
{ 
	cout << "Press ENTER to continue...";
	cin.ignore(999,'\n');
}

The output should look like this
Name Inches Feet

name height
name height
name height
name height
name height

average is number inches.
In line 72 of function average, you have the line int f, g, h, i, j; with all variables uninitialized.
you then pass these uninitialized variables to calculateAverage.

In line 89 of calculateAverage you have the lines
1
2
float sum = a + b + c + d + e;
return sum / 5;
which manipulate these non-initialized variables.

When a variable is declared, junk value is given to it.
junk + junk = junk;
junk/5 = junk;

Hence, output is junk;

This is my output of your program
http://picpaste.com/Capture-F9NtKRxw.PNG

Topic archived. No new replies allowed.