Simple Calculator

Nov 13, 2008 at 3:17pm
Can anyone show me how to make a program do simple mathmatical proceedures? I had to write a program last week to make a simple calculator with enumerators and switch statements that allowed math problems like addition, subtraction, multiplication, division, and Modulus division. I wrote the program several times like the examples given but did not know how to remove the circle calculations and put in the mathmatical equations.
Last edited on Nov 13, 2008 at 3:18pm
Nov 13, 2008 at 3:22pm
post the code here, so we can take a look at it, I think the best way is with a switch statement, but let me see ur code and I'll help you, since I have a calculator of my own.
Nov 13, 2008 at 3:26pm
This is how i should do it:
-ask the user for input, eg:
First value? >> 4 (store into double)
Second value? >> 5 (store into double)
Action? >> + (store into string/char)
-compare the action with possible actions (+-*/) and perform this action with the two number, storing the answer in a double variable. You can use a switch for this.
-return the answer, and ask the user or he wants to perform another caculation (putt the whole code in a while-loop)
Nov 13, 2008 at 3:30pm
///*specification: Raymond Henry Sr Lab2 exercise 2
//My Calculator

#include "stdafx.h"
#include <iostream>
using namespace std;
using namespace myCalculator{



int main()

enum operation{Exit, Addition, Subtraction, Multiplication, Division, Modulus Division};

{

//ask for the user's numbers

double numbers = 0;
cout << endl << "Welcome to My Calculator:\n";
cout << endl << "Please Enter the first number followed by the second number followed by the operation you would like to accomplish: "
cout << endl << "Enter First number: ";
cin >> firstNum;
cout << endl << "Enter Second number: ";
cin >> secondNum;
cout << endl << endl;



// ask the user for desired operation
int operation = 0;
cout << "Choose an operation you would like to perform:\n\n";
cout << "1 - Addition\n";
cout << "2 - Subtraction\n";
cout << "3 - Multiplication\n";
cout << "4 - Division\n";
cout << "5 - Modulus Division\n";
cout << "0 - Exit\n";
cout << "Enter a Number: ";
cin >> Operation;

if(operation < 0 || operation > 5)
{ cout << endl <<"You did not select a valid operation, Program Ended:\n\n"; }

else

//find choice using switch statement
cout << "You Chose: ";
switch (
(HERE IS WHERE I GET LOST)

};
};
};
};
};

return 0;

}
Nov 13, 2008 at 3:32pm
The only thing this code does is asks me to pick a mathmatical operation. It does not do any sort of mathmatical process.
Nov 13, 2008 at 3:34pm
A class mate of mine told me to code the program like this but to exclude the circle processes and add the other math processes.

//specification: this program accepts a the radius of a circle and computes a the diameter
//circumference and Area and displays whichever value the user selects from a menu.
//This is a demonstration program to highlight the core concepts of this weeks lecture
#include <iostream>

using namespace std;

namespace circle{
const double PI = 3.1415;
enum menuOptions {Exit, Circum, Diameter, Area};
}

int main (){
//ask the user for a circle's radius
double userRadius = 0;
cout << endl << "Welcome to the Circle Calculator!\n";
cout << endl << "Enter a Radius: ";
cin >> userRadius;
cout << endl << endl;

//ask the user for his desired calculation
int usersChoice = 0;
cout << "Choose a Calculation Option:\n\n";
cout << "1 - Circumference\n";
cout << "2 - Diameter\n";
cout << "3 - Area\n";
cout << "0 - Exit\n";
cout << "Enter a number: ";
cin >> usersChoice;
if (usersChoice < 0 || usersChoice > 3){
cout << endl << "Invalid choice - program terminated\n\n";
}
else {
//perform calculations
struct circleType {double radius; double area; double diameter; double circum;};
circleType myCircle;
myCircle.radius = userRadius;
myCircle.diameter = myCircle.radius * 2; //radius * 2
myCircle.circum = myCircle.diameter * circle::PI; // 2 * radius * pi
myCircle.area = myCircle.radius * myCircle.radius * circle::PI ; // radius * radius * pi
switch (usersChoice) {
case circle::Exit:
cout << endl << "You choose to exit the program\n\n"; break;
case circle::Circum:
cout << endl
<< "A circle with a radius of "
<< myCircle.radius
<< " has a Circumference of "
<< myCircle.circum << "\n\n"; break;
case circle::Diameter:
cout << endl
<< "A circle with a radius of "
<< myCircle.radius
<< " has a diameter of "
<< myCircle.diameter << "\n\n"; break;
case circle::Area:
cout << endl
<< "A circle with a radius of "
<< myCircle.radius
<< " has an area of "
<< myCircle.area
<< "\n\n"; break;
default:
cout << endl << "Not a valid choose!\n\n";
}

cout << endl << endl << "end of program\n\n";
}

return 0;

}
Nov 13, 2008 at 3:35pm
The place where I get lost is when it asks me to perform calculations I did not know how to change this to make it do the mathmatical calculations I needed. if you motice in my code I have the top portion right I think.
Last edited on Nov 13, 2008 at 3:36pm
Topic archived. No new replies allowed.