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>

Your entry: 2
Computer generated: 3



Does it look good?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  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
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,

Your program will not compile as is. The comments should help:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cstdlib>      // rand and srand
#include <ctime>        // 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<unsigned int>(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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cstdlib>       // rand and srand
#include <ctime>        // For the time function

using namespace std;  // <--- Best not to use.

int main()
{
    // Seed the random number generator.
    srand(static_cast<unsigned int>(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:
1
2
3
4
5
6
7
8
#include <string>

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
#include <iomanip>
#include <iterator>
#include <limits>
#include <cstdlib>
#include <ctime>

template<typename T = int>
auto getNum(const std::string& prm)
{
	const auto notsp {[&]() {while (std::isspace(static_cast<unsigned char>(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<std::streamsize>::max(), '\n');
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return n;
}

int main()
{
	srand(static_cast<unsigned int>(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.