C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 14th, 2008, 02:56 PM
Blackout22 Blackout22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 14 Blackout22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 24 sec
Reputation Power: 0
Question General - Need Help with functions please

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!!

Reply With Quote
  #2  
Old November 14th, 2008, 03:42 PM
ukman408 ukman408 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 5 ukman408 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 51 m 46 sec
Reputation Power: 0
I'm no expert but this should help. Basically you'll want to take a colored section and seperate it out of the main part of the program, and then basically reference it with the function name.

For example...

those switch statements you have could be done like so...

*Take the input from the user, and pass it into a named function you create.

The function declaration would look like this...
void decideWinner(int);

Use this inside the main program instead of the switch statements...
//this will keep it going till they "quit"
while (num != 4)
decideWinner(num);

Below the main program add something like this...

void decideWinner(num)
{
"SWICTH STATEMENTS HERE"
}

This will pass that num value they enter into the function and get that code out of the main program. Do similar things for the other parts. You can even reference a function from within another function.

Reply With Quote
  #3  
Old November 14th, 2008, 04:26 PM
Blackout22 Blackout22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 14 Blackout22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 24 sec
Reputation Power: 0
Question

Thankyou for you help ... I'm not really sure how to pass function properly ... but now i have this as my code ... and i'm getting a couple of errors .... 1. 'num' is an undeclared identifier and 2. '<Unknown>' function-style initializer appears to be a function definition ... What am I doing wrong?

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

//function prototypes
decideWinner(int);


int main ()

{
int compPick;
char resp;

do{

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;




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


decideWinner(num);


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

return 0;

}

/////////////////////////////////////////////////////////////////
void decideWinner(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";

}
}

Reply With Quote
  #4  
Old November 15th, 2008, 12:19 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 11
Don't forget you can re-edit you old posts and the icon "#" wraps code tags around any selected text !

//function prototypes - minimum
Code:
void decideWinner(int);

// OR

void decideWinner(int num);

if you leave off the return type (the "void") the compiler assumes a return type of "int". this is ok as long as it matches up with the main part of that function later in the program

the compiler puts up with the actual name of parameters being different in the prototype and body though. It's good policy to write the names of parameters in the prototype though, as it helps when you read your code much later.

....

when declaring a function header with body:
Code:
void decideWinner(int num)
{
    // body of function
}

you must declare the types and names of all parameters (the "int" here).




when calling the function you just write the name of the variable you are passing :
Code:
decideWinner(anyNum)


I'm trying to compile your program, and another issue(which you probably would've come across any minute) is that compPick is created in "main" but used in "decideWinner" without being passed. Either you have two variables passed or you move the creation of "compPick" into "decideWinner"

Reply With Quote
  #5  
Old November 15th, 2008, 12:56 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 11
Here's my take on your program. let me know if you have any questions about what i did here

Code:
#include <iostream>
#include <ctime>
using namespace std;

//function prototypes
void decideWinner(int humanPick);

// original game moves

char *moves[4] = {"", "Rock", "Paper", "Scissors"};

// alternate games - un-comment one put and // before all others

//char *moves[4] = {"", "Infantry", "Artillery", "Cavalry"};
//char *moves[4] = {"", "Pikemen", "Archers", "Knights"};

int main ()
{
	int num;

	cout << "Now playing " << moves[1] << ", " << moves[2] << ", "  << moves[3] << ".\n\n";

	do // loop while num is not 4
	{
		cout<< "What do you want to choose?" 
			<< "\n\n 1. " << moves[1]
			<< "\n 2. " << moves[2]
			<< "\n 3. " << moves[3]
			<< "\n 4. Quit";

		do // ensure input is in range 1 - 4
		{
			cout << "\n\n Enter you choice (1 - 4): ";

			cin >> num;
		}
		while(num < 1 || num > 4);

		if(num >= 1 && num <= 3)
		{
			decideWinner(num);

			char resp;

			do // loop while resp is not y or n
 			{
				cout <<"\n\nDo you want to go again? (y/n) ";
				cin >> resp;
			}
			while( (resp | 32) != 'y' && (resp | 32) != 'n');

                       // resp | 32 gives the lower case value, by the way

			if( (resp | 32) == 'n')
				num = 4;
		}
	}
	while(num != 4);

	return 0;
}

/////////////////////////////////////////////////////////////////
void decideWinner(int humanPick)
{
	srand (time(0));
	int compPick = rand () % 3 + 1;

	cout <<"\nThe computer picked " << moves[compPick] << ", ";
	cout <<"and you picked " << moves[humanPick] << ", ";

	if(compPick == humanPick) 
		cout << "it's a draw!";

	else // a winner and a loser
	{
		int beatsComputer = compPick + 1;

		if(beatsComputer == 4)
			beatsComputer = 1;


		if(humanPick == beatsComputer)
			cout << "you win!";
		else
			cout << "you lose!";
	}
}

Reply With Quote
  #6  
Old November 15th, 2008, 04:42 AM
Blackout22 Blackout22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 14 Blackout22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 24 sec
Reputation Power: 0
Question

Thank you very much for all your help ... I just have a few questions on your code though ... This is what I have now ... I changed some things to a way so i understand them a little bit better ...

PHP Code:
#include <iostream>
#include <ctime>
using namespace std;

//function prototypes
void decideWinner(int humanPick);

// original game moves

char *moves[4] = {"""Rock""Paper""Scissors"};

int main ()
{
    
int num;
    
char resp;

    do 
// loop while num is not 4
    
{
        
cout<< "What do you want to choose?" 
            
<< "\n\n 1. " << moves[1]
            << 
"\n 2. " << moves[2]
            << 
"\n 3. " << moves[3]
            << 
"\n 4. Quit";

            
cout << "\n\n Enter you choice (1 - 4): ";

            
cin >> num;

            
decideWinner(num);


                
cout <<"\n\nDo you want to go again? (y/n) ";
                
cin >> resp;
                
cout <<endl;

    }while ((
resp == 'y') || (resp == 'Y'));

    return 
0;
}

/////////////////////////////////////////////////////////////////
void decideWinner(int humanPick)
{
    
srand (time(0));
    
int compPick rand () % 1;

    
cout <<"\nThe computer picked " << moves[compPick] << ", ";
    
cout <<"and you picked " << moves[humanPick] << ", ";

    if(
compPick == humanPick
        
cout << "it's a draw!\n";

    else 
// a winner and a loser
    
{
        
int beatsComputer compPick 1;

        if(
beatsComputer == 4)
            
beatsComputer 1;


        if(
humanPick == beatsComputer)
            
cout << "you win!\n";
        else
            
cout << "you lose!\n";
    }



... the questions i have are ... 1. What are the variables moves[compPick] and moves[humanPick] ... I'm not sure how those work as I don't see them anywhere else in the program.
2. If you could please explain to me how this function works ...
PHP Code:
 void decideWinner(int humanPick)
{
    
srand (time(0));
    
int compPick rand () % 1;

    
cout <<"\nThe computer picked " << moves[compPick] << ", ";
    
cout <<"and you picked " << moves[humanPick] << ", ";

    if(
compPick == humanPick
        
cout << "it's a draw!\n";

    else 
// a winner and a loser
    
{
        
int beatsComputer compPick 1;

        if(
beatsComputer == 4)
            
beatsComputer 1;


        if(
humanPick == beatsComputer)
            
cout << "you win!\n";
        else
            
cout << "you lose!\n";
    }



... I get lost with this statement ...
PHP Code:
 int beatsComputer compPick 1;

        if(
beatsComputer == 4)
            
beatsComputer 1

... I'm not sure how that works ...

and 3. How exactly does this section of code work?

PHP Code:
 char *moves[4] = {"""Rock""Paper""Scissors"}; 


... I'm not sure what that line is doing?

Thank you very much for taking the time to help me out!

Reply With Quote
  #7  
Old November 15th, 2008, 04:53 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 11
This creates a table of string values and loads it with some data

Code:
 char *moves[4] = {"", "Rock", "Paper", "Scissors"};  


break down :


"char *" is the basic c way of doing strings. this at the start of the line means the type of data to follow

"moves" is a name i made up, can be anything you want

[4] says to allot 4 values of the "type" specified at the start of the line

" {"", "Rock", "Paper", "Scissors"};" is just a list of values to pre-load into the array "moves"

this is what is called a "lookup table" for the names

the set-up allows you to convert an integer value into a set choice of string values

it is used to pick the right name from humanPick and compPick :

the line moves[compPick] returns the string for which ever compPick is set to, same for moves[humanPick]

note how i also used moves[] in the main func to display the choices

Reply With Quote
  #8  
Old November 15th, 2008, 05:00 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 11
Code:
int beatsComputer = compPick + 1;

        if(beatsComputer == 4)
            beatsComputer = 1;  


well, compPick is the move which the computer picked :=

1 = rock
2 = paper
3 = scissors

as you can see, 2 beats 1, 3 beats 2, and 1 beats 3.

i set up "beatsComputer" to calculate which player move literally would beat the computer.

int beatsComputer = compPick + 1; creates a variable which is set to compPick + 1

if compPick is 1, beatsComputer now equals 2 (paper)
if compPick is 2, beatsComputer now equals 3 (scissors)
if compPick is 3, beatsComputer now equals 4 (4 = not allowed)

obviously, we don't have a move '4', so at this point i had it check if beatComputer equals 4, and change it to 1 if it is 4.

at this point, beatsComputer now says which player move is required to beat the computer

we already checked for a draw earlier, so if humanPick == beatsComputer, you win, else, you lose

Reply With Quote
  #9  
Old November 15th, 2008, 05:09 AM
Blackout22 Blackout22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 14 Blackout22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 24 sec
Reputation Power: 0
Question

Would there possibly be a way I could get this program to work without using ...
Code:
char *moves[4] = {"", "Rock", "Paper", "Scissors"};  

... Because I don't entirely understand how that works ...?

Reply With Quote
  #10  
Old November 15th, 2008, 05:11 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 11
it's just an array it's not code

Reply With Quote
  #11  
Old November 15th, 2008, 05:15 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 11
same stuff, split over more lines :-

Code:
 char *moves[4] = 
{
       "",               // "moves[0]"
       "Rock",       // "moves[1]"
       "Paper",      // "moves[2]"
       "Scissors"   // "moves[3]"
};  


usage :-


Code:
 
cout << moves[0]; // prints nothing
cout << moves[1]; // prints "Rock"
cout << moves[2]; // prints "Paper"
cout << moves[3]; // prints "Scissors"

int compPick = 3; // example value for compPick

cout << moves[compPick]; // prints "Scissors"  

compPick = 2;

cout << moves[compPick]; // prints "Paper"  

compPick = 1;

cout << moves[compPick]; // prints "Rock"  

Reply With Quote
  #12  
Old November 15th, 2008, 05:16 AM
Blackout22 Blackout22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 14 Blackout22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 24 sec
Reputation Power: 0
So basically ...
Code:
 char *moves[4] = {"", "Rock", "Paper", "Scissors"};  

... that code is just initializing the char "moves" ...?

Reply With Quote
  #13  
Old November 15th, 2008, 05:18 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 11
multiple char strings, which can be selected between with an index of type "int" for example

Reply With Quote
  #14  
Old November 15th, 2008, 05:19 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 11
it's a list of words basically, and all references to "moves" are using the bit in the [] to pick which word you are after

Reply With Quote
  #15  
Old November 15th, 2008, 05:29 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 11
you can alter the text values to be anything :=

Code:
char *moves[4] = 
{
       "",               // "moves[0]"
       "Mouse",       // "moves[1]"
       "Cat",      // "moves[2]"
       "Elephant"   // "moves[3]"
};  


with this code, your game would be Cat eats Mouse, Elephant stomps Cat, Mouse scares Elephant

Reply With Quote
  #16  
Old November 15th, 2008, 09:36 AM
Blackout22 Blackout22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 14 Blackout22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 24 sec
Reputation Power: 0
Question

Alright I think I've got that a little better ... but now I'm trying to create a second function so that one decides the winner, and the other prints the output ... this is what i have ...

Code:
#include <iostream>
#include <ctime>
using namespace std;

//function prototypes
void decideWinner(int userPick);
void output(int userPick, int comPick);

// original game moves

char *moves[4] = {"", "Rock", "Paper", "Scissors"};

int main ()
{
	int num;
	char resp;

	do // loop while num is not 4
	{
		cout<< "What do you want to choose?" 
			<< "\n\n 1. " << moves[1]
			<< "\n 2. " << moves[2]
			<< "\n 3. " << moves[3]
			<< "\n 4. Quit";

			cout << "\n\n Enter you choice (1 - 4): ";

			cin >> num;

			if ((num >= 4) || (num <= 0))
			{
				cout << endl;
				return -1;
			}

			decideWinner(num);
			output(int userPick, int comPick);


				cout <<"\n\nDo you want to go again? (y/n) ";
				cin >> resp;
				cout <<endl;

	}while ((resp == 'y') || (resp == 'Y'));

	return 0;
}

/////////////////////////////////////////////////////////////////
void decideWinner(int userPick)
{
	srand (time(0));
	int compPick = rand () % 3 + 1;

/*	cout <<"\nThe computer picked " << moves[compPick] << ", ";
	cout <<"and you picked " << moves[userPick] << ", ";

	if(compPick == userPick) 
		cout << "it's a draw!\n";

	else // a winner and a loser
	{
		int beatsComputer = compPick + 1;

		if(beatsComputer == 4)
			beatsComputer = 1;


		if(userPick == beatsComputer)
			cout << "you win!\n";
		else
			cout << "you lose!\n";
	}*/
}

//////////////////////////////////////////////////////////////////////
void output (int userPick, int comPick)
{
		cout <<"\nThe computer picked " << moves[compPick] << ", ";
	cout <<"and you picked " << moves[userPick] << ", ";

	if(compPick == userPick) 
		cout << "it's a draw!\n";

	else // a winner and a loser
	{
		int beatsComputer = compPick + 1;

		if(beatsComputer == 4)
			beatsComputer = 1;


		if(userPick == beatsComputer)
			cout << "you win!\n";
		else
			cout << "you lose!\n";
	}
}


... But i am getting 4 errors ...
1. syntax error missing ')' before type 'int'
2. 'output' : function does not take 0 parameters
3. syntax error ')'
4. compPick undeclared identifier

I thought I had done everything right ... what am i doing wrong? (humanPick was changed to userPick)

Reply With Quote
  #17  
Old November 15th, 2008, 09:40 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 11
calling the function as

output(int userPick, int comPick); (line 37 in your code)

is an error. you leave the types off when using variables mostly

call it like this :-

output(userPick, comPick);

but remember these variables are not local to "main". num is the local variable equal to "userPick" and you will have to get decideWinner to return some values

Reply With Quote
  #18  
Old November 15th, 2008, 09:50 AM
Blackout22 Blackout22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 14 Blackout22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 24 sec
Reputation Power: 0
Alright so i changed my code to this ... when i run it and enter my choice it stops and crashes the program ... Too me it looks like everything is alright .... but I can't figure out what I'm doing wrong ...

Code:
#include <iostream>
#include <ctime>
using namespace std;

//function prototypes
void decideWinner(int userPick);
void output(int userPick, int compPick);

// original game moves

char *moves[4] = {"", "Rock", "Paper", "Scissors"};

int main ()
{
	int num, compPick, userPick;
	char resp;

	do // loop while num is not 4
	{
		cout<< "What do you want to choose?" 
			<< "\n\n 1. " << moves[1]
			<< "\n 2. " << moves[2]
			<< "\n 3. " << moves[3]
			<< "\n 4. Quit";

			cout << "\n\n Enter you choice (1 - 4): ";

			cin >> num;

			if ((num >= 4) || (num <= 0))
			{
				cout << endl;
				return -1;
			}

			decideWinner(num);
			output(userPick, compPick);


				cout <<"\n\nDo you want to go again? (y/n) ";
				cin >> resp;
				cout <<endl;

	}while ((resp == 'y') || (resp == 'Y'));

	return 0;
}

/////////////////////////////////////////////////////////////////
void decideWinner(int userPick)
{
	srand (time(0));
	int compPick = rand () % 3 + 1;

/*	cout <<"\nThe computer picked " << moves[compPick] << ", ";
	cout <<"and you picked " << moves[userPick] << ", ";

	if(compPick == userPick) 
		cout << "it's a draw!\n";

	else // a winner and a loser
	{
		int beatsComputer = compPick + 1;

		if(beatsComputer == 4)
			beatsComputer = 1;


		if(userPick == beatsComputer)
			cout << "you win!\n";
		else
			cout << "you lose!\n";
	}*/
}

//////////////////////////////////////////////////////////////////////
void output (int userPick, int comPick)
{
	int compPick;
		cout <<"\nThe computer picked " << moves[compPick] << ", ";
	cout <<"and you picked " << moves[userPick] << ", ";

	if(compPick == userPick) 
		cout << "it's a draw!\n";

	else // a winner and a loser
	{
		int beatsComputer = compPick + 1;

		if(beatsComputer == 4)
			beatsComputer = 1;


		if(userPick == beatsComputer)
			cout << "you win!\n";
		else
			cout << "you lose!\n";
	}
}

Reply With Quote
  #19  
Old November 15th, 2008, 09:53 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 11
i think your barking up the wrong tree trying to separate this function in two, i've looked at it and all it will do is increase the complexity of your program

but if you want to try, you'll learn a lot about return values and re-writing function headers

just think about the responsibilities of each function and how they are going to communicate

Reply With Quote
  #20  
Old November 15th, 2008, 09:57 AM
Blackout22 Blackout22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 14 Blackout22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 24 sec
Reputation Power: 0
Yea ... I understand that it will make the code more complex ... but i would like to learn how to create this separate function for future reference ...

Reply With Quote
  #21  
Old November 15th, 2008, 10:02 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 11
well think for a start : what inputs will "decidewinner" take

how to get result from "decide winner" to output function

what decidewinner function needs to know - userPick, comPick

what output function needs to know - userPick, comPick, win/lose/draw

now, you have a slight problem. beforehand, comPick was chosen inside decideWinner. now, you want output also to know comPick, so you have to move the choosing of computer's move back to main part of program.

then, make decideWinner have two variables - userPick,comPick
and return :=

0 = Draw
1 = Win
-1 = Lose

output now must take 3 inputs userPick,comPick and the value returned from decideWinner

Reply With Quote
  #22  
Old November 15th, 2008, 10:06 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 11
Code:
int decideWinner(int userPick, int comPick)
{
	if(comPick== userPick) 
		return 0; // draw

	else // a winner and a loser
	{
		int beatsComputer = comPick+ 1;

		if(beatsComputer == 4)
			beatsComputer = 1;


		if(userPick == beatsComputer)
			return  1; // win
		else
			return -1; // loss
	}
}

Reply With Quote
  #23  
Old November 15th, 2008, 10:08 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 11
Code:
void outputResult(int userPick, int compPick, int outcome)
{
	cout <<"\nThe computer picked " << moves[compPick] << ", ";
	cout <<"and you picked " << moves[userPick] << ", ";

	switch(outcome)
	{
		case 0: cout << "it's a draw!\n";	break;
		case 1: cout << "you win!\n";		break;
		case-1: cout << "you lose!\n";		break;
	}
}

Reply With Quote
  #24  
Old November 15th, 2008, 10:09 AM
Blackout22 Blackout22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 14 Blackout22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 24 sec
Reputation Power: 0
Umm ... well I wanted to set the code up into multiple functions .... one function to generate the computer pick ... one to decide the winner ... and one to print all the output ... but I have a feeling that it's going to be impossible for me ....

Reply With Quote
  #25  
Old November 15th, 2008, 10:13 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 11
it's doable, just sometimes splitting stuff up just creates lots of extra work. i can set it up for you if you like at least that way you see how it would fit together

Reply With Quote
  #26  
Old November 15th, 2008, 10:15 AM
Blackout22 Blackout22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 14 Blackout22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 24 sec
Reputation Power: 0
If you could do that please it would be great.

Reply With Quote
  #27  
Old November 15th, 2008, 10:44 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 11
Here's the Ultimate Version

Code:
#include <iostream>
#include <ctime>
#include <conio.h> // needed for 'getch()' and kbhit()
using namespace std;
/////////////////////////////////////////////////////////////////
// Program Data
/////////////////////////////////////////////////////////////////

// Uncomment these two data lines to get "normal" rock paper scissors, but you have to comment out any other verisons to compile

//char *moves[4] = {"", "Rock", "Paper", "Scissors"}; // names of the choices
//char *effects[4] = {"", "Blunts", "Wraps", "Cuts"}; // names of the effects

// Just for fun ... ANIMAL rock paper scissors

char *moves[4] = {"", "Mouse", "Cat", "Elephant"}; // names of the choices
char *effects[4] = {"", "Scares", "Eats", "Stomps"}; // names of the effects

int results[3]; // to count wins, losses and draws

/////////////////////////////////////////////////////////////////
// Function Prototypes
/////////////////////////////////////////////////////////////////
bool getYesNo(char *titleText); // Y/N key getter
int getUserPick();
int getCompPick();
int decideWinner(int userPick, int comPick);
void outputResult(int userPick, int comPick, int outcome);

/////////////////////////////////////////////////////////////////
// MAIN
/////////////////////////////////////////////////////////////////
int main ()
{
	bool ok;

	do
	{
		system("cls");

		cout << "Welcome to " << moves[1] << ", " << moves[2] << ", "  << moves[3] << ".\n\n";
		cout << "By Blackout22\n\n";

		cout << "Score : Wins " << results[2] << " Losses " << results[0] << " Draws " << results[1] << "\n\n";

		cout<< "\nWhat do you want to choose?" 
			<< "\n\n 1. "	<< moves[1]
			<< "\n 2. "		<< moves[2]	
			<< "\n 3. "		<< moves[3];

		int userPick = getUserPick();
		int compPick = getCompPick();

		int outcome = decideWinner(userPick, compPick);

		results[outcome]++;

		outputResult(userPick, compPick, outcome);

		ok = getYesNo("Do you want to go again?");

	}while (ok);

	return 0;
}
/////////////////////////////////////////////////////////////////
// Yes No Selector Function
/////////////////////////////////////////////////////////////////
bool getYesNo(char *titleText) // Tested Y/N key getter
{
	char resp = 0;

	cout << "\n" << titleText << " (y/n) ";

	do
	{
		resp = getch();
		resp = resp | 32; //OR 32 makes uppercase to lowercase (using binary maths)
	}
	while (resp != 'n' && resp != 'y');

	cout << resp << endl << endl;

	return resp == 'y';
}
/////////////////////////////////////////////////////////////////
// User Move Function
/////////////////////////////////////////////////////////////////
int getUserPick()
{
	char resp;

	cout << "\n\n Enter you choice (1 - 3): ";

	do // get used to this sort of loop, you will end up relying on them
	{
		resp = getch();
	}
	while( resp < '1' || resp > '3'); // end userpick loop

	cout << resp << endl;
	return resp - '0'; // convert ASCII code to actual number
}
/////////////////////////////////////////////////////////////////
// Computer Move Function
/////////////////////////////////////////////////////////////////
int getCompPick()
{
	srand (time(0));
	
	return rand () % 3 + 1;

}
/////////////////////////////////////////////////////////////////
// Game Decision Function
/////////////////////////////////////////////////////////////////
int decideWinner(int userPick, int comPick)
{
	if(comPick== userPick) 
		return 1; // draw

	else // a winner and a loser
	{
		int beatsComputer = comPick+ 1;

		if(beatsComputer == 4)
			beatsComputer = 1;


		if(userPick == beatsComputer)
			return  2; // win
		else
			return 0; // loss
	}
}

/////////////////////////////////////////////////////////////////
// Game Result Output Function
/////////////////////////////////////////////////////////////////
void outputResult(int userPick, int compPick, int outcome)
{
	cout <<"\nThe computer picked " << moves[compPick] << ", ";
	cout <<"and you picked " << moves[userPick] << ".\n\n";

	switch(outcome)
	{
		case 0: cout << moves[compPick] << " " << effects[compPick] << " " << moves[userPick] << " So You Lose!\n";break;
		case 1: cout << "They're The Same ! So It's A Draw!\n";	break;
		case 2: cout << moves[userPick] << " " << effects[userPick] << " " << moves[compPick] << " So You Win!\n"; break;
	}
}

Reply With Quote
  #28  
Old November 15th, 2008, 11:07 AM
Blackout22 Blackout22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 14 Blackout22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 24 sec
Reputation Power: 0
wow ... thank you very much ... now i just wanted to ask ... what is getch () and what does it do ...?

Reply With Quote
  #29  
Old November 15th, 2008, 11:37 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 11
"getch" reads a SINGLE character from the input stream, so it's responsive more like an arcade game etc, than cin where you have to hit enter after typing

Reply With Quote
  #30  
Old November 15th, 2008, 11:39 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 11
I just made an even more refined version - got decideWinner down to ONE line of code and few other tweaks you may be interested in.

plus it has instructions now

Code:
#include <iostream>
#include <ctime>
#include <conio.h> // needed for 'getch()' and kbhit()
using namespace std;
/////////////////////////////////////////////////////////////////
// Program Data
/////////////////////////////////////////////////////////////////

// Uncomment these two data lines to get "normal" rock paper scissors

//char *moves[4] = {"View Instructions", "Rock", "Paper", "Scissors"}; // names of the choices
//char *effects[4] = {"", "Blunts", "Wraps", "Cuts"}; // names of the effects

// Just for fun ... ANIMAL rock paper scissors

char *moves[4] = {"View Instructions", "Mouse", "Cat", "Elephant"}; // names of the choices
char *effects[4] = {"", "Scares", "Eats", "Stomps"}; // names of the effects

int results[3]; // to count wins, losses and draws

enum // enum is like a table of const variables, very handy. you can number them, but if you don't numbering is automatic anyway
{
	resultDraw,
	resultLose,
	resultWin
};
/////////////////////////////////////////////////////////////////
// Function Prototypes
/////////////////////////////////////////////////////////////////
bool getYesNo(char *titleText); // Y/N key getter
int getUserPick();
int getCompPick();
int decideWinner(int userPick, int comPick);
void outputResult(int userPick, int comPick, int outcome);


int beats(int i) // returns the choice which beats choice 'i'
{
	return (i == 3) ? 1 : i + 1;
}
/////////////////////////////////////////////////////////////////
// MAIN
/////////////////////////////////////////////////////////////////
int main ()
{
	bool ok;

	do
	{
		system("cls");

		cout << "Welcome to " << moves[1] << ", " << moves[2] << ", "  << moves[3] << ".\n\n";
		cout << "By Blackout22\n\n";

		cout << "Score : Wins " << results[resultWin] << " Losses " << results[resultLose] << " Draws " << results[resultDraw] << "\n\n";

		cout<< "What do you want to choose?\n";

		for(int i= 0; i < 4; ++i) 
			cout << "\n " << i << ". " << moves[i];

		int userPick = getUserPick();

		if(userPick) // play game
		{
			int compPick = getCompPick();

			int outcome = decideWinner(userPick, compPick);

			results[outcome]++;

			outputResult(userPick, compPick, outcome);
		}
		else // show rules
		{
			system("cls");

			cout << moves[1] << ", " << moves[2] << ", "  << moves[3] << ".\n\n";
			cout << "Try and beat the computers choice.\n\n\n";

			for(int i = 1; i < 4; ++i)
				cout << moves[beats(i)] << " " << effects[beats(i)] << " " << moves[i] << ".\n\n\n";
		}

		ok = getYesNo("Do you want to keep playing ?");			
	}
	while (ok);

	return 0;
}
/////////////////////////////////////////////////////////////////
// Yes No Selector Function
/////////////////////////////////////////////////////////////////
bool getYesNo(char *titleText) // Tested Y/N key getter
{
	char resp = 0;

	cout << "\n" << titleText << " (y/n) ";

	do
	{
		resp = getch();
		resp = resp | 32; //OR 32 makes uppercase to lowercase (using binary maths)
	}
	while (resp != 'n' && resp != 'y');

	cout << resp << endl << endl;

	return resp == 'y';
}
/////////////////////////////////////////////////////////////////
// User Move Function
/////////////////////////////////////////////////////////////////
int getUserPick()
{
	char resp;

	cout << "\n\n Enter you choice (0 - 3): ";

	do // get used to this sort of loop, you will end up relying on them
	{
		resp = getch();
	}
	while( resp < '0' || resp > '3'); // end userpick loop

	cout << resp << endl;
	return resp - '0'; // convert ASCII code to actual number
}
/////////////////////////////////////////////////////////////////
// Game Decision Function
/////////////////////////////////////////////////////////////////
int getCompPick()
{
	srand (time(0));	
	return rand () % 3 + 1;
}
int decideWinner(int userPick, int compPick)
{
	// a condensed type of "if" statement the ? is "if" and the : is "else"
	return (compPick == userPick) ? resultDraw : (userPick == beats(compPick)) ? resultWin : resultLose; 	
}
/////////////////////////////////////////////////////////////////
// Game Result Output Function
/////////////////////////////////////////////////////////////////
void outputResult(int userPick, int compPick, int outcome)
{
	cout <<"\n\nThe computer picked " << moves[compPick] << ", ";
	cout <<"and you picked " << moves[userPick] << ".\n\n";

	switch(outcome)
	{
		case resultLose: cout << moves[compPick] << " " << effects[compPick] << " " << moves[userPick] << " So You Lose!\n";break;
		case resultDraw: cout << "They're The Same ! So It's A Draw!\n";	break;
		case resultWin: cout << moves[userPick] << " " << effects[userPick] << " " << moves[compPick] << " So You Win!\n"; break;
	}
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > General - Need Help with functions please


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap