hey, this is my first time posting so please don't be too harsh ...
I am trying to write a program that allows the user to play rock paper scissors against the computer. I am able to create the whole game ... but now i need to put different sections of my code into different functions ... Each color needs to be in it's own separate function ... here's what i have so far ...

#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int main ()

{
	int num, compPick;
	char resp;

	do{
	

	srand (time(0));
	compPick = rand () % 3 + 1;


	cout << "What do you want to choose?";
	cout << "\n\n	1. Rock";
	cout << "\n	2. Paper";
	cout << "\n	3. Scissors";
	cout << "\n	4. Quit";
	cout << "\n\n	Enter you choice (1 - 4): ";
	cin >> num;

switch (num)

{
case 1:
	{
		if (compPick == 2)
		{
			cout <<"\nThe computer picked paper, and you picked rock, you lose!\n\n";
		}
		if (compPick == 3)
		{
			cout <<"\nThe computer picked scissors, and you picked rock, you win!\n\n";
		}
		if (compPick == 1)
		{
			cout <<"\nThe computer picked rock, and you picked rock, it's a draw!\n\n";
		}
	}

	break;

case 2:
	{
		if (compPick == 1)
		{
			cout <<"\nThe computer picked rock, and you picked paper, you win!\n";
		}
		if (compPick == 2)
		{
			cout <<"\nThe computer picked paper, and you picked paper, it's a draw!\n";
		}
		if (compPick == 3)
		{
			cout <<"\nThe computer picked scissors, and you picked paper, you lose!\n";
		}
	}

	break;


case 3:
	{
		if (compPick == 1)
		{
			cout <<"\nThe computer picked rock, and you picked scissors, you lose!\n";
		}
		if (compPick == 2)
		{
			cout <<"\nThe computer picked paper, and you picked scissors, you win!\n";
		}
		if (compPick == 3)
		{
			cout <<"\nThe computer picked scissors, and you picked scissors, it's a draw!\n";
		}
	}

	break;

case 4: 
	{
		cout << endl << endl;
		return 2;
	}

		break;

default: 
	cout << endl << endl;
	cout << num << " is invalid\n\n";

}
	cout <<"\n\nDo you want to go again? (y/n) ";
	cin >> resp;
	cout << endl;
	}while ((resp == 'y') || (resp == 'Y'));

	return 0;

}

... can anyone help me to put the colored sections into their own separate functions? Thanks!!

There are a few things to think about when designing functions. One, what does the function do? Two, what parameters, if any, does the function need to take? Three, what, if anything, does the function need to return? Four, how do you call the function?

cout << "What do you want to choose?";
	cout << "\n\n	1. Rock";
	cout << "\n	2. Paper";
	cout << "\n	3. Scissors";
	cout << "\n	4. Quit";
	cout << "\n\n	Enter you choice (1 - 4): ";
	cin >> num;

A good name for this function would be Menu or perhaps PlayerMove. It wouldn't need to take any parameters. It would return an integer. It's a cut and paste of your code except for the green lines. Try designing a call for it from main. It should work identically to what you have now. Then try the other function.

int Menu ()
{
        int num;
	cout << "What do you want to choose?";
	cout << "\n\n	1. Rock";
	cout << "\n	2. Paper";
	cout << "\n	3. Scissors";
	cout << "\n	4. Quit";
	cout << "\n\n	Enter you choice (1 - 4): ";
	cin >> num;
        return num;
}

And for the second function, it will require an int argument. Make it something like this:

void Play(int); //declare it

int main()
{
//call it
Play(Menu());
...
}

//define it
void Play(int iChoice)
{
//put the switch here
}
This article has been dead for over six months. Start a new discussion instead.