### Any comments on my program? I wrote a complete C++ program that will ask the user to enter a number between 1 and 3. The program will then generate a random number between 1 and 3. The numbers 1, 2, and 3 represent: 1=Rock, 2= Paper, 3=Scissors. If the user enters 1 (rock) and the computer generates 2 (paper), the computer wins, as paper covers rock. For 2 (paper) and 3 (scissors) 3 wins, as scissors cut paper. But since rock breaks scissors, 1 (rock) wins when it is 1 (rock) and 3 (scissors). The ouput should end up like
Enter an integer between 1 and 3: 2 <Enter>

Computer generated: 3

Does it look good?
 ``1234567891011121314151617181920212223242526272829303132`` `````` Put the code you need help with here.// This program generates a random number between 1 and 3. #include #include // rand and srand #include // For the time function using namespace std; int main() { // Get the system time. unsigned seed = time(0); int randomNumber; // Declaring Variables int Rock = '1', Paper = '2', Scissors = '3'; // Seed the random number generator. srand(seed); cout << "Enter an integer between 1 and 3: "; cin >> Rock; Paper; Scissors; // randomNumber will be assigned a number in the range [1,3]. randomNumber = 3 + rand() % 1; cout << endl; cout << "Your entry: " << Rock, Paper, Scissors; cout << '\n' << "Computer generated: " << randomNumber << endl; return 0; }``````
Last edited on Hello BlueCOCO1,

 ``1234567891011121314151617181920212223242526272829303132`` ``````#include #include // rand and srand #include // For the time function using namespace std; // <--- Best not to use. int main() { // Get the system time. //unsigned seed = time(0); // Seed the random number generator. //srand(seed); srand(static_cast(time(nullptr))); int randomNumber; // Declaring Variables int Rock{ '1' }, Paper{ '2' }, Scissors{ '3' }; // <--- Variables ate "int"s. You are trying to initialize as "char"s. Remove the qoutes. cout << "Enter an integer between 1 and 3: "; cin >> Rock; Paper; Scissors; // <--- Only the "Rock" gets a value the other 2 have no effect. // randomNumber will be assigned a number in the range [1,3]. randomNumber = 3 + rand() % 1; // <--- What you want is rand() % 3 + 1 cout << endl; cout << "Your entry: " << Rock, Paper, Scissors; // <--- There is a good chance that only "Scissors" will print. cout << '\n' << "Computer generated: " << randomNumber << endl; return 0; // <--- This blank line is not needed. }``````

This does compile and should give you a good start for improving your game:
 ``123456789101112131415161718192021222324252627`` ``````#include #include // rand and srand #include // For the time function using namespace std; // <--- Best not to use. int main() { // Seed the random number generator. srand(static_cast(time(nullptr))); // Declaring Variables int randomNumber{}; // <--- ALWAYS initialize all your variables. int userChoice{}; cout << "Enter an integer between 1 and 3: "; cin >> userChoice; // randomNumber will be assigned a number in the range [1,3]. randomNumber = rand() % 3 + 1; cout << endl; cout << "Your entry: " << userChoice << '\n'; cout << '\n' << "Computer generated: " << randomNumber << endl; return 0; // <--- Not required, but makes a good break point for testing. }``````

This does compile and run in the shell program here, the gear icon on the right side of the code box.

Andy Hello BlueCOCO1,

I made a small change to the program:
 ``12345678`` ``````#include std::string items[]{ "", "Rock", "Paper", "Scissors" }; cout << "Your entry: " << items[userChoice] << '\n'; cout << '\n' << "Computer generated: " << items[computerChoice] << '\n'; ``````

After a couple of runs it produced this:
 ``` Enter an integer between 1 and 3: 2 Your entry: Paper Computer generated: Rock Enter an integer between 1 and 3: 3 Your entry: Scissors Computer generated: Scissors Enter an integer between 1 and 3: 1 Your entry: Rock Computer generated: Scissors ```

See what you think.

Andy As an update to Andy's:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` ``````#include #include #include #include #include #include #include template auto getNum(const std::string& prm) { const auto notsp {[&]() {while (std::isspace(static_cast(std::cin.peek())) && std::cin.peek() != '\n') std::cin.ignore(); return std::cin.peek() != '\n'; }}; T n {}; while ((std::cout << prm) && (!(std::cin >> n) || notsp())) { std::cout << "Not a number\n"; std::cin.clear(); std::cin.ignore(std::numeric_limits::max(), '\n'); } std::cin.ignore(std::numeric_limits::max(), '\n'); return n; } int main() { srand(static_cast(time(nullptr))); const std::string items[] {"", "Rock", "Paper", "Scissors"}; int userChoice {}; for (size_t i = 1; i < std::size(items); ++i) std::cout << i << ") " << std::left << std::setw(10) << items[i]; std::cout << "\n\n"; do { userChoice = getNum("Enter an integer between 1 and 3: "); } while ((userChoice < 1 || userChoice > 3) && (std::cout << "Invalid choice\n")); const auto computerChoice {rand() % 3 + 1}; std::cout << "\nYour entry: " << items[userChoice] << '\n'; std::cout << "Computer generated: " << items[computerChoice] << '\n'; }`````` Okay, I appreciate the comments, I made changes to my program and It's compiling right. Thank you so much!
Topic archived. No new replies allowed.