Greetings,
This may sound like a silly question but I want to add this factorial function inside my calculator w/o really changing my calculator that I worked hard on to create. How can I add this into my calculator w/o changing everything? Any ideas?
Code:
#include <iostream> //cin, cout, <<, >>
#include <conio.h>
#include <cctype> // for strings
#include <cassert> // for assert()
using namespace std;
#define nl "\n"
int Factorial(int n);
float first, second, answer;
char o, y;
int n;
int main()
{
do {
cout<<"Type a sum (format: a number +,-,*,/ with another number): ";
cin>>first>>o>>second; //inputs number, operator, number
switch (o) // upon your selection, it selects the operator you chose
{
case '+': answer = first + second; break;
case '-': answer = first - second; break;
case '*': answer = first * second; break;
case '/': answer = first / second; break;
default:
cout<<nl<<"Error: format: +,-,*,/ number"<<nl //Error: defaults message
<<"Press any key to continue...";
getch(); //get a character
return 1;
}
cout<<nl<<"The Answer to "<<first<<o<<second<<" is: "<<answer; //output answer
cout<<nl<<"Do you want to do another sum? ";
y = toupper(getche()); // change y to Y
cout<<nl;
} while (y == 'Y'); //if equals y then do loop again
return 0;
}
int Factorial(int n) //want to add it
{
assert(n >= 0);
double product = 1;
while (n > 1)
{
product *= n;
n--;
}
return product;
}