That might be a bad example... Im not sure but I think changing the int variable into a string variable changes the value of the number. From what I understand your changing an int into a number viewed for its ascii value when you change it into a string and that could create a problem all by itself. I might be wrong but I think it would be better to stick with an int variable that changes its value by multiplying its self by the next factorial which would be determined by the loop.
******************************************************************************************
This was written before I notice that the code wasn't written by the person searching for the solution. The only reason I'm including it is because it might help both of you... If I'm wrong someone please tell me.....
So, @condor, <--(initially this was not intended for you but after recognizing it wasn't created by @janineee29, I put your name in it too.. )
Before I say anything about your code, I want you to know your not doing a bad job at all. Although some of the tools your using for the problem at hand, seem to be used in a manor that I consider, unorthodox. (By tools I mean
loops are a tool for programming that create a reoccurring pattern,
int/string/double & so on are tools of programming to store input/data and
if statements are a way to control the structure of a prog.: if (this is/is not what I want) { Do something else that I want...} ) Although, sometimes in programming being unorthodox is not only beneficial but essential, I don't believe this is one of those cases.
What's the purpose of the string?
My opinion:
-Unless your teacher is requiring a string to be used , which was not mentioned, I don't think you need one. If its part of the assignment ok, but if not ask your self why a string. The reason Im having you ask yourself stuff like this is because you need to view everything as a way to manipulate and store data. ---(Also, @condor, you forgot the header.)
Another thing, you need to know how some of these tools work before you try to use them. By converting the int's into strings,
from my understanding, you would be converting what was a numerical integral value to the value provided via the ascii chart. Now if your intent is to use the string as a way of creating an array why not just make an array of int type? If anything Im saying is confusing just ask...
Now, lets look at the problem at hand and see what it is that your trying to do:
how generate a Factorial Chart from 1 to N?
|
Here is an example of how I feel you would find the best result.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
int factorial;
cout<<"Enter the number you want the factorial for: ";
cin>>factorial;
cout<<endl;
cout<<factorial<<" ";
for(int i = factorial, i > 0, --i){
factorial = factorial * i;
cout<<faactorial<<" ";
}
cout<<endl;
| |
*** I might have done something wrong in the math part but still it should be close... Also if you want to store each factorial separately with an array then try an int array instead of a string. Any questions about int arrays just ask..
If any of this repeats its because I made the mistake of reading the code of condor and thought it was the code of @janineee29. Sorry...
Hope this helps and if I am wrong someone anyone please tell me.