Alright I have this code done, and I'm not sure what I should be doing to fix the error message it is giving me. The computer is saying something about z not being initialized, and I dont know how to fix it, help would be much appreciated.
Also the error happens at "GetData(x,y,z);
This is my already written code
//DebugHer5D
//
//
//
//Programmer: Harold Osborn
//
#include <iostream>
using namespace std;
void GetData(double x, double y, double z);
double DoComputations (double x, double y, double z, double product);
void PrintResults(double x, double y, double z, double product, int sum);
int main()
{
double x,
y,
z,
sum,
product;
cout << "Type 0 0 0 to exit!\n\n";
GetData(x, y, z);
while ( x != 0 || y != 0 || z != 0 );
{
DoComputations(x, y, z, product);
PrintResults(x, y, z, sum, product);
GetData(x, y, z);
}
return 0;
}
void GetData(double a, double b, double c)
{
cout << "Enter three real numbers: ";
cin >> a;
cin >> b >> c;
}
double DoComputations (double a, double b, double c, double product)
{
double sum;
sum = a + b + c;
product = a * b * c;
return (sum);
}
void PrintResults(double x, double y, double z, double product, int sum)
{
cout.setf(ios::fixed);
cout.precision(1);
cout << "The numbers entered were: " << x << " " << y << " " << z << endl;
cout << "\tThe product is " << product;
cout << "\tThe sum is " << sum;
}
you're passing x, y and z into GetData by value, this means that the function makes a copy of them and the input the user gives is only assigned to the copies, which are destroyed when the function returns. Pass them in by reference like so:
void GetData(double &a, double &b, double &c)
This way a, b and c are aliases for the variables you pass in from main
does this mean i should change all (x,y,z) to (&a,&b,&c), because when i do that for just the "GetData" function I get an error when trying to start debugging again saying that "the system cannot find the file specified."
Firstly - a, b and c or z, y and z are just names, it doesn't matter what names you give to the variables in the function definition.
Secondly - You don't call the function with the ampersands, they go in the function definition, and you call the function in the the same way as usual.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void GetData(double &a, double &b, double &c) // define that the function takes the variables by reference
{
cout << "Enter three real numbers: ";
cin >> a >> b >> c;
}
// in main you call it the same way
int main()
{
// ...
int a, b, c;
GetData(a, b, c);
// ...
}
you should also try to make your variable names more meaningful rather than just arbitrary letters
In this specific project, we were given the main body of code to start with and were supposed to use the debugger to get through it. I am definately not a CS major and I am really struggling with the class. Ill try what you suggested as soon as i get the chance
I have made the changes you suggested and now have made it through the debugging all the way to the interior of the "DoComputations" command. It keeps giving me some crazy number for the sum