Need help converting this to h files

Hey guys, I don't understand how to convert this rock paper scissors game to multiple files such as header file, cpp file, and main cpp file.

I read all the materials and lecture slides about it, and I still don't understand what to put in the header file with classes


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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;

int main() {
	enum choice {ROCK, PAPER, SCISSORS};
	
	choice select;
	char user;
	bool play_again;
	string play;
	int num_games = 0;
	int player_wins = 0;
	int computer_wins = 0;
	bool tie = false;
	int num_ties = 0;
	
	ofstream outfile;
	ifstream infile;
	
	
		
		
	// Ask the user if they would like to play.
	cout << "By playing this game, do you want to save your data? (y or n): ";
	cin >> play;
	
	// If they answer yes, then play, otherwise no.
	if(play == "y" || play == "Y"){
		play_again = true;
		num_games++;
		infile.open("RockPaperScissors.txt");
		infile >> player_wins >> computer_wins;
		infile.close();
		}
	
	// Start playing the actual game if they want to play.
	while(play_again != false){
		cout << endl << endl << "Rock, Paper, or Scissors? ('r', 'p', or 's'): " ;
		cin >> user;
		cout << endl;
		
		
		// Taking the input and applying it to constant in enum.
		if (user == 'r' || user == 'R'){
			select = ROCK;
			}
		
		else if (user == 'p' || user == 'P'){
			select = PAPER; 
			}
			
		else if (user == 's' || user == 'S'){
			select = SCISSORS;
			}
		
		else {
			cout << "Come on... That's not an option." << endl;
			cout << endl << endl << "Rock, Paper, or Scissors? ('r', 'p', or 's'): " ;
			cin >> user;
			}
		
		
		long  seed = time(NULL); // gets current time
		srand(seed);
		
		// Computer's random selection
		int computer = rand();
		
		//DEBUG: cout << computer << endl;
		
		// Computer's random selection
		computer = rand() % 3;
		
		//DEBUG: cout << computer << endl << endl;;
		//DEBUG: cout << "Here is a test: " << select << endl;
		
		// In the event of a tie, make both players make a new selection and re-evaluate them.
		
		while(computer == select){
			cout << endl << "It's a tie, Pick again please: ";
			cin >> user;
			
			// This is repeated... :/
			if (user == 'r' || user == 'R'){
				select = ROCK;
				}
		
			else if (user == 'p' || user == 'P'){
				select = PAPER; 
				}
			
			else if (user == 's' || user == 'S'){
				select = SCISSORS;
				}
		
			else {
				cout << "Come on... That's not an option." << endl;
				cout << "Rock, Paper, or Scissors? ('r', 'p', or 's'): " ;
				cin >> user;
				}
			
			computer = rand() % 3;
			}
		
		
		// Applying the computer's selection to the appropriate value in enum.
		choice cpu_select;
		
		if (computer == 0){
			cpu_select = ROCK;
			}
		
		else if (computer == 1){
			cpu_select = PAPER; 
			}
			
		else if (computer == 2){
			cpu_select = SCISSORS;
			}
		
		
			
		// Displays each respective selection.
		cout << endl << endl << "Your selection is " << select << endl;
		cout << "The computer selects " << cpu_select << endl;
				
		// Win conditions

		if ((select == ROCK && cpu_select == SCISSORS) || (select == PAPER && cpu_select == ROCK) || (select == SCISSORS && cpu_select == PAPER)){
				cout << endl << "You win." << endl;
				player_wins ++;
			}

		else if((cpu_select == ROCK && select == SCISSORS) || (cpu_select == PAPER && select == ROCK) || (cpu_select == SCISSORS && select == PAPER)){
			cout << endl << "The computer wins." << endl;
			computer_wins ++;
			}
		
		// Show the results of all the games.
		cout << "The player has won " << player_wins << " games." << endl;
		cout << "The computer has won " << computer_wins << " games." << endl;
		
		
		// Ask if the player would like to play again.
		cout << endl << endl << "Would you like to play again?: ";
		cin >> play;
		
		// Exit the loop if the player chooses no.
		if (play == "n" || play == "N"){
			play_again = false;
			}
		else if (play == "y" || play == "Y"){
			num_games++;
			}
		
	outfile.open("RockPaperScissors.txt");
	//outfile <<  num_games << "\n";
	outfile << player_wins << "\n";
	outfile << computer_wins << "\n";
	outfile << num_ties << "\n";
	outfile.close();
	}
	
	
	
	system("pause");
	return 0;
	
	}
Since you have no classes in your program and all your logic is in main (ever hear of functions?), there is not really anything to move to .h and .cpp files.

Line 67: srand() should only ever be called once in a program. Calling it multiple times (inside a loop) causes it to reset the random number generator.
Like what AbstractionAnon suggested.. You should break it down into Functions! It would make your code so much easier to read!

Here is a set of some Good Tutorials I've used in the past:

Functions: https://youtu.be/aOmfWxbm4mQ?list=PLmpc3xvYSk4wDCP5zjt2QQXe8-JGHa4Kt

From Lessons 25-34 Talks about Functions to Classes..

This is what I've been working on Learning and Practicing with <fstream>
I have almost as much code and mine is so much easier to read.. and yes I could have a MapHandler class w/ member functions just haven't gotten there yet.. still testing and playing with the code.
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 <fstream>
#include <string>
#include <vector>
#include <stdlib.h> //for exit() function

using namespace std;

//FUNCTION DEF
void WriteToFile();
void ReadFromFile();
void LoadMap(vector<string> &mapdata, string fileName);
void PrintMap(const vector<string> &mapdata);

//MAIN
int main()
{
	string fileName = "Map_Outpost.txt";
	vector<string> Map;


	//WriteToFile(fileName);
	//ReadFromFile(fileName);
	LoadMap(Map, fileName);
	PrintMap(Map);


	system("PAUSE");//TESTING ONLY
	return 0;
}

//FUNCTIONS
void WriteToFile(string fileName)
{
	ofstream file;

	file.open(fileName);
	if (file.fail())//ERROR CHECK
	{
		cout << "===========================" << endl;
		cout << "WRITE TO FILE ERROR: Could not open file!\n" << endl;
		perror(fileName.c_str());
		cout << "===========================" << endl;
		system("PAUSE");
		exit(1);
	}

	file << "Map:test" << endl;
	file << "XXX" << endl;
	file << "XXX" << endl;
	file << "XXX" << endl;

	file.close();

	cout << "Writing to file was a Succses!" << endl;
}

void ReadFromFile(string fileName)//
{
	ifstream file;

	file.open(fileName);
	if (file.fail())//ERROR CHECK
	{
		cout << "===========================" << endl;
		cout << "READ FROM FILE ERROR: Could not open file!\n" << endl;
		perror(fileName.c_str());
		cout << "===========================" << endl;
		system("PAUSE");
		exit(1);
	}

	string lineContent;
	while (getline(file, lineContent))
	{
		cout << lineContent << endl;
	}
	cout << endl;

	file.close();
	cout << "Reading from file was a Succses!" << endl;
}
 
void LoadMap(vector<string> &mapData, string fileName)
{
	ifstream file;

	file.open(fileName);
	if (file.fail())//ERROR CHECK
	{
		cout << "===========================" << endl;
		cout << "LOAD MAP ERROR: Could not open file!\n" << endl;
		perror(fileName.c_str());
		cout << "===========================" << endl;
		system("PAUSE");
		exit(1);
	}

	string lineContent;

	while (getline(file, lineContent))
	{
		mapData.push_back(lineContent);
	}

	file.close();
	cout << "Loading Map was a Succses!\n" << endl;
}

void PrintMap(const vector<string> &mapData)
{
	for (auto i = mapData.begin(); i != mapData.end(); ++i) 
	{
		cout << *i << endl; 
	}
	cout << "Drawing Map was a Succses!\n" << endl;
}

Topic archived. No new replies allowed.