PLEASE help with an assignment

I have to make a rock, paper, scissors program. It is basically done, but it doesn't tell the user which selection the cpu picked. How do I return the selection? I honestly don't know what I am doing.

#include <iostream>
#include <ctime>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
int computerSelection = 0;
int userSelection = 0;
computerSelection = 1+rand()%3 +1 ;
string computerDescription = " ";
char playYN = 'Z';
userSelection = 0;

void getComputerDescription(int &computerSelection);
cout << "Do you want to play rock, paper, scissors? (Y/N) ";
cin >> playYN;

while (playYN == 'Y' || playYN == 'y')
{
computerSelection = 1+rand()%3;
getComputerDescription(computerSelection);
cout << "Please enter the number of your selection; 1 for rock, 2 for paper, 3 for scissors" << endl;
cin >> userSelection;
cout << "The computer selected " << computerDescription << endl;

if ( computerSelection == 1 && userSelection == 3 || computerSelection == 3 && userSelection == 1)
cout << "Rock wins because rock crushes scissors!" << endl;
else if ( computerSelection == 2 && userSelection == 3 || computerSelection == 3 && userSelection == 2)
cout << "Scissors win because scissors cut paper!" << endl;
else if ( computerSelection == 1 && userSelection == 2 || computerSelection == 2 && userSelection == 1)
cout << "Paper wins because paper covers rock!" << endl;
else
cout << "We have a tie! Play again!" << endl;


cout << "Play again (YN)? " << endl;
cin >> playYN;
}
system("pause");
return 0;
}
void getComputerDescription(int &computerSelection)
{
string description = " ";
switch (computerSelection)
{
case 1:
description = "rock";
break;
case 2:
description = "paper ";
break;
case 3:
description = "scissors ";
break;
}
}
use string for the getCompDesc function and return desc
You coding style isn't correct but after that i made it at least to run so you can understand where you were wrong.

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <ctime>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
int computerSelection = 0;
int userSelection = 0;
computerSelection = 1+rand()%3 +1 ;
char element[10];// This has Been Changed
char playYN = 'Z';
userSelection = 0;

void getComputerDescription(int &computerSelection, char element[]);// This has Been Changed
cout << "Do you want to play rock, paper, scissors? (Y/N) ";
cin >> playYN;

while (playYN == 'Y' || playYN == 'y')
{
computerSelection = 1+rand()%3;
getComputerDescription(computerSelection, element);// This has Been Changed
cout << "Please enter the number of your selection; 1 for rock, 2 for paper, 3 for scissors" << endl;
cin >> userSelection;
cout << "The computer selected " << element << endl;

if ( computerSelection == 1 && userSelection == 3 || computerSelection == 3 && userSelection == 1)
cout << "Rock wins because rock crushes scissors!" << endl;
else if ( computerSelection == 2 && userSelection == 3 || computerSelection == 3 && userSelection == 2)
cout << "Scissors win because scissors cut paper!" << endl;
else if ( computerSelection == 1 && userSelection == 2 || computerSelection == 2 && userSelection == 1)
cout << "Paper wins because paper covers rock!" << endl;
else
cout << "We have a tie! Play again!" << endl;


cout << "Play again (YN)? " << endl;
cin >> playYN;
}
system("pause");
return 0;
}
void getComputerDescription(int &computerSelection, char element[])// This has Been Changed
{
// This has Been Changed
switch (computerSelection)
{
case 1:
{
element[0] = 'r';
element[1] = 'o';
element[2] = 'c';
element[3] = 'k';
break;
}
case 2:
{
element[0] = 'p';
element[1] = 'a';
element[2] = 'p';
element[3] = 'e';
element[4] = 'r';
break;
}
case 3:
{
element[0] = 's';
element[1] = 'c';
element[2] = 'i';
element[3] = 's';
element[4] = 's';
element[5] = 'o';
element[6] = 'r';
element[7] = 's';
break;
}
}
}


It isn't perfect coding still you can say [The Worst One] :/
Last edited on
Topic archived. No new replies allowed.