I have only been learning c++ for the last 3 weeks. Here is my code for a calculator, it works ok, but I was wondering if my code is any good. Also, I can quit with q at a character prompt, but I don't know how to quit with q at an integer prompt. Any suggestions for me would be greatly appreciated. One more thing is it alright to use continue the way I have?
Thanks,
D.B
#include <iostream>
#include <cmath>
#include<string>
using namespace std;
int main()
// declaring variables:
{
double value1,value2;
char operation;
int x=0;
cout<<endl;
cout<<"\t\t"<<" "<<"Welcome to my calculator : "<<endl;
// enter value to compute
do {
cout<<" "<<"________________________________________________________________________\n\n";
cout<<"Enter a value you want to work with : ";
cin >>value1;cout<<endl;
// enter operation want performed
cout<<"Enter the operation you want to perform (enter q or Q to exit program) "<<endl<<endl;
cout<<"Choose from these options + - * / ^ % ! ";
cin >> operation; cout<<endl;
// entering q or Q to exit program
if (operation == 'q' || operation == 'Q')
{cout<<"\t\t\t\t\tGoodbye for now! "<<endl<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
// computation ^ square
if (operation == '^')
{
cout<<"\t\tThe square of "<<value1<<" = "<<value1 * value1<<endl<<endl;
continue;
}
// computation factorial ! convert value1 to an integer in order to calculate
//
{
int n =int(value1);
if(value1 > 12) {cout <<"\tNo factorial of any number greater than 1, try again 2 "<<endl<<endl;continue;}
if (n <0 ) {cout <<"\tNo factorial of a negative number, try again "<<endl<<endl;continue;}
int f = 1;
while (n >1)
f*=n--;
if (operation == '!')
{
cout<<"\t\tThe factorial of "<<value1<<" = " <<f<<endl<<endl;
continue;
}
}
//enter another value if needed
cout<<"Enter second value you want to work with : ";
cin >>value2;cout<<endl;
// computation remainder % convert value1 and value2 to integers in order to calculate
{
int n = int(value1);
int m = int(value2);
if (operation == '%') cout<<"\t\tThe remainder of "<<value1<<" / "<<value2<< " = "<< n % m<<endl<<endl;
}
// computations + - * /
if (operation == '+') cout<<"\t\t"<<value1<<" + "<<value2<<" = "<< value1 + value2<<endl<<endl;
elseif (operation == '-') cout<<"\t\t"<<value1<<" - "<<value2<<" = "<< value1 - value2<<endl<<endl;
elseif (operation == '*') cout<<"\t\t"<<value1<<" x "<<value2<<" = "<<value1 * value2<<endl<<endl;
elseif (operation == '/') cout<<"\t\t"<<value1<<" / "<<value2<<" = "<< value1 / value2<<endl<<endl;
x++;
} while (x < 100);
/* Exit program (pause so the program output window
doesn't close until you press a key */
system("PAUSE");
return EXIT_SUCCESS;
}