Sunday, August 2, 2009

Calculating factorials, combinations, and permutations with C++?

I'm am trying to create a program where it will calculate factorials, permutations, and combinations where a user types in the type of problem, the numbers involved, and it will display the answer. But when I compile it, there are lots of error messages. I've tried resolving some of them, but nothing worked. Can someone fix these errors and tell me what the problem was? Thanks.





FOR THE SOURCE CODE, CLICK ON THE FOLLOWING LINK:





http://pwnedbyjohn.com/files/files/11814...





You can use the same file hosting service to upload the fixed code, if you need to.

Calculating factorials, combinations, and permutations with C++?
Well, here is the file:





http://pwnedbyjohn.com/files/files/11814...





The spacing is a bit messed up, I don't know why. It looks fine in Visual Studio. But you can fix it yourself, no big deal.





Now for the errors:





cout: undeclared identifier - You need to put the following at the top of your file, below the #include statements (which is where it is usually put)





using namespace std;





If you do not do this, you will need to use cout, cin, endl, etc.. by typing the following before the cout, etc..





std::





so they woul look like this:





std::cout


std::cin





etc.. (too much typing, so we usually put the using statement above.)








------------------- Next error-------------





if (input == 'Factorial')


else if (input == 'Permutation')


else if (input == 'Combination')





You have the same error on the 3 statements above. I believe you use single quotes when you are putting one character only. If you want to put more than one, you need to use double quotes, because it is a string of characters, not only one. They would look like this:





if (input == "Factorial")


else if (input == "Permutation")


else if (input == "Combination")








------------------- Next error-------------





You missed one %26gt; in both of these: cin %26gt; d; and cin %26gt; e;





should be: cin %26gt;%26gt; d; and cin %26gt;%26gt; e;





------------------- Next error-------------





You missed the closing brace on a couple of for loops.








And I think that gets them all.





If I may make a couple of suggestions:





I don't know if you coded it like that, or when you uploaded the file, it got messed up, but your code has no Indentation. It would've been easier to read if it did.





I noticed that you used two styles of opening and closing the braces. For example:





int myFunction() {


}





and





int myFunction()


{


}





You should pick one that you like and be consistent in its use throughout your code.





BTW: just so you know, (to save you some typing) if you are using only one statement inside the for loop, you don't need the braces. For example:





(assuming i and x were already declared )





for (i = 0 ; i %26lt; 5; ++i)


{


x += i;


}





the braces can be omitted, so you can have it like this:





for (i = 0 ; i %26lt; 5; ++i)


x += i;








Same thing, do it any way you like. But remember, it works if there is only one statement executing inside the for loop. If you have more than one, you need the braces.





Happy coding!





*If I missed or messed something up, please someone correct me. Thanks
Reply:Just checking, but are you trying to make a new Version of John, the password-cracker?

stamen

No comments:

Post a Comment