RPS code

I'm not entirely sure how I would go about using the results i get in my winner function in other functions i.e. outputting into a file, displaying results in program.

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

// prototypes
int ComputerPick();
void FileOutput(int, int, int);
char Winner(int, int);
void ShowMenu();

int main()
{
	// declare variables
	int choice, comp_pick;
	char play;

	// declare constants
	const int	ROCK = 1,
				PAPER = 2,
				SCISSORS = 3;
	comp_pick = ComputerPick();

	cout << "Rock, Paper, Scissors\n";
	
	do	
	{
		// display menu
		
		cout << endl;
		ShowMenu();
		cin >> choice;
		cout << endl;

		// validate menu choice
		while (choice < ROCK || choice > SCISSORS)
		{ 
			cout << "Please enter a valid choice: ";
			cin >> choice;
		}

		// choose winner
		Winner(choice, comp_pick);

		cout << "Do you want to play again? (Y/N) ";
		cin >> play;

		while (! (play == 'Y' || play == 'N'))
		{
			cout << "Please enter a valid input: ";
			cin >> play;
			break;
		}

	} while (play == 'Y');

	return 0;
}

// menu function
void ShowMenu()
{
	cout << "1 - Rock\n";
	cout << "2 - Paper\n";
	cout << "3 - Scissor\n";
	cout << "Please select your choice: ";
}

// computer pick function, random number
int ComputerPick()
{
	int RAND_NUM;

	RAND_NUM = 1 + rand() % 3;
	return RAND_NUM;
}

// file output function
void FileOutput()
{
	ofstream OUTPUT_FILE;
	int WIN = 0, LOSS = 0, DRAW = 0;

	// open file
	OUTPUT_FILE.open("RPS_Results.txt");

	// output results
	OUTPUT_FILE << "Wins: " << WIN << endl;
	OUTPUT_FILE << "Losses: " << LOSS << endl;
	OUTPUT_FILE << "Draws: " << DRAW << endl;

	// display into program
	cout << endl << "The records for the game were saved.\n";

	// close file
	OUTPUT_FILE.close();
}

char Winner(int COM_PICK, int USER_PICK)
{
	char result = 'a';

	// user picks rock
	if (USER_PICK == 1)
	{
		cout << "You chose Rock.\n";
		if (COM_PICK == 1)
			cout << "Computer chose Rock.\n" << "It's a tie!\n";
		else if (COM_PICK == 2)
			cout << "Computer chose Paper.\n" << "You lost!\n";
		else if (COM_PICK == 3)
			cout << "Computer chose Scissors.\n" << "You won!\n";
	}

	// user picks paper 
	if (USER_PICK == 2)
	{
		cout << "You chose Paper.\n";
		if (COM_PICK == 1)
			cout << "Computer chose Rock.\n" << "You won!\n";
		else if (COM_PICK == 2)
			cout << "Computer chose Paper.\n" << "It's a tie!\n";
		else if (COM_PICK == 3)
			cout << "Computer chose Scissors.\n" << "You lost!\n";
	}

	// user picks scissors
	if (USER_PICK == 3)
	{
		cout << "You chose Scissors.\n";
		if (COM_PICK == 1)
			cout << "Computer chose Rock.\n" << "You lost!\n";
		else if (COM_PICK == 2)
			cout << "Computer chose Paper.\n" << "You won!\n";
		else if (COM_PICK == 3)
			cout << "Computer chose Scissors.\n" << "It's a tie!\n";
	}

	FileOutput();

	return result;
}
You could try passing them in as arguments to the other functions.
Could you elaborate as to how I would go about doing that? Say the user won, how would translate that into a value that I could use in other functions?
You've already defined a variable result in your Winner function, and the function is returning the value of that variable to the calling code. You just need to actually set the value of that variable to something useful.
Gotcha, thanks.

I have just two more questions. When in event of a tie, how would I force the loop to start over, forcing the user to play another round. To my knowledge, I can't just recall the main function to do so.

As for the winner function, I've set the values accordingly in events of a win, loss, or tie. How would I pass those values so that I could use them in the main/output function? As in, if the winner function returns a user win, how could I use that to increment a value that would be displayed in the main function. Would I use an if statement? If so, how would the statement go?
Topic archived. No new replies allowed.