PDA

View Full Version : C++ four function calculator

debbie_lee104
07-16-2007, 03:01 AM

//////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
float compute ( float&, float&, char);

////////////DRIVER FUNCTION

main()
{

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

//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
{
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;

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//////////////////

Gox
07-16-2007, 06:12 AM
It would have been nice if you'd specified what errors you were getting. It makes it much easier for us to help you.

I had to change the following in order for your program to compile:

In main:
int result; AND int value;
TO
float result; AND float value;
Your compute function expects float values to be passed to it, not ints, thus your variables should be declared as floats.

In main:
compute (Result, value, oper_num);
TO
compute(result,value,oper_num);
C++ is Case-Sensitive. Your variable is declared as result NOT Result.

In compute:
float compute ( float &Result, float &value, char oper_num)
TO
float compute ( float &result, float &value, char oper_num)
Same reason as above.

Your switch in your compute function wasn't written properly and wasn't doing what you expected. If you use a switch you shouldn't need the if-statements. Use one of the two options below,

If-Statements

float compute ( float &result, float &value, char oper_num)
{

if(oper_num == '+')
{
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";
}
}

Switch Statement

float compute ( float &result, float &value, char oper_num)
{
switch(oper_num)
{
case '+':
result += value; //value specified after the operation
break;

case '-':
result -= value;
break;

case '*':
result *= value;
break;

case '/':
if (value == 0) {
cout << "Error: Attempt to Divide by zero\n";
}
else
result /= value;
break;
default:
cout << "Error:'x' is not a valid operator\n";
break;
}
}

You have some other logic issues in your code that can cause your program to enter an infinite-loop. I'll let you work on that.

debbie_lee104
07-16-2007, 12:55 PM
Thanks a bunch! I'll work on that.

D.

sage45
07-16-2007, 08:30 PM
/////////FUNCTION PROTOTYPES
float compute ( float&, float&, char);While this will compile, it is generally a good idea to put your function variable declarations in the prototype area as well (It helps you (and anyone else following you) to know which variable is used and which spot they are used in - i.e. -
/////////FUNCTION PROTOTYPES
float compute (float &Result, float &value, char oper_num);Starting this in practice now will help immensely in the future.

There also appears to be a logic error in your application (unless I am misunderstanding the assignment). Here you present this to the user:

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;But your program only allows for the entry of one variable instead of two (integers instead of integer).

Other than that, looks good so far.

-sage-

sage45
07-16-2007, 08:53 PM
Now just a little clarafication on variables and their use in functions.
//////COMPILER DIRECTIVES
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

/////////FUNCTION PROTOTYPES
float AddMe(int A, int B, int C);

////////////DRIVER FUNCTION
main()
{
int Z = 10;
int Y = 20;
int X = 30;

cout << "Value 1 = " << Z << '\n';
cout << "Value 2 = " << Y << '\n';
cout << "Value 3 = " << X << '\n';
cout << "Add them together and you get [" << AddMe(Z, Y, X) << "]"
cout << endl;
return (0);
}

///////////HELPER FUNCTION IMPLEMENTATIONS
int AddMe(int A, int B, int C)
{
return (A + B + C);
}
When thinking of functions and their parameters, you do not have to use the same name as the function parameter itself. As you can see here, I have three integer variables that are instansiated as Z, Y and X respectively. The AddMe function allows for three integer variables to be pass (By value) as parameters. The scope of the AddMe variables (A, B and C) exists within the AddMe function and not within the main function.

I hope this helps to clarify a few things.

-sage-

debbie_lee104
07-17-2007, 12:49 AM
Thank you all- I'm new to this and I appreciate all your comments. My program is working great!

Now i'm trying to create a makefile with the following target: calc, p2_calculator.o, and clean.

I'm trying to follow some instructions from my google search but i'm not sure what i'm doing.
I made a : make p2_calculator and the result was "p2_calculator is up to date". Is this correct because i don't see any new file being created?

Thanks,
debbie_lee104

Gox
07-18-2007, 02:29 AM
The make file's purpose is to compile your code. If your code has already been compiled in the past you'll have a file called, say 'calculator'. When you call make it'll just overwrite this executable and won't actually create a new file.

Now, if your program has been compiled already and you call make to compile it again without changing any of the source files you will indeed get the message "..is up to date". Which means you have not changed any code since the last time you compiled.

On Unix (I don't think it'll work in dos) if you wish to force make to compile anyways you can try typing 'touch *' at the command prompt. This will update the timestamp on your files to the current time, making it look like they've been changed.

I'm not sure that was clearly explained, let us know if you need further clarification.

ralph l mayo
07-18-2007, 04:55 AM
The 'clean' target in your makefile should delete all the products of previous compilations so that you can force a recompile by running 'make clean' and then 'make whatever_other_target'

eg:

clean:
-rm -f *.o *~ <other specific names or paths you told gcc to output to, in your case likely just 'calc'>

(prefixing a command with - in a makefile means a failure for that line will not cause make to report failure of the whole process)