# Thread: Help on a simple functions program

1. ## Help on a simple functions program

It seems that my code is way off.

I get no errors from this code, but the output is...er...VERY interesting.

Code:
```//Cube volume calculating program v2.0
//calculates the difference between two 3-dimensional objects

typedef unsigned short USHORT;

#include <iostream>

using namespace std;

USHORT areaOfObj1(USHORT length, USHORT width, USHORT height);
USHORT areaOfObj2(USHORT length, USHORT width, USHORT height);

int main() {
cout << "\t\tCube Volume Calculating Program v2.0" << endl;
cout << "\t   Calculates the difference between the volume" << endl;
cout << "\t       of one cube to the volume of another." << endl;

USHORT Obj1length;
USHORT Obj1width;
USHORT Obj1height;
USHORT Obj2length;
USHORT Obj2height;
USHORT Obj2width;
USHORT Obj1volume;
USHORT Obj2volume;
USHORT Obj1and2dif;

Obj1volume = areaOfObj1(Obj1length, Obj1width, Obj1height);
Obj2volume = areaOfObj2(Obj2length, Obj2width, Obj2height);

cout << "\n\tWhat is the length of the first cube?" << endl;
cin >> Obj1length;
cout << "\n\tWhat is the width of the first cube?" << endl;
cin >> Obj1width;
cout << "\n\tWhat is the height of the first cube?" << endl;
cin >> Obj1height;
cout << "\n\tWhat is the length of the second cube?" << endl;
cin >> Obj2length;
cout << "\n\tWhat is the width of the second cube?" << endl;
cin >> Obj2width;
cout << "\n\tWhat is the height of the second cube?" << endl;
cin >> Obj2height;

Obj1and2dif = Obj1volume - Obj2volume;
cin.get();

cout << "The volume of the first cube is " << Obj1volume << endl;
cout << "The volume of the second cube is " << Obj2volume << endl;
cout << "\nThe difference between the volume of" << endl;
cout << "the first cube and the second cube is: " << Obj1and2dif << endl;

cin.get();
return 0;
}

USHORT areaOfObj1 (USHORT l, USHORT w, USHORT h) {
cin.ignore();
cin.get();
return l * w * h;
};

USHORT areaOfObj2 (USHORT l, USHORT w, USHORT h) {
cin.ignore();
cin.get();
return l * w * h;
};```
I'm making logical errors. Please help me! Thanks!

2. You are calculating the volume before you ask the user to input the dimensions. You should probably wait until you have the dimensions before using them. If you initialize your variables which is a good habit (to 0 in this case), then you would have noticed that your volume was always 0.

3. Try reading the values of Obj1Length and other variables BEFORE you pass their values to the functions that compute the volumes.

The reason you'll get interesting output is that variables are not initialised to anything in particular unless you initialise them (or reassign them to something valid).

4. Thanks guys! Stupid mistake.

Appreciate it.

Popular pages Recent additions