Hello everyone...

I have 2 questions and it would be great if you guys can help me on these.

I wrote a factorial program and it works already.
The problem is when it comes to higher outputs.

For example:
11 P 5 = 55440 // this works so far
but
13 P 5 = 154440 // but my output is 47917

Can someone help me out on this?

Second thing is, how do i avoid that someone will enter letters? :-)

Btw my code is:

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

long factorial (long n)
{
if (n > 1)
return (n * factorial (n-1));
else
return (1);
}

int main ()
{

cout<< " WELCOME TO THE 2 BASED FACTORIAL CALCULATOR " << endl << endl;

start:;

long number;
cout << "Please type a number: ";
cin >> number;

long number2;
cout << endl << "Please type a 2nd number: ";
cin >> number2;

long number3;

number3 = factorial(number) / factorial(number - number2);

{

if (number < number2)

{

cout<< "Please let the first number be bigger than the second number." << endl << endl;
goto start;

}

if (number >= number2)

{

cout << endl << number << " P " << number2 << " = " << number3 << endl << endl;
}

}

goto start;

return 0;
}``````
4
Contributors
13
Replies
14
Views
8 Years
Discussion Span
Last Post by MyrtleTurtle
Featured Replies
• 1
WaltP 2,905   8 Years Ago

[QUOTE=MyrtleTurtle;1151698]Oh yeah...I was going to say that, too. Don't use goto. And, you can't actually control what the user will input. But your program can handle it if they do not enter what you want them to, if you add the code I suggested. :)[/QUOTE] And he can fail the …

I am not sure about the first part but I can answer the second part. One way to ensure that the input is good (the user enters a number) is to do this:

``````void clearError(long int& num){ //pass number in by reference so it can be changed within the function
cin.clear(); //clear the error state
//ignore characters left in the buffer up to and including the new line character
cin.ignore(numeric_limits<streamsize>::max(), '\n');
//allow them to reenter number
cout<<"That is not a number. Try again: ";
cin>>num;
}

int main ()
{

cout<< " WELCOME TO THE 2 BASED FACTORIAL CALCULATOR " << endl << endl;

start:;

long number;
cout << "Please type a number: ";
cin >> number;

while(!cin.good()){
clearError(number);
}``````

I like to put it in a function so it can be reused, but you don't have to do it that way.

Edited by MyrtleTurtle: I usually think of a better way to code it, after posting

1) Are you certain the program is handling 13! correctly?

2) Not easily. Do you understand how to parse strings? If so, you can try it. If not, you might want to wait a while to handle that.

3) Do not use `goto` . It is considered bad programming practice since a loop can easily be used instead and to better effect.

Oh yeah...I was going to say that, too. Don't use goto.

And, you can't actually control what the user will input. But your program can handle it if they do not enter what you want them to, if you add the code I suggested. :)

Oh yeah...I was going to say that, too. Don't use goto.

And, you can't actually control what the user will input. But your program can handle it if they do not enter what you want them to, if you add the code I suggested. :)

And he can fail the project for using code that was not taught and has no business using. The instructor will know that the code was given to him and he's getting others to write his code for him.

Please keep in mind who is asking and their level of knowledge before showing your prowess as a programmer and blowing their mind with higher level techniques. :icon_wink:

1) Are you certain the program is handling 13! correctly?

2) Not easily. Do you understand how to parse strings? If so, you can try it. If not, you might want to wait a while to handle that.

3) Do not use `goto` . It is considered bad programming practice since a loop can easily be used instead and to better effect.

Hello...

To number1:
What is the difference between 12 and 13? (except an increase of 1) :-)

2:
I know strings but parse strings sounds new to me.

3:
I know, sorry i will try to get rid of this habbit ;-)

And he can fail the project for using code that was not taught and has no business using. The instructor will know that the code was given to him and he's getting others to write his code for him.

Please keep in mind who is asking and their level of knowledge before showing your prowess as a programmer and blowing their mind with higher level techniques. :icon_wink:

First thanks to both of you for your help.

Second i did the code by my own and it works somehow.
It´s not like i was asking for everything :-P
I just need little help by these 2 parts. I guess this will be okay by the teacher.

I am not sure about the first part but I can answer the second part. One way to ensure that the input is good (the user enters a number) is to do this:

``````void clearError(long int& num){ //pass number in by reference so it can be changed within the function
cin.clear(); //clear the error state
//ignore characters left in the buffer up to and including the new line character
cin.ignore(numeric_limits<streamsize>::max(), '\n');
//allow them to reenter number
cout<<"That is not a number. Try again: ";
cin>>num;
}

int main ()
{

cout<< " WELCOME TO THE 2 BASED FACTORIAL CALCULATOR " << endl << endl;

start:;

long number;
cout << "Please type a number: ";
cin >> number;

while(!cin.good()){
clearError(number);
}``````

I like to put it in a function so it can be reused, but you don't have to do it that way.

Do i have to include something in the header?
Because i add your codes into mine and i have errors on max (not a member of '`global namespace) and numeric_limits (undeclared identifier)

First thanks to both of you for your help.

Sure. Did you figure out what was wrong with 13? What was it?

Second i did the code by my own and it works somehow.
It´s not like i was asking for everything :-P
I just need little help by these 2 parts. I guess this will be okay by the teacher.

I was not suggesting you wanted someone to write your code. What I was saying is that by using someone else's code that you don't understand in your code, your instructor may believe someone else wrote it all. Keep your code at a level you understand.

Sure. Did you figure out what was wrong with 13? What was it?

I was not suggesting you wanted someone to write your code. What I was saying is that by using someone else's code that you don't understand in your code, your instructor may believe someone else wrote it all. Keep your code at a level you understand.

It´s not 13 alone... every number above 13 gives the wrong answer.
Somehow i think it hast to do with my decleration that there is not enough memory. I tried to use an array instead of long but id didn´t work either. Okay maybe it will work but i had errors :-D
To 13 again i think to know its the Ascii for the enter or return key.

My teacher is cool because i show a lot of effort... and using higher level of coding makes me better in the long run i guess.

There is a very easy way to parse strings using atoi().

Actually, 13! is larger than the maximum size your variable can hold. That's why it doesn't work. Nothing to do with ASCII 13.

Okay Thanks to all of you...

I just pass it as it is can´t figure it out now.

Thanks again!!!

And he can fail the project for using code that was not taught and has no business using. The instructor will know that the code was given to him and he's getting others to write his code for him.

Please keep in mind who is asking and their level of knowledge before showing your prowess as a programmer and blowing their mind with higher level techniques. :icon_wink:

Oh....sorry! I was just trying to help.

I do see your point, but in my classes, I always look for other ways to do things, even if the instructor hasn't taught it yet. I try to make my programs the best they can be, regardless of what level has been taught in class. To me, it's best to learn everything you can instead of just the (very limited) amount that they teach in school.

But I don't want to get anyone in trouble, either. :(