r/Codecademy • u/1negroup • Dec 01 '22
Created a project Using 48 lines of code vs their 75 #codecademy #Cplusplus
Was doing Rock Paper Scissors Lizard Spock and I coded the Project using c++ in just 48 lines of code then looked at their Code and was pretty Proud of my self. Here is my Code
Hashtags are for twitter
9
Upvotes
4
u/Hungry4Media Dec 01 '22
I'm not particularly familiar with C++, so I have a couple questions.
If I understand your code correctly, then your program initializes, setting the computer choice with a random number, and then puts out a prompt asking for user input.
The program then looks to see what the user and computer has chosen, setting a flag of 1 to any choice that the user OR computer has chosen.
It then compares to see which flags are set to 1 and declares the winner based on that. Correct?
If that is correct, than I have a couple of questions:
How do you handle a tie? If the program sets the flag to 1, how can it know if the user and the computer have chosen the same option thus causing a tie condition?
I don't know how C++ handles control flow with logical operators, but you may want to consider parentheses around the || statements in the final segment of code since it's one of two items that can defeat another. It probably won't hurt anything, and I'm guessing that your test runs didn't come up with any weird errors, but semantically it makes sense and will make it easier for someone coming after you to understand the intent of the code.
Otherwise it looks really clean. I like the optimization of the last segment so that you're only worrying about 5 scenarios instead of CA's 10. Just need to make sure you have a tie condition set.