My problem is this: I can't figure out how to take the value generated by the factorial function and print it to the screen.
When I try to print it out in the factorial function loop, nothing is printed out.
When I print it out in the main function, it just prints out the initial value of the variable.
I thought that if it was a global variable both functions could access and modify it, but I guess I am having a conceptual misunderstanding.
Also, I am doing the factorial incorrectly...If the user inputted numbers were 1 and 7, I want it to start by calculating the factorial of 1, then 2, and on until 7.
int firstNumber;
int secondNumber;
int greaterNumber;
int factorial;
int secondFactorial;
int multiplyFactorial;
int smallerNumber;
/* this function should calculate the factorial of all numbers that are larger than the smaller number (variable smallerNumber, entered by the user) and smaller than the larger number (variable greaterNumber, also entered by the user) and print the factorial*/
int factorialFunct(int factorial)
{
do
{
int secondFactorial = factorial + 1;
int multiplyFactorial = secondFactorial * factorial;
return 0;
}
while (multiplyFactorial >= smallerNumber && multiplyFactorial <= greaterNumber);
std::cout << factorial << std::endl;
}
int main()
{
/*this takes the input for the two numbers from the user and makes sure both numbers are positive*/
do
{
std::cout << "Enter a positive number!\n";
std::cin >> firstNumber;
}
while (firstNumber <= 0);
if (firstNumber >= 0)
do
{
std::cout << "How about another?\n";
std::cin >> secondNumber;
}
while (secondNumber <= 0);
{
if (secondNumber >= 0)
/*this is the call to the factorial function above. it passes the number 1 in to be calculated*/
factorialFunct(1);
}
/*this assigns the two numbers entered in to either the greaterNumber variable or the smallerNumber variable*/
{
if (firstNumber > secondNumber) {
greaterNumber = firstNumber;
smallerNumber = secondNumber;
}
else if(secondNumber > firstNumber) {
greaterNumber = secondNumber;
smallerNumber = firstNumber;
}
else if(firstNumber == secondNumber) {
greaterNumber = firstNumber;
smallerNumber = secondNumber;
}
}
}
well. you said that by making the variable global they will be visible in both functions. It's true, but if you declare it again in that function, the function will use the local variable.
1 2
int secondFactorial = factorial + 1;
int multiplyFactorial = secondFactorial * factorial;
That is one example where you do it. secondFactorial and multiplyFactorial are already global. If you declare them again, you will use the variables you declared inside the scope of that function.
In a function, if it runs over return ; it ends that function and it returns to the function that called it. that means that...
#include <iostream>
int Factorial(int factorial)
{
// declares result and gives it the value 1.
// we multiply it so it can't start with the value 0
int result=1;
// we get the right value here.
for(int i=1; i<= factorial; i++)
{
result = result * i;
}
// it returns the value :)
return result;
}
int main()
{
// declares variable x
int x;
// prints that text;
std::cout<< "Enter a number: ";
// gets some input from the user
std::cin>>x;
// this runs only if the input is negative
while(x<0)
{
// prints this text
std::cout<<"Wrong number. Enter again: ";
// gets input again.
std::cin>>x;
}
// prints the factorial
std::cout<<Factorial(x);
// we get our of the program and everyone is happy :)
return 0;
}