passing arguments by reference

I'm currently writing a code that returns the area and perimeter of a triangle. It gets user inputted side lengths and calculates from there. When I try to compile it I get a message that the variables in the main function need to be initialized. I didn't think I needed to do this if the variables are chosen by the user. I'm very new to programming so I apologize if I am missing something obvious.

#include<iostream>
#include<cmath>
using namespace std;

void title();
void side1(double &);
void side2(double &);
void side3(double &);
void area(double &);

int main(){
double one, two, three, result;

title();
side1(one);
side2(two);
side3(three);

cout << "The area of the triangle formed by the sides " <<
one << ", " << two << " and " << three << " is: " << sqrt(result) << endl;
cout << "The perimeter is: " << one + two + three << "\n\n";

system("pause");
return 0;
}
void title(){
cout << "Triangle Area Solution\n";
cout << "Welcome to Triangle City!\n\n\n";
}
void side1(double &first){
cout << "Type the size of side a: ";
cin >> first; cin.ignore(80, '\n');
while (first <= 0){
cout << "Please enter a valid side. Try again: ";
cin >> first; cin.ignore(80, '\n');
}
}
void side2(double &second){
cout << "Type the size of side b: ";
cin >> second; cin.ignore(80, '\n');
while (second <= 0){
cout << "Please enter a valid side. Try again: ";
cin >> second; cin.ignore(80, '\n');
}
}
void side3(double &third){
cout << "Type the size of side c: ";
cin >> third; cin.ignore(80, '\n');
while (third <= 0){
cout << "Please enter a valid side. Try again: ";
cin >> third; cin.ignore(80, '\n');
}
}
void area(double &answer){
double one, two, three, semi;
semi = (one + two + three)/2;
answer = (semi * (semi - one) * (semi - two) * (semi - three));
}
Last edited on
From what I can tell you have two main problems.

First, in the function void area(double &answer) you use three variables that have not been initialized.

1
2
double one, two, three, semi;
semi = (one + two + three)/2;

These are not the same as the ones in main, but instead are used only in this function. You will need to pass them in as a parameter in order to use them here.

It also seems you are using double result without initializing it when you try to take its root:

 
sqrt(result) 

Hope that helps.
I think I'm getting confused with variable names now. I would want 'double one, two, three' to be the user inputs from the other three functions. If I were to initialize them wouldn't I essentially be choosing arbitrary values? Thanks.
You're right in that you wouldn't want to initialize them with just anything. What you need to do is pass them as parameters into your function. There are a couple ways to do this but the simplest will probably be to set up your function like this:

 
void area(double &answer, double one, double two, double three)

Then you can just pass the data to it from main when you call it. This will make copies of the variables inside the area function.
have a look:
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
#include<iostream>
#include<cmath>
#include <conio.h>
using namespace std;

void title();
void side1(double &);
void side2(double &);
void side3(double &);
void area(double &, double, double, double);

int main(){
double one, two, three, result;

title();
side1(one);
side2(two);
side3(three);
result = one + two + three;
cout << "The area of the triangle formed by the sides " << 
one << ", " << two << " and " << three << " is: " << sqrt(result) << endl;
cout << "The perimeter is: " << one + two + three << "\n\n";
cout << "press any key to continue...";
getch();
return 0;
}
void title(){
cout << "Triangle Area Solution\n";
cout << "by M. Caramello\n\n";
cout << "Welcome to Triangle City!\n\n\n";
}
void side1(double &first){
	do {
		cout << "Type the size of side a: ";
		cin >> first;
	} while (first < 1);
}

void side2(double &second){
do {
		cout << "Type the size of side b: ";
		cin >> second;
	} while (second < 1);
}
void side3(double &third){
do {
		cout << "Type the size of side c: ";
		cin >> third;
	} while (third < 1);
}
void area(double &answer, double one, double two, double three){
double semi;
semi = (one + two + three)/2;
answer = (semi * (semi - one) * (semi - two) * (semi - three));
}


btw, you never use area()
Topic archived. No new replies allowed.