Rock, Paper, Scissors Final Project Due Tomorrow - Please Help!

My Final Project is suppose to include User interaction(done),a loop(done,a user defined class, write to an external file, operating overloading & at least one
Array. I just need to pass this class and I honestly never want to see a piece of code again in my life. Any help would be greatly appreciated!
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
    #include <iostream>
	#include <cstdlib>
	#include <ctime>
	#include <string>
	#include <fstream>
	using namespace std;
 

/*Game of Rock - Paper - Scissors*/

// Assigning variables
// Keeping track of the scores

int comp_score = 0, player_score = 0; 

int comp_choice();
void winner(int comp_choice, int player_choice);

/*int main()
{
//Open file for recording game score
	ifstream in_stream;
	ofstream out_stream;

	in_stream.open("RPS Game Score");
	out_stream.open("RPS Game Score");
	int first, second;
	in_stream >> player_score >> comp_score;
	out_stream << "The player's score is   /n"
			   << "The computer's score is   /n"
			   << endl;
	in_stream.close();
	out_stream.close();
	return 0;

	{*/

int main (void)

	{
int choice;
char again = 'y';
srand(time(0));

// Intoduction to the game/do-while loop/game will continue until there is a winner.
//The the user may choose 'y' or 'Y' to continue
//or press 'N' or 'n' to quit.

do

{

cout << "Welcome to Rock, Paper, Scissors! Let's Play!\n\n";

//Output will display your score as well as the computer's.

cout << "Player - " << player_score << " | Computer - " << comp_score << "\n\n";
cout << "Type 1 for rock, 2 for paper, or 3 for scissors.\n\n";
cin >> choice;

int sec_choice = comp_choice();
winner(sec_choice, choice);

cout << "Would you like to play Again? Y/N ";
cin >> again;

system("CLS");

}

while (again == 'y' || again == 'Y');

return 0;
}

// Random number generator for the computer's choice

int comp_choice()
{
return (rand() % 3) + 1;
}

void winner(int comp_choice, int player_choice)
{

// If statement when user and computer choices are the same, output that the game was as tie

if (player_choice == comp_choice)
{
cout << "You both have chosen the same thing, you tied, try again!" << endl;
main();
}

// If-Else statements outputting computer's choice and whether the user has won or lost

if (comp_choice == 3)
cout << "The Computer has chosen scissors." << endl;

else if(comp_choice == 2)
cout << "The Computer has chosen paper." << endl;

else
cout << "The Computer has chosen rock." << endl;

if ((player_choice == 1 && comp_choice == 3) || (player_choice == 2 && comp_choice == 1) || (player_choice == 3 && comp_choice == 2))
{
cout << "Congratulations you win!" << endl;

//This increments the player's score.

player_score+=1;

}
else
{
	//This increments the computer's score.
cout << "Too bad you lose!" << endl;
comp_score+=1; 
}

return;

}

And what exactly is the problem with this code?
As it is it works fine right now except I can't output the scores to a file & I need to incorporate a
class and an array. Is this possible for me to complete today?
What do you mean you can't output the scores to a file? You don't know how? If so, http://lmgtfy.com/?q=c%2B%2B+file+i%2Fo.

Although a class is unnecessary, you could create one to manage the computer's choices, I guess. An array could be used to keep track of both scores, but again, is unnecessary.
Last edited on
I thought I new how to output to a file I just can't get it to work.
I have to have a class & an array to get credit.
I just need to pass this class to continue with my engineering certificate. I have
no desire to do programming after this assignment. I wouldn't have made it this
far in class without my tutor and unfortunately he is unavailable at this time.
I would appreciate any help you can give. Thanks.
Last edited on
You need to type the file extension when working with file I/O. There could be several files with the same name but a different file extension. Do you know how to program classes and arrays?
Well, I thought I did but I have been sitting at my computer for 2 days straight and I am just not sure how to implement them in this code.
First of all, create a class. Declare an integer/short array to keep the scores. Declare the functions "winner()" and "comp_choice()" as methods in the class. Delete those global variables. By the way, instead of "var += 1", "var++" or "++var" can be used.
I will try that, Thanks so much. After I am done I will re-post my code so you can take a look if you don't mind. Thanks again!
I'm feeling like a complete idiot right now. I'm trying to do this and my code just keeps giving me more and more errors. Is this even remotely close?

1
2
3
4
5
6
7
8
9
10
11
12

class rps

     int scores[];
     
{
     public:
          void winner();
          int comp_choice();  
           
};
Last edited on
"int scores[]" should be in the class ;). You might want to make those functions static (allowing them to be accessed without an object, using the syntax "Foo::bar()" or "Foo::bar"), though.
Last edited on
Here is what I have so far, it is compiling but with a few errors.
I have only used a class & static function in one program. I have never used arrays.
Please tell me what else you think I am missing or doing wrong.
Thanks again.

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
 
        #include <iostream>
	#include <cstdlib>
	#include <ctime>
	#include <string>
	#include <fstream>
	using namespace std>

/*Game of Rock - Paper - Scissors*/

	class rps

	{
		public:
		int scores[];
		void winner();
		int comp_choice90;

	};
// Assigning variables
// Keeping track of the scores

int comp_score = 0, player_score = 0; 

void winner(int comp_choice, int player_choice);


int main (void)

	{

		ofstream out_stream;
		out_stream.open ("RPS.txt");
        out_stream << "Thank You for playing Rock Paper Scissors.\n";
        out_stream.close();

	int choice;
	char again = 'y';
	srand(time(0));

// Intoduction to the game/do-while loop/game will continue until there is a winner.
//The the user may choose 'y' or 'Y' to continue
//or press 'N' or 'n' to quit.

do
{

cout << "Welcome to Rock, Paper, Scissors! Let's Play!\n\n";

//Output will display your score as well as the computer's.

cout << "Player - " << player_score << " | Computer - " << comp_score << "\n\n";
cout << "Type 1 for rock, 2 for paper, or 3 for scissors.\n\n";
cin >> choice;

int sec_choice = comp_choice();
winner(sec_choice, choice);

cout << "Would you like to play Again? Y/N ";
cin >> again;

system("CLS");

}

while (again == 'y' || again == 'Y');

return 0;
}

// Random number generator for the computer's choice

int comp_choice()
{
return (rand() % 3) + 1;
}

void winner(int comp_choice, int player_choice)
{

// If statement when user and computer choices are the same, output that the game was as tie

if (player_choice == comp_choice)
{
cout << "You both have chosen the same thing, you tied, try again!" << endl;
main();
}

// If-Else statements outputting computer's choice and whether the user has won or lost

if (comp_choice == 3)
cout << "The Computer has chosen scissors." << endl;

else if(comp_choice == 2)
cout << "The Computer has chosen paper." << endl;

else
cout << "The Computer has chosen rock." << endl;

if ((player_choice == 1 && comp_choice == 3) || (player_choice == 2 && comp_choice == 1) || (player_choice == 3 && comp_choice == 2))
{
cout << "Congratulations you win!" << endl;

//This increments the player's score.

player_score++;

}
else
{
	//This increments the computer's score.
cout << "Too bad you lose!" << endl;
comp_score++;

return;

}
Last edited on
Well, for one thing, functions usually aren't declared with a suffix of "90". Which compiler do you use? It'd be much easier for you if you'd read the errors you get and read the line the error report points to, because that way you'd be able to solve some errors on your own. Or, at the very least, copy/paste me the reports along with the line they point to.
Last edited on
I am using Visual Basic, here are the errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
1>Build started 12/11/2011 2:07:23 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\Final C++ Project.unsuccessfulbuild".
1>ClCompile:
1>  Final.cpp
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(19): warning C4200: nonstandard extension used : zero-sized array in struct/union
1>          Cannot generate copy-ctor or copy-assignment operator when UDT contains a zero-sized array
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(21): error C2229: class 'rps' has an illegal zero-sized array
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(43): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(60): error C3861: 'comp_choice': identifier not found
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(72): error C2561: 'main' : function must return a value
1>          c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(32) : see declaration of 'main'
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(123): fatal error C1075: end of file found before the left brace '{' at 'c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(83)' was matched
1>
1>Build FAILED.
Sorry, should have been () not 90.
Here is the latest code with the errors.

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
 #include <iostream>
	#include <cstdlib>
	#include <ctime>
	#include <string>
	#include <fstream>
	using namespace std;

/*Game of Rock - Paper - Scissors*/

	class rps

	{
		public:
		int scores[];
		void winner();
		int comp_choice();

	};
// Assigning variables
// Keeping track of the scores

int comp_score = 0, player_score = 0; 

void winner(int comp_choice, int player_choice);


int main (void)

	{

		ofstream out_stream;
		out_stream.open ("RPS.txt");
        out_stream << "Thank You for playing Rock Paper Scissors.\n";
        out_stream.close();

	int choice;
	char again = 'y';
	srand(time(0));

// Intoduction to the game/do-while loop/game will continue until there is a winner.
//The the user may choose 'y' or 'Y' to continue
//or press 'N' or 'n' to quit.

do
{

cout << "Welcome to Rock, Paper, Scissors! Let's Play!\n\n";

//Output will display your score as well as the computer's.

cout << "Player - " << player_score << " | Computer - " << comp_score << "\n\n";
cout << "Type 1 for rock, 2 for paper, or 3 for scissors.\n\n";
cin >> choice;

int sec_choice = comp_choice();
winner(sec_choice, choice);

cout << "Would you like to play Again? Y/N ";
cin >> again;

system("CLS");

}

while (again == 'y' || again == 'Y');

return 0;
}

// Random number generator for the computer's choice

int comp_choice()
{
return (rand() % 3) + 1;
}

void winner(int comp_choice, int player_choice)
{

// If statement when user and computer choices are the same, output that the game was as tie

if (player_choice == comp_choice)
{
cout << "You both have chosen the same thing, you tied, try again!" << endl;
main();
}

// If-Else statements outputting computer's choice and whether the user has won or lost

if (comp_choice == 3)
cout << "The Computer has chosen scissors." << endl;

else if(comp_choice == 2)
cout << "The Computer has chosen paper." << endl;

else
cout << "The Computer has chosen rock." << endl;

if ((player_choice == 1 && comp_choice == 3) || (player_choice == 2 && comp_choice == 1) || (player_choice == 3 && comp_choice == 2))
{


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
1>Build started 12/11/2011 2:39:51 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\Final C++ Project.unsuccessfulbuild".
1>ClCompile:
1>  Final.cpp
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(19): warning C4200: nonstandard extension used : zero-sized array in struct/union
1>          Cannot generate copy-ctor or copy-assignment operator when UDT contains a zero-sized array
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(43): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(60): error C3861: 'comp_choice': identifier not found
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(123): fatal error C1075: end of file found before the left brace '{' at 'c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(83)' was matched
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.16
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
According to the error reports, you need to give the array a size ahead of time, type "return 0" instead of "return" at the end of main() and add another '}' at the end.

Oh, I just noticed, you called main() as a function. Don't do that, that causes stack overflow.

EDIT: You wrote "using namespace std>", but for some reason you don't have any errors from that. It should be "using namespace std;".
Last edited on
All of my code didn't show up last time sorry.
I gave the array a size of 10.
When I put a 0 at the end of my last return statement I get an error.
Do I get rid of main() altogether?

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    #include <iostream>
	#include <cstdlib>
	#include <ctime>
	#include <string>
	#include <fstream>
	using namespace std;

/*Game of Rock - Paper - Scissors*/

	class rps

	{
		public:
		int scores[10];
		void winner();
		int comp_choice();

	};
// Assigning variables
// Keeping track of the scores

int comp_score = 0, player_score = 0; 

void winner(int comp_choice, int player_choice);


int main (void)

	{

		ofstream out_stream;
		out_stream.open ("RPS.txt");
        out_stream << "Thank You for playing Rock Paper Scissors.\n";
        out_stream.close();

	int choice;
	char again = 'y';
	srand(time(0));

// Intoduction to the game/do-while loop/game will continue until there is a winner.
//The the user may choose 'y' or 'Y' to continue
//or press 'N' or 'n' to quit.

do
{

cout << "Welcome to Rock, Paper, Scissors! Let's Play!\n\n";

//Output will display your score as well as the computer's.

cout << "Player - " << player_score << " | Computer - " << comp_score << "\n\n";
cout << "Type 1 for rock, 2 for paper, or 3 for scissors.\n\n";
cin >> choice;

int sec_choice = comp_choice();
winner(sec_choice, choice);

cout << "Would you like to play Again? Y/N ";
cin >> again;

system("CLS");

}

while (again == 'y' || again == 'Y');

return 0;
}

// Random number generator for the computer's choice

int comp_choice()
{
return (rand() % 3) + 1;
}

void winner(int comp_choice, int player_choice)
{

// If statement when user and computer choices are the same, output that the game was as tie

if (player_choice == comp_choice)
{
cout << "You both have chosen the same thing, you tied, try again!" << endl;
main();
}

// If-Else statements outputting computer's choice and whether the user has won or lost

if (comp_choice == 3)
cout << "The Computer has chosen scissors." << endl;

else if(comp_choice == 2)
cout << "The Computer has chosen paper." << endl;

else
cout << "The Computer has chosen rock." << endl;

if ((player_choice == 1 && comp_choice == 3) || (player_choice == 2 && comp_choice == 1) || (player_choice == 3 && comp_choice == 2))
{
cout << "Congratulations you win!" << endl;

//This increments the player's score.

player_score++;

}
else
{
	//This increments the computer's score.
cout << "Too bad you lose!" << endl;
comp_score++;

return 0;
}

}
	

And here are the latest errors. The list is getting smaller (-:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
1
1>Build started 12/11/2011 3:01:57 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\Final C++ Project.unsuccessfulbuild".
1>ClCompile:
1>  Final.cpp
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(43): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(60): error C3861: 'comp_choice': identifier not found
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(119): error C2562: 'winner' : 'void' function returning a value
1>          c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(29) : see declaration of 'winner'
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.86
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I do appreciate all of the help you gave me. I think I finally have it figured out.
Thanks again!
Topic archived. No new replies allowed.