Scientific Calculator using 30 functions in C++

Making a scientific calculator in C++ using functions, (30 of them). User should select and run any function. My code works perfectly fine for options 1 to 9. However, when I chose an option ranging between 10-30, it behaves erratic and returns me an option between 1 to 9 instead, why? How can I fix this?

[code]
Put the code you need help with here.

#include<iostream>
#include<conio.h>
#include<chrono>
#include<math.h>
#include<stdlib.h>
#include<iomanip>


char op;
using namespace std;


void sum()
{

int sum = 0;
int n;
int numberitems;
cout << "How many numbers do you want to add?: \n";
cin >> numberitems;

for(int i=0;i<numberitems;i++)
{
cout<< "Enter number "<<i<<":\n\n" ;
cin>>n;
sum+=n;
}
cout<<" The sum of those numbers is: "<< sum<<endl<<endl;

}
void difference()
{
int difference;
int a,b;
cout<<"Enter any two numbers to find their difference:\n\n";
cout<<"Enter the first number: ";
cin>>a;
cout<<"\nEnter the second number: ";
cin>>b;
difference=a-b;
cout<<"\n "<<a<<"-"<<b<<" ="<<difference<<endl<<endl;
}

void product()

{
int product=1;
int n;
int numberitems;
cout<<" How many numbers do you want to multiply: \n";
cin>>numberitems;
for(int i=1;i<=numberitems;i++)
{
cout<<"\nEnter number "<<i<<" :";
cin>>n;
product*=n;
}

cout<<" Their product is :"<<product<<endl<<endl;
}

void division()
{
float quotient;
float a,b;
cout<<"Enter 2 numbers to find their quotient\n\n";
cout<<"Enter your numerator: ";
cin>>a;
cout<<"\nEnter your denominator: ";
cin>>b;
quotient=a/b;
cout<<"\n"<<a<<"/"<<b<<" = : "<<quotient<<endl<<endl;
}

void Square_Root()
{
float b;
int a;
cout<<"Enter the number whose square root you want to find : ";
cin>>a;
b=sqrt(a);
cout<<"\nThe square root of "<<a<<" is :"<<b<<endl<<endl;
}

void exponent()
{
int a,b,c;
cout<<" Enter the number: ";
cin>>a;
cout<<" Enter the exponent: ";
cin>>b;
c = pow(a,b);
cout<<""<<a<<" to the "<<b<<" power = "<<c<<endl<<endl;
}

void Absolute_Value()
{
int a,b;
cout<<" Enter the number whose absolute value you want to find: ";
cin>>a;
b=abs(a);
cout<<" The absolute value of "<<a<<" ="<<b<<endl<<endl;

}

void factorial()
{
long int f=1;
int c=1,n;
cout<<"Enter number to find its factorial:";
cin>>n;
while(c<=n)
{
f=f*c;
c+=1;
}
cout<<"\n The factorial of "<<n<<" is :"<<f<<endl<<endl;
}
void sin ()
{
float a,b;
cout<<" Enter the number whose Sin you want to find: ";
cin>>a;
b=sin(a);
cout<<"Sin"<<a<<"="<<b<<endl<<endl;

}
void cos ()
{
float a,b;
cout<<" Enter the number whose Cos you want to find: ";
cin>>a;
b=cos(a);
cout<<"Cos"<<a<<"="<<b<<endl<<endl;

}
void tan ()
{
float a,b;
cout<<" Enter the number whose Tan you want to find: ";
cin>>a;
b=tan(a);
cout<<"Tan"<<a<<"="<<b<<endl<<endl;

}
void asin ()
{
float a,b;
float PI = 3.14159265;
cout<<" Enter the number whose aSin you want to find: ";
cin>>a;
b=asin(a)*180.0/PI;
cout<<"aSin"<<a<<"="<<b<<endl<<endl;

}
void acos ()
{
float a,b;
float PI = 3.14159265;
cout<<" Enter the number whose aCos you want to find: ";
cin>>a;
b=acos(a)*180.0/PI;
cout<<"aCos"<<a<<"="<<b<<endl<<endl;

}
void atan ()
{
float a,b;
float PI = 3.14159265;
cout<<" Enter the number whose aTan you want to find.";
cin>>a;
b=atan(a)*180.0/PI;
cout<<"aTan"<<a<<"="<<b<<endl<<endl;

}
void sinh ()
{
float a,b;
cout<<" Enter the number whose sinh you want to find: ";
cin>>a;
b=sinh(a);
cout<<"Sinh"<<a<<"="<<b<<endl<<endl;
}
void cosh ()
{
float a,b;
cout<<" Enter the number whose cosh you want to find: ";
cin>>a;
b=cosh(a);
cout<<"Cosh"<<a<<"="<<b<<endl<<endl;
}
void tanh ()
{
float a,b;
cout<<" Enter the number whose tanh you want to find: ";
cin>>a;
b=tanh(a);
cout<<"Tanh"<<a<<"="<<b<<endl<<endl;
}
void Natural_Log ()
{
float a,b;
cout<<" Enter the number whose ln you want to find: ";
cin>>a;
b=log10(a);
cout<<"ln"<<a<<"="<<b<<endl<<endl;
}
void Logarithm ()
{
float a,b;
cout<<" Enter the number whose Logarithm you want to find: ";
cin>>a;
b=log(a);
cout<<"Log"<<a<<"="<<b<<endl<<endl;
}

void Area_of_a_Triangle ()
{
float base, height, area;
cout<<" Enter the base of the Triangle: ";
cin>>base;
cout<<" Enter the height of the Triangle: ";
cin>>height;
area=((base*height)*0.5);
cout<<"The Area of the Triangle = "<<area<<endl<<endl;
}

void Area_of_a_Rectangle ()
{
float length, width, area;
cout<<" Enter the length of the Rectangle: ";
cin>>length;
cout<<" Enter the width of the Rectangle: ";
cin>>width;
area=length*width;
cout<<"The Area of the Rectangle = "<<area<<endl<<endl;
}
void Area_of_a_Circle ()
{
float PI,radius, area;
PI=3.14159265;
cout<<" Enter the radius of the circle: ";
cin>>radius;
area=(PI)*pow(radius,2);
cout<<" The Area of the Circle = "<<area<<endl<<endl;
}
void Area_of_a_Square ()
{
float side, area;
cout<<" Enter the side of the square: ";
cin>>side;
area=pow(side,2);
cout<<" The Area of the Square = "<<area<<endl<<endl;
}
void Perimeter_of_a_Rectangle ()
{
float length, width, perimeter;
cout<<" Enter the length of the Rectangle: ";
cin>>length;
cout<<" Enter the width of the Rectangle: ";
cin>>width;
perimeter=((2*length)+(2*width));
cout<<" The Perimeter of the Rectangle = "<<perimeter<<endl<<endl;
}
void Area_of_a_Parallelogram ()
{
float base, height, area;
cout<<" Enter the base of the Parallelogram: ";
cin>>base;
cout<<" Enter the height of the Parallelogram: ";
cin>>height;
area=base*height;
cout<<" The Area of the Parallelogram = "<<area<<endl<<endl;
}
void Area_of_a_Trapezoid ()
{
float base_1, base_2, height, area;
cout<<" Enter base_1 of the Trapezoid: ";
cin>>base_1;
cout<<" Enter base_2 of the Trapezoid: ";
cin>>base_2;
area=(((base_1+base_2)*0.5)*height);
cout<<" The Area of the Trapezoid = "<<area<<endl<<endl;
}
void Circumference_of_a_Circle ()
{
float PI,radius,circumference;
PI=3.14159265;
cout<<" Enter the radius of the circle: ";
cin>>radius;
circumference=(2*PI*radius);
cout<<" The Circumference of the Circle = "<<circumference<<endl<<endl;
}
void Perimeter_of_a_Square ()
{
float side, perimeter;
cout<<" Enter the side of the square: ";
cin>>side;
perimeter=side*4;
cout<<" The Perimeter of the Square = "<<perimeter<<endl<<endl;
}
void Volume_of_a_solid_figure ()
{
float length,width,height, volume;
cout<<" Enter the length of the solid figure: ";
cin>>length;
cout<<" Enter the width of the solid figure: ";
cin>>width;
cout<<" Enter the height of the solid figure: ";
cin>>height;
volume =(length*width*height);
cout<<" The Volume of the solid figure = "<<volume<<endl<<endl;
}
void Perimeter_of_parallelogram ()
{
float base, side, perimeter;
cout<<" Enter the base of the parallelogram: ";
cin>>base;
cout<<" Enter the side of the parallelogram: ";
cin>>side;
perimeter =(2*(base*side));
cout<<" The Perimeter of the Parallelogram = "<<perimeter<<endl<<endl;
}

int main()
{
cout<<"******************************* Calcio Calculator **************************************\n";
_sleep(4000);
system("cls");
cout<<"INITIALIZING......................................................................... PLEASE WAIT!!\n";
_sleep(4000);
system("cls");

//system("cls");
do
{

system("pause");
system("cls");


End of code to my question since it could all fit...


cout<<" Categories and List of Functions to Select From \n";
//cout<<"---------------------------------------------------------------------\n";
cout<<" "<<endl;
cout<<" "<<endl;

cout<<" OPERATIONS "<<endl;
cout<<" 1.Addition 2.Subtraction 3.Multiplication 4. Division"<<endl;
cout<<" 5.Square root 6. Exponent 7.Absolute Value 8. Factorials"<<endl;
cout<<" "<<endl;
cout<<" "<<endl;

cout<<" TRIGONOMETRIC FUNCTIONS"<<endl;
cout<<" 9.Sin 10.Cos 11.Tan 12.aSin 13.aCos"<<endl;
cout<<"14.aTan 15.Sinh 16.Cosh 17.Tanh"<<endl;
cout<<" "<<endl;
cout<<" "<<endl;

cout<<" LOGARITHM FUNCTIONS"<<endl;
cout<<"18.Natural Log 19.Logarithm"<<endl;
cout<<" "<<endl;
cout<<" "<<endl;

cout<<"GEOMETRIC FUNCTIONS"<<endl;
cout<<"20.Area of Triangle 21.Area of Rectangle 22.Area of Circle"<<endl;
cout<<"23.Area of Square 24.Perimeter of Rectangle 25.Area of Parallelogram"<<endl;
cout<<"26.Area of Trapezoid 27.Circumference of Circle 28.Perimeter of Square"<<endl;
cout<<"29.Volume of Solid Figure 30. Perimeter of Parallelogram"<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<"press option:";
cout<<"Enter the number for the function that you want to perform : ";
cout<<" "<<endl;
cout<<" "<<endl;

cin>>op;
switch(op)
{
case '1':
sum();
break;
case '2':
difference();
break;
case '3':
product();
break;
case '4':
division();
break;
case '5':
Square_Root();
break;
case '6':
exponent();
break;
case '7':
Absolute_Value();
break;
case '8':
factorial();
break;
case '9':
sin ();
break;
case '10':
cos ();
break;
case '11':
tan ();
break;
case '12':
asin ();
break;
case '13':
acos ();
break;
case '14':
atan ();
break;
case '15':
sinh ();
break;
case '16':
cosh ();
break;
case '17':
tanh ();
break;
case '18':
Natural_Log ();
break;
case '19':
Logarithm ();
break;
case '20':
Area_of_a_Circle ();
break;
case '21':
Area_of_a_Rectangle ();
break;
case '22':
Area_of_a_Circle ();
break;
case '23':
Area_of_a_Square ();
break;
case '24':
Perimeter_of_a_Rectangle ();
break;
case '25':
Area_of_a_Parallelogram ();
break;
case '26':
Area_of_a_Trapezoid ();
break;
case '27':
Circumference_of_a_Circle ();
break;
case '28':
Perimeter_of_a_Square ();
break;
case '29':
Volume_of_a_solid_figure ();
break;
case '30':
Perimeter_of_parallelogram ();
break;


case '0':
exit(0);
default:
cout<<"invalid input" ;
system("cls");
}
}

while(op!='0');

getch();


}

> case '10':
Because op is a char, and things between '' with more than one character are not characters.

Make op an integer, and remove all the single quotes off your cases and comparisons.

Also make op a local variable, there's no need for it to be a global.

> why? How can I fix this?
You should have stopped at 10, and then figured out how to fix the problem.
You were lucky this time that the hole you dug for yourself wasn't that deep.

Your work flow should be code/test in a nice tight loop:
- add one function
- add the case in main
- test it.

NOT write all 30 functions and then wonder why it doesn't work.

adding obligatory 'please use code tags' next time and
its <cmath> <cstdlib> (you are using C headers, these are c++ versions)

did you get to pick the functions? these are odd (creative, but odd). (This is just a ramble that may interest the generation that has never held a scientific calculator due to phones).

+-*/ sin/cos/tan/asin/acos/atan log/exp (base 10 and e at least, so 4 functions) that is 14
sqrt, y^x +/- (*-1 of current value button) and abs is 20 total.
1.0/x 21
hyperbolic and inverse is 6 more trig functions, I forgot those, thats 27
mine (real calculator) has permutation and combination p(a,b) and c(a,b), factorial gives you 30 and I still have a few buttons that i never hit (% and %delta, linear regression) and a few I do (pi constant button, and conversion from hours/min/sec to decimal and reverse, conversion from degrees to radians and reverse).
If I needed the areas and volumes, I have 5 user defined programmable buttons and would have to code those into it (its just macro programming, series of keypresses saved) and a dozen functions to support programming it (conditionals, mostly)

note that the physical calculators had a lot of redundancy. 1/x is silly, we have a division button. Yet I use it all the time, its handy. same for sqrt, you have x^y so x^0.5 is sqrt, but they put the extra button in. You only need 1 log but they provide 2 at least. Software calcs mimic the handy excess buttons and that is fine too, but if you had to do it with less buttons, you could get by with a lot less. A lot of them have an 'inverse' button to avoid 2 buttons for say sin and asin. Instead of that, mine has 2 buttons kinda like alt and ctrl on a keyboard that change every button making each button work as 3 things.
Last edited on
Topic archived. No new replies allowed.