Hello kyoo,
As I looked over your program and have had a chance to run it this is what I see.
The header files are good, but you should also include "cctype" for the "std::toupper()".
using namespace std; // <--- Best not to use.
If you are interested do a search here. There are many posts that cover this.
I have found this to be the best way to use "srand()"
srand(static_cast<size_t>(time(nullptr)));
Something you could do is define
std::string weapon[]{ "", "Paper", "Rock", "Scissors" };
then in line 71 the "cout" would be
cout << "2) Computer randomly played " << weapon[computerNumber] <<
. This would replace all the if/else if statements to change a number to a word.
Inside the do/while loop you are using to many {}s to define blocks. These are not needed and could work against you since the block defines a scope thet ends with the closing }.
In addition to what
ne555 has said the else state where the lines
1 2
|
cin.clear(); //the input failed, the stream was in an error state
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //discard the erroneous input
|
should go should be done after line 28. And for an input of a number it would work better as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
cout << "Paper-Rock-Scissors GAME by Quiyet\n"
<< "----------------------------------------------\n\n"
<< "1) Player choose (1-paper, 2-rock, or 3-scissors)? "; // prompts player to choose a weapon
cin >> userWeapon; //player chooses and shoots a weapon
while (!std::cin || (userWeapon < 1 || userWeapon > 3))
{
std::cout << "\n ERROR-2A: Invalid input. Must be a character"
"from the list of 'P', 'R', or 'S'.\n\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
cout << "1) Player choose (1-paper, 2-rock, or 3-scissors)? "; // prompts player to choose a weapon
cin >> userWeapon; //player chooses and shoots a weapon
}
|
The error message would have to be adjusted to reflect that numbers are needed or that a character is not allowed. It is better to catch it here than in the else statement.
The last do/while look is OK. Since you use "std::toupper;" to change the case of the input then in the while condition of the first do/while you only need to check for a capital letter. There should not be a lower case letter.
The first time I ran the program I found that when I entered "2" and the computer chose "3" which took me to line 133 which has the wrong output for the graphics and the wrong message. Also it added to the "loss" when it should have added to the "win".
I have an idea for entering a character for user input, but I want to test it before I say anything.
Hope that helps,
Andy
Edit: typo