Simple Calculator

I have built a code for class and it does nto work. I do not understand why. Can someonne look over the code and give me some explination to why it does not build.

///*specification: Raymond Henry Sr Lab2 exercise 2
//My Calculator

#include <iostream>
using namespace std;


int firstNum = 0;
int secondNum = 0;
int operator = 0;
int answer = 0;

int main()

{

cout << "Welcome to My Calculator: "<< endl;
cout << "Please Enter the first number followed by the second number followed by the operation you would like to accomplish: " << endl;
cout << "Enter First number: " << endl;
cin >> firstNum << endl;
cout << "Enter Second number: " << endl;
cin >> secondNum << endl;
cout << "Select an operation: " << endl;

enum operator{Exit = 0, Addition = 1, Subtraction = 2, Multiplication = 3, Division = 4, Modulus Division = 5};

cin >> Operator;

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

else

if(operator < 0 || operator > 2)
{ answer = firstNum + secondNum;
cout << firstNum;
cout << " + ";
cout << secondNum;
cout << " = ";
cout << answer << endl;}

else

if(operator < 1 || operator > 3)
{ answer = firstNum - secondNum;
cout << firstNum;
cout << " - ";
cout << secondNum;
cout << " = ";
cout << answer << endl;}

else

if(operator < 2 || operator > 4)
{ answer = firstNum * secondNum;
cout << firstNum;
cout << " * ";
cout << secondNum;
cout << " = ";
cout << answer << endl;}

else

if(operator < 3 || operator > 5)
{ answer = firstNum / secondNum;
cout << firstNum;
cout << " / ";
cout << secondNum;
cout << " = ";
cout << answer << endl;}

else

if(operator < 4 || operator > 6)
{ answer = firstNum % secondNum;
cout << firstNum;
cout << " % ";
cout << secondNum;
cout << " = ";
cout << answer << endl;}

else
{ cout << "Error: " }

};
};
};
};
};

return 0;

}
Use the #format when uploading code.

There are a few bugs in your program, read the error messages you get and you will be able to find them. Few notes to point you in the right direction:
-you cant use reserved key-words as name for variables
-i dont know "enum", but your program doesnt recognize it either. Maybe you have to include another library for that?
-For every open-bracket "{" you use you need only one close-bracket "}"
-you cant use the inputstream for any output (including new lines)
Last edited on
Well one of my classmates says my code should look like this but changed to handle the calculations for a calculator. I am so confused. This code produces calculations for a circle. Our code is supposed to do various calculations. The part where I am having the trouble is converting the perform calculations part.

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

}
Last edited on
> In the first code you posted, you are using operator as variable name, as enum and it is a keyword. This will be very confusing for your compiler
> You are using many times things like cin >> firstNum << endl;
but it should be
1
2
cin >> firstNum;
cout << endl;
> In the enumerator the value Modulus Division contains a space, change it to Modulus_Division (why did you declare this enum if you didn't use it?)
> At the end you are missing a semicolon
1
2
else
     { cout << "Error: "; }
> You have to delete all the extra };
> I suggest you to use a char to get the operator, not an int and to use a float or a double for the number used in the calculations in order to have more precise results on division
Last edited on
Bazzy, we have to make the code similar to the second one I posted and it has to calculate and give the answer for the calculation. They require us to have a enumerator and switch statement. I am so confused. I need to eliminate the junk for the circle and add the mathmatical equations fo rthis program to perform the math functions.
Could have helped you, but solving homework is forbidden, sorry.
Topic archived. No new replies allowed.