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';
}