So I was trying out this other exercise where I wrote a program that calculates the factorial of an integer, and so I managed to get that part right:
//File: calculatingfactorials.cpp
#include <iostream>
using namespace std;
int main (void)
{
int K;
int factorial;
factorial = 1;
cout << "Enter an integer: ";
cin >> K;
CHECK:
if (K > 0)
{
factorial = K * factorial;
K = K - 1;
goto CHECK;
}
else
cout << factorial << "\n";
}
I am now supposed to modify the code so that if I enter a number that is not an integer, e.g. -3, it will output that "Factorial cannot be calculated" and I do not know where to even begin the modifications, so any help please.
Make a function called factorial. Use if statements to check whether a positive number is given or not. If yes, then use recursion to find the factorial (return n*factorial(n-1);). Otherwise, return -1. In your main code, check the return value. If its positive, print the return value as its the factorial. If its -1, print the error.
This may sound a bit complicated but its not.