hi, i wanted to find the factorial with the same problem that I had the previous time:

here's the code that I wrote, so how can i find the factorial and display it to the user?

``````#include <iostream>
#include <cmath>
#include <string>
using namespace std;

int main(void)
{
string input ;
int a, b;
char Doagain;

do{
cout << "What would you like to do? (Add, Subtract, Multiply, Divide) Or if you would like to do something else, type other. ";
cin >> input;

if (input == "other")
{
char inputtoo;

cout << "Would you like to find a factorial? (Y/N) ";
cin >> inputtoo;

switch (inputtoo)
{
case 'Y':
{
cout << "Input the number you would like to find the factorial for: ";
cin >> a;
for(int b; b=1; b--);
b = a;
cout << "The factorial of is" << a << endl;
}
case 'N':
cout << "Why did you select this? " << endl;
}

}
else if (input == "Add")
{
cout << "Enter a value: ";
cin >> a;
cout << "Enter another value: ";
cin >> b;
cout << "The sum of the numbers is " << a + b << endl;
}
else if  (input == "Subtract")
{
cout << "Enter a value: ";
cin >> a;
cout << "Enter another value: ";
cin >> b;
cout << "The difference of the two numbers is " << a - b << endl;
}
else if  (input == "Multiply")
{
cout << "Enter a value: ";
cin >> a;
cout << "Enter another value: ";
cin >> b;
cout << "The product of the two numbers is " << a * b << endl;
}
else if   (input == "Divide")
{
cout << "Enter a value: ";
cin >> a;
cout << "Enter another value: ";
cin >> b;
cout << "The quotient of the two numbers is " << a / b << endl;
cout << "And the remainder is " << a % b << endl;
}

cout << "Would you like to do it again? (y/n) ";
cin >> Doagain;
}while (Doagain == 'Y' || Doagain == 'y');

return 0;
}``````

so how can i change this code so that the factorial could also be found? for the for loop here it's showing an error :limac@limac-kubuntu:~/Desktop\$ g++ -o math math.cpp
math.cpp: In function ‘int main()’:
math.cpp:31: warning: name lookup of ‘b’ changed
math.cpp:10: warning: matches this ‘b’ under ISO standard rules
math.cpp:30: warning: matches this ‘b’ under old rules

any help would be very appreciated!

Thx

## All 4 Replies

``````cin >> a;
for(int b; b=1; b--);
b = a;``````

The b is already declare once at the first of the function; you seem to declare it again which cause the syntax error. Moreover, you failed to use for loop. Here is the syntax of for loop: for( initialization, condition, increasement/decreasement ). In the condition part, you use = (assign operator) instead of == (equalition). In this case, you are experience the infinitive loop because b variable is always 1 (all the non-zero integers are true, zero mean false). Remember, the for loop keep looping until the condition became false. If your condition is always true, then it will loop endlessly.

``````b = 1;
cin >> a;
for ( ; a > 0; a--)
b *= a;``````

This would solve your problem, but you might ask me why I would write like this? where is my initialization? a variable recieves the input of the user to perform the calculation, which is my initialization. a > 0, this is my condition, I tell my program to loop until a's value is 0 or smaller than 0. a--, is my decreasement.

(Sorry, I was forgetten to initialize b=1 and to change = to *=)

thanks invisal but, it didn't cause any compilation error but for any number i put in it is showing -1 as the result

thanks invisal but, it didn't cause any compilation error but for any number i put in it is showing -1 as the result

Which code is giving you -1? Your original code? As posted, your factorial code actually results in an infinite loop.

Consider what it's doing:

``````cin >> a;  //get the user input, OK

for(int b; b=1; b--);  //sets b to - nothing!  Tests(?) b, but actually
// sets be to 1 which evaluates to TRUE, so loop
// proceeds. Semicolon following loop statement
// is an empty statement,  so no action occurs.
//  b gets decremented (now 0), then goes back to
//   the test, where it gets set back to 1 (TRUE).
// Repeat till you kill the running process.

b = a;      //assuming this is what you meant do do as the loop body, why?``````

thanks invisal but, it didn't cause any compilation error but for any number i put in it is showing -1 as the result

the code given by invisal is absolutely correct.. it must work for you..

and one more thing..in your original code you worked for factorial in a for loop (which is incorrect ,as invisal explained) ...you are outputting a as the factorial of a `cout << "The factorial of is" << a << endl;` which is incorrect...

so output b which is holding the factorial of a

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.