Hi

I am making a rock, paper scissors game for fun & for practice.
My problem is; I am trying to create a string by adding 2 character variables together. How do I do this?

Do I use the insert function? Or something I think is called crstrt or something similar to that.

Also I have a question; I have made this game using only one function & it is rather simple. In my text book, I looked over its example of a rock paper scissors game & it uses around 4 functions; a function to check if input is allowed using a switch, a fucntion to determine the winner, some other function I forget & finally a function to print the outcome of the game & it also stores the rock paper scissors input options in an enumeration.

My question is...What is better programming practice, the book version or my version which is simple. I always thought that in programming simple( & less lines of code) is better because it also means the program will preform faster, you know KISS (keep it simple...). If I were working for a business which code would be better/wanted?

Here's my code (the problem is located in the fuction):

#include <iostream>
#include <string>
#include <cstring>


using namespace std;

int find_winner(char p1, char p2);

int main()
{
	char p1choice, p2choice;
	int p1_score = 0, p2_score = 0;
	char winner;


	cout << "Player one, Enter either rock, paper or scissors(r,p,s): ";
	cin >> p1choice;

	cout << "Player two, Enter either rock, paper or scissors(r,p,s): ";
	cin >> p2choice;

	if  ( p1choice == p2choice )
	{
		cout << "Game is a tie\n";
	}
	else
	{

		winner = find_winner(p1choice, p2choice);

		cout << winner << "\n";

		if (p1choice == winner)
		{
			p1_score++;
			cout << "The winner is Player one!!\n";
			cout << "P1 Score: "<< p1_score << "  P2 Score: "<< p2_score << "\n";
		}
		else if (p2choice == winner)
		{
			p2_score++;
			cout << "The winner is Player two!!\n";
			cout << "P1 Score: "<< p1_score << "  P2 Score: "<< p2_score << "\n";
		}
	}

	return 0;
}

int find_winner(char p1, char p2)
{
	char winner;

	// below is where I am trying to add 2 character into the string choices
	string choices;

	choices.insert(p1[0]);
	choices.insert(p1[0]);

	if ( choices == "sp" || choices == "ps" )
	{
		return winner = 's';
	}
	else if ( choices == "pr" || choices == "rp" )
	{
		return winner = 'p';
	}
	else if ( choices == "rs" || choices == "sr" )
	{
		return winner = 'r';
	}
	else
		return winner = 'M';
}

Several things from your question:

First the string part. You have many options (a) you can indeed use
insert but the method requires an iterator to define the position for insertion. (b) the easiest to use simply += e.g

char p='x';
std::string A("test");
A+=x;
// A is now testx

Second :note your mistake in your function, you have used pl[0]
but it is a simple character. It is not an array.

Third: I don't understand why you have the variable winner. you assign a value to it and then return from the function. The means that the variable is immediately lost.
do this return 'M'; instead.

Also you claim to return an integer from find_player but you return a character. That is acceptable to the compile (with warnings) but unlikely to be the intent. Surely you wanted to return 0 if it is a draw and 1 if player one won and 2 (or maybe -1) if player 2 won.

Also, don't make a string and then compare, just use the individual result. e.g

// after test for p1,p2 only are s,r,p.
if (p1==p2)   // draw 
  return 0;
if (p1=='r')
   return (p2=='p') ? 2 : 1;
// .. etc

you might want a switch statement, you might also use a lookup table etc.

As for production code, I feel that you need a lot more experience getting code to work and compile before you even ask the question. Nobody checks in code that doesn't compile. Only then do we discuss classes and methods etc. There are many dicussions on the subject and over a time you develop your own style that needs to be adjusted to the project you are working on, however, don't worry until you have written some more c++, then read Scott Myers 50 and 35 C++ Tips, then worry about it.

@StuXYZ :

Several things from your question:
First the string part. You have many options (a) you can indeed use
insert but the method requires an iterator to define the position for insertion. (b) the easiest to use simply += e.g

char p='x';
std::string A("test");
A+=x;
// A is now testx

Make sure that the examples you give are correct because in the above code what you have stated is wrong and it would give rise to a compiler error stating that char variable x not declared.
Its either :

char p='x';
std::string A("test");
A+=p;
// A is now testx

or better and simple do this :

//char p='x';//Not at all required
std::string A("test");
A+='x';
// A is now testx

Also, don't make a string and then compare, just use the individual result. e.g

// after test for p1,p2 only are s,r,p.
if (p1==p2)   // draw 
  return 0;
if (p1=='r')
   return (p2=='p') ? 2 : 1;
// .. etc

you might want a switch statement, you might also use a lookup table etc.

Thanks for your help :) Finding the winner the way you suggested above is alot smarter :) I'd like to use this kind of thing in the future, but when I return either 0,1 or 2 back into main how do I still determine the winner?

Is it just a simple case of using if/esle, eg

if ( find_winner(p1, p2) == 0 )
{
	cout << "Tie\n";
}
else if ( find_winner(p1, p2) == 1 )
{
	cout << "P1 wins\n";
}
else if ( find_winner(p1, p2) == 2 )
{
	cout << "P2 wins\n";
}

function

int find_winner(char p1, char p2)
{
	if (p1 == p2)
	{
		return 0;
	}
	else if ( p1 == 's')
	{
		return ( p2 == 'r' ) ? 2:1;
	}
	else if ( p1 == 'r')
		{
			return ( p2 == 'p' ) ? 2:1;
		}
	else if ( p1 == 'p')
		{
			return ( p2 == 's' ) ? 2:1;
		}
}

i read this as "rock paper scissor string" problem.

and thought the problem is, that "string" doesnt fit the "rock paper scissors" paradigm.

:cool:

This question has already been answered. Start a new discussion instead.