Rock Paper Scissors

Hey guys. I am new in programming and I have an assignment for the university. I have written the code. However every time I run it I get the same output ( which is "tie" and then the program crushes. Can you take a look and tell me where I am 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//7540
#include <iostream>
#include <string>
#include <cstdlib>
#include <locale>
using namespace std;

struct Player {
	char name[41];
	int points;
	bool master;
	char weapon[9];
}players[2], *pointer1, *pointer2;

void checkMastery(Player *pointer);
void letsFight(Player *pointer1,Player *pointer2);
string showBestPlayer(Player* pointer1, Player* pointer2);
int main(){
	pointer1 = players;
	pointer2 = &players[1];
	cout<<"Enter the player's name:\n";
	cin>>players[0].name;
	fflush(stdin);
	cout<<"Enter the player's weapon:\n";
	cin>>players[0].weapon;
	fflush(stdin);
	cout<<"Enter the player's name:\n";
	cin>>players[1].name;
	cout<<"Enter the player's weapon:\n";
	cin>>players[1].weapon;
	fflush(stdin);
	checkMastery(pointer1);
	checkMastery(pointer2);
	letsFight(pointer1, pointer2);
	showBestPlayer(pointer1, pointer2);
}
void checkMastery(Player* pointer){
	pointer->master=((pointer->points<100) ? false : true); 
}
void letsFight(Player* pointer1, Player* pointer2){
	if (((*pointer2).weapon !="rock")&&((*pointer2).weapon != "paper")&&((*pointer2).weapon!= "scissors")) {
			(*pointer2).points = 0 ;
			if (((*pointer1).weapon !="rock")&&((*pointer1).weapon != "paper")&&((*pointer1).weapon!= "scissors")){
				(*pointer1).points = 0;
		}
	}
	if ((*pointer1).weapon == "rock"){
		if ((*pointer2).weapon == "rock"){
			(*pointer1).points+=1;
			(*pointer2).points+=1;
		}else if((*pointer2).weapon == "paper"){
			(*pointer1).points-=1;
			(*pointer2).points+=1;
		}else if((*pointer2).weapon == "scissors"){
			(*pointer1).points+=1;
			(*pointer2).points-=1;
	}else if ((*pointer1).weapon == "paper"){
		if ((*pointer2).weapon == "rock"){
			(*pointer1).points+=1;
			(*pointer2).points-=1;
		}else if ((*pointer2).weapon == "paper"){
			(*pointer1).points+=1;
			(*pointer2).points-=1;
		}else if ((*pointer2).weapon == "scissors"){
			(*pointer1).points-=1;
			(*pointer2).points+=1;
		}
	}else if((*pointer1).weapon == "scissors"){
		if((*pointer2).weapon == "rock"){
			(*pointer1).points+=1;
			(*pointer2).points-=1;
		}else if((*pointer2).weapon == "paper"){
			(*pointer1).points-=1;
			(*pointer2).points+=1;
		}else if ((*pointer2).weapon == "scissors"){
			(*pointer1).points+=1;
			(*pointer2).points+=1;
		}
	}
	}	
}

string showBestPlayer(Player* pointer1, Player* pointer2){
	if ((*pointer1).points > (*pointer2).points) {
		char bestPlayer[41];
		memcpy(bestPlayer,(*pointer1).name,40);
		cout << bestPlayer;
                return bestplayer;
	}
	else if ((*pointer2).points > (*pointer1).points) {
		char bestPlayer[41];
		memcpy(bestPlayer, (*pointer2).name,40);
		cout << bestPlayer;
	        return bestPlayer;
        }
	else if((*pointer1).points == (*pointer2).points) {
		cout << "tie";
	        return "tie";}
}


Last edited on
Does your compiler generate any warnings while compiling your program?

A compiler says:
 In function 'void letsFight(Player*, Player*)':
41:28: warning: comparison with string literal results in unspecified behaviour [-Waddress]
41:60: warning: comparison with string literal results in unspecified behaviour [-Waddress]
41:92: warning: comparison with string literal results in unspecified behaviour [-Waddress]
43:30: warning: comparison with string literal results in unspecified behaviour [-Waddress]
43:62: warning: comparison with string literal results in unspecified behaviour [-Waddress]
43:94: warning: comparison with string literal results in unspecified behaviour [-Waddress]
47:28: warning: comparison with string literal results in unspecified behaviour [-Waddress]
48:29: warning: comparison with string literal results in unspecified behaviour [-Waddress]
51:34: warning: comparison with string literal results in unspecified behaviour [-Waddress]
54:34: warning: comparison with string literal results in unspecified behaviour [-Waddress]
57:34: warning: comparison with string literal results in unspecified behaviour [-Waddress]
58:29: warning: comparison with string literal results in unspecified behaviour [-Waddress]
61:35: warning: comparison with string literal results in unspecified behaviour [-Waddress]
64:35: warning: comparison with string literal results in unspecified behaviour [-Waddress]
68:33: warning: comparison with string literal results in unspecified behaviour [-Waddress]
69:28: warning: comparison with string literal results in unspecified behaviour [-Waddress]
72:34: warning: comparison with string literal results in unspecified behaviour [-Waddress]
75:35: warning: comparison with string literal results in unspecified behaviour [-Waddress]
 In function 'std::string showBestPlayer(Player*, Player*)':
86:40: error: 'memcpy' was not declared in this scope
91:41: error: 'memcpy' was not declared in this scope
97:1: warning: no return statement in function returning non-void [-Wreturn-type]


Why do you use char arrays, when you do have string in use too?
I don't get warnings like these.
It is stated in the assignment I have that it should be a char type.
It would be good, if you could make your compiler more verbose.

comparison with string literal results in unspecified behaviour
( (*pointer2).weapon !="rock" )
The weapon is member of Player. It is an array of char.
The "rock" is a literal string constant.

Therefore, the above code is almost same as:
1
2
3
4
char* lhs = (*pointer2).weapon;
const char* rhs = "rock";

( lhs != rhs )

That does not compare words. That compares two addresses.
The compiler states "unspecified behaviour" and a "crush" fulfills that promise.
The addresses are probably different and therefore the inequality is true.
Since both players have 0 points, a tie it is.

If you have to compare two C-strings, then you have to use C functions:
http://www.cplusplus.com/reference/cstring/strncmp/

With C-strings you would use string copy rather than memcpy:
http://www.cplusplus.com/reference/cstring/strncpy/

Why do you memcpy anyway? You can print 'name' directly.


To read into array. See the importance of width in http://www.cplusplus.com/reference/istream/istream/operator-free/


Note that cin and stdio are distinct. There is no reason to flush stdin.

Note that (*pointer2).weapon and pointer2->weapon mean the same.

Get rid of global variables (players, pointer1, pointer2 on line 13). You can declare these local, in main().


You check mastery (lines 32-33) before any games. Points are uninitialized. Unknown values.
If your C-string comparisons were right, then you would +=1 or -=1 unknown values.


Are you forced to use pointers too? C++ has by reference parameters.
Topic archived. No new replies allowed.