I know it is not that great, just a simple calculator. I would be interested in accolades and criticisms.
two things which I couldn't figure out.
one: is the limited amount of precision only 7 didgits - from what i could find this is a limitation of the computer. but isn't there away around this? I thought
cout << setprecision(15);
woud do this but it doesn't seem to.
two: when the user puts in more than one character in the final exit loop, the loop checks all the characters going through the loop for each character. How can you get it to just check the first character and discard the rest?
#include <iostream>
#include <limits>
#include <iomanip>
using namespace std;
//***************Function Declarations********************
float fadd (float x, float y);
float fsub (float x, float y);
float fmul (float x, float y);
float fdiv (float x, float y);
//**************Function Definitions************************
float fadd (float x, float y)
{
float total;
total = x + y;
return total;
}
float fsub (float x, float y)
{
float total;
total = x - y;
return total;
}
float fmul (float x, float y)
{
float total;
total = x * y;
return total;
}
float fdiv (float x, float y)
{
float total;
total = x / y;
return total;
}
//*****************Start Main Function********************
int main()
{
cout << ".........................................\n";
cout << ". .\n";
cout << ". Simple Calculator .\n";
cout << ". by Prometheus .\n";
cout << ". copyright 2009 .\n";
cout << ". General Public Non-Commercial License .\n";
cout << ". .\n";
cout << ".........................................\n\n";
cout << "To end Program at any time, type <ctrl>-c \n";
cout << "This calculator is only significant upto 7 digits \n\n\n";
cout << "Begin by entering the first number or \nthe complete expression in the form: 5+5 \n\n";
char exit = 'n';
cout << setprecision(15);
//****************primary while loop to determine exit condition*************
while(exit!='x' && exit!='X')
{
float x, y, z, total;
char op;
exit = 'n';
//***************secondary while loop to determine new or continue status******
while (exit == 'n' || exit == 'c')
{
if (exit == 'n')//If it is a new calculation do this
{
cout << "\nfirst number: ";
while(!(cin >> x))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "\nenter a valid number please: ";
}
cout << "\noperation: ";
cin >> op;
while (((op != '+') && (op != '-') && (op != '*') && (op != '/')))
{
cout << "I don't understand your operation, please try again: ";
cin >> op;
}
cout << "\nsecond number: ";
while(!(cin >> y))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "\nenter a valid number please: ";
}
}//close brace - if exit is n
if (exit == 'c')//If it is a continueing Calculation do this
{
cout << "\nfirst number: " << z;
cout << "\n\noperation: ";
cin >> op;
while (((op != '+') && (op != '-') && (op != '*') && (op != '/')))
{
cout << "I don't understand your operation, please try again: ";
cin >> op;
}
cout << "\nsecond number: ";
while(!(cin >> y))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "\nenter a valid number please: ";
}
}//close brace - if exit is c
switch (op)//determines which function to call depending on the operation sign
{
case '+':
if (exit == 'n')
total = fadd (x, y);
else
total = fadd (z, y);
break;
case '-':
if (exit == 'n')
total = fsub (x, y);
else
total = fsub (z, y);
break;
case '*':
if (exit == 'n')
total = fmul (x, y);
else
total = fmul (z, y);
break;
case '/':
if (exit == 'n')
total = fdiv (x, y);
else
total = fdiv (z, y);
break;
default:
break;
}
//********************Output*************************************
if (exit == 'n')
cout << " \n\n " << x << " " << op << " " << y << " = " << total << endl;
else
cout << " \n\n " << z << " " << op << " " << y << " = " << total << endl;
cout << " \n\n ";
cout << "\nRunning Total: " << total << " \n\n " << endl;
z = total;
//********************end of loops - asking user for instructions
cout << " c to continue with running total, \n n for a new calculation, \n x to exit program: ";
cin >> exit;
cout << "\n\n";
short i=0;
while (((exit != 'c') && (exit != 'n') && (exit != 'x') && (i < 5)))
{
i=i+1;
cout << " Please choose from the availabe options (c, n, x): " << endl;
cin >> exit;
if (i >= 5)
{exit = 'x';}
}
}// close brace - while loop for continue or new
}//close brace - while loop for exit
return 0;
}//close brace for main function