
10-23-04, 03:37 PM
|
|
New Member
|
|
Join Date: Oct 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Calculator coding...need help =)
Hi! I'm doing this project and i need help....it's a calculator program and has to do this:
welcome to the super dooper calculator
Usage:
Enter the operator followed by the operand and the SDC will show you the accumulated total. Enter Q to quit or C to enter the contents. You can use the following operators:
+ add
- subtract
* multiply
/ divide
^ power
q or Q quit
c or C clear total
h or H show help
total: 0
+ 5.0 //user enters line
total: 5
- 3 //user enters line
total: 2
c //user enters line - clears total
total: 0
% 34 //user enters line - bad input
bad input: '%' is not a valid operator
total: 3
q //user enters line - wants to quit
good bye.
This is what i have so far:
/************************************************** *******PROGRAM #3 -
This program is a simple calculator. It allows users to
perform basic functions such as add, subtract, multiply,
divide and to find the square root of a number. It will also
allow users to get help, clear the total or quit the
program.
************************************************** *******/
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using std::setw;
using namespace std;
//declaring functions
void help();
bool inputData(char &theOperator, double &theOperand);
double calulate(double total, char &theOperator, double &theOperand);
int _tmain(int argc, _TCHAR* argv[])
{
//declaring variables
bool done = false;
char theOperator; //symbol
double theOperand; //number
double total = 0;
cout<<"Welcome to the Super Dooper Calculator."<<endl<<endl;;
help();
cout<<endl<<endl;
cout<<"\nTotal: "<<total<<endl;
while (!done)
{
done = inputData(theOperator, theOperand);
calulate(total, theOperator, theOperand);
cout<<"Total: "<<total<<endl;
}
}
void help()
{
cout<<"Usage: "<<endl;
cout<<"Enter the operator followed by the operand and the "<<endl;
cout<<"SDC will show you the accumulated total. Enter Q to quit "<<endl;
cout<<"or C to enter the contents. You can use the following operators: "<<endl<<endl;
cout<<" + "<<setw(5)<<"add"<<endl;
cout<<" - "<<setw(5)<<"subtract"<<endl;
cout<<" * "<<setw(5)<<"multiply"<<endl;
cout<<" / "<<setw(5)<<"divide"<<endl;
cout<<" ^ "<<setw(5)<<"power"<<endl;
cout<<" q or Q "<<setw(3)<<"quit"<<endl;
cout<<" c or C "<<setw(3)<<"clear total"<<endl;
cout<<" h or H "<<setw(3)<<"show help"<<endl;
}
bool inputData(char &theOperator, double &theOperand)
{
bool cont = true; // While Will Repeat Unless cont = false
bool toQuit = true; // Default Value Of True Unless Changed
char symbol = theOperator; // operator
double number = theOperand; //operand
while (cont)
{
if (symbol != 'Q' || symbol != 'q')
{
cin>>symbol;
cin>>number;
cont = false; // Stop Function
toQuit = true;
}
}//End While
return toQuit; // Returns True Or False To End
}
double calulate(double total, char &theOperator, double &theOperand)
{
char decision = theOperator;
double num = theOperand;
switch (decision)
{
case '+':
{
total = total + num;
cout<<"Total: "<<total<<endl<<endl;
break;
}
case '-':
{
total = total - num;
cout<<"Total: "<<total<<endl<<endl;
break;
}
case '*':
{
total = total * num;
cout<<"Total: "<<total<<endl<<endl;
break;
}
case '/':
{
total = total / num;
cout<<"Total: "<<total<<endl<<endl;
break;
}
/* case '^':
{
total = total ^ num;
cout<<"Total: "<<total<<endl<<endl;
break;
} */
case 'c':
case 'C':
{
total = 0;
cout<<"Total: "<<total<<endl<<endl;
break;
}
default:
{
cout<<"wrong!"<<endl;
}
}
return total;
}
i hope you can help! thanks!! 
|