debbie_lee104
07-16-2007, 04:01 AM
I'm trying to create a four-function calculator with parameter passing, but i'm getting errors. Please help!
//////COMPILER DIRECTIVES
#include <iostream> //provides cin and cout
#include <cstdlib> //provides exit_success
#include <cmath>
using namespace std; //allows all standard library to be used
/////////FUNCTION PROTOTYPES
void print_headings(); // print table headings
float compute ( float&, float&, char);
////////////DRIVER FUNCTION
main()
{
print_headings(); //Print table heading
int result; // the result of the calculations
char oper_num; // the user-specified operator
int value; // value specified after the operator
result = 0; // initialize the result
// loop until the 'q' command is reached
while (1)
{ //loop starts with 1
cout << "Result: " << result << '\n';
cout << "Enter operator and number: "; //user is prompted for input
cin >> oper_num;
if ((oper_num == 'q') || (oper_num == 'Q')) //quit program when 'q' is
//reached
break;
cin >> value;
compute (Result, value, oper_num);
cin >> oper_num;
}
return (0);
} //end main()
///////////HELPER FUNCTION IMPLEMENTATIONS
void print_headings()
//Prints table headings to standard output
//Precondition: table constants have been pre-established
//Postcondition: table header written to standard out, in preparation for values
//library Utilities: iostream
{
//Print intro statement and headings
cout << endl;
cout << "*************************************************************" << endl;
cout << " calculator.cc " << endl;
cout << " This is a four-function calculator that add, " << endl;
cout << " subtracts, multiplies,and divides simple integers, " << endl;
cout << " and maintains an ongoing accumulated result (starting with 0) " << endl;
cout << " Program by Debbie_lee104 " << endl;
cout << " Jul. 13, 2007 " << "\n\n";
cout << "**************************************************************" << endl;
cout << endl;
} //End print_headings()
float compute ( float &Result, float &value, char oper_num)
{
switch (oper_num)
{
if (oper_num == '+') //perform addition when the operator is a plus(+)
{
result += value; //value specified after the operation
}
else if (oper_num == '-')
{
result -= value;
}
else if (oper_num == '*')
{
result *= value;
}
else if (oper_num == '/') {
if (value == 0) {
cout << "Error: Attempt to Divide by zero\n";
} else
result /= value;
} else {
cout << "Error:'x' is not a valid operator\n";
}
}
}
/////////END OF PROGRAM FILE//////////////////
//////COMPILER DIRECTIVES
#include <iostream> //provides cin and cout
#include <cstdlib> //provides exit_success
#include <cmath>
using namespace std; //allows all standard library to be used
/////////FUNCTION PROTOTYPES
void print_headings(); // print table headings
float compute ( float&, float&, char);
////////////DRIVER FUNCTION
main()
{
print_headings(); //Print table heading
int result; // the result of the calculations
char oper_num; // the user-specified operator
int value; // value specified after the operator
result = 0; // initialize the result
// loop until the 'q' command is reached
while (1)
{ //loop starts with 1
cout << "Result: " << result << '\n';
cout << "Enter operator and number: "; //user is prompted for input
cin >> oper_num;
if ((oper_num == 'q') || (oper_num == 'Q')) //quit program when 'q' is
//reached
break;
cin >> value;
compute (Result, value, oper_num);
cin >> oper_num;
}
return (0);
} //end main()
///////////HELPER FUNCTION IMPLEMENTATIONS
void print_headings()
//Prints table headings to standard output
//Precondition: table constants have been pre-established
//Postcondition: table header written to standard out, in preparation for values
//library Utilities: iostream
{
//Print intro statement and headings
cout << endl;
cout << "*************************************************************" << endl;
cout << " calculator.cc " << endl;
cout << " This is a four-function calculator that add, " << endl;
cout << " subtracts, multiplies,and divides simple integers, " << endl;
cout << " and maintains an ongoing accumulated result (starting with 0) " << endl;
cout << " Program by Debbie_lee104 " << endl;
cout << " Jul. 13, 2007 " << "\n\n";
cout << "**************************************************************" << endl;
cout << endl;
} //End print_headings()
float compute ( float &Result, float &value, char oper_num)
{
switch (oper_num)
{
if (oper_num == '+') //perform addition when the operator is a plus(+)
{
result += value; //value specified after the operation
}
else if (oper_num == '-')
{
result -= value;
}
else if (oper_num == '*')
{
result *= value;
}
else if (oper_num == '/') {
if (value == 0) {
cout << "Error: Attempt to Divide by zero\n";
} else
result /= value;
} else {
cout << "Error:'x' is not a valid operator\n";
}
}
}
/////////END OF PROGRAM FILE//////////////////