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>
Put the code you need help with here.// This program generates a random number between 1 and 3.
#include <iostream>
#include <cstdlib> // rand and srand
#include <ctime> // For the time function
usingnamespace 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;
}
#include <iostream>
#include <cstdlib> // rand and srand
#include <ctime> // For the time function
usingnamespace 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<unsignedint>(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:
#include <iostream>
#include <cstdlib> // rand and srand
#include <ctime> // For the time function
usingnamespace std; // <--- Best not to use.
int main()
{
// Seed the random number generator.
srand(static_cast<unsignedint>(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.
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