In: Computer Science
Im getting an error on the Player.h class.
Error reads expected initializer before player.
I have put a comment by the line with the error.
sample out put is
Welcome to Rock Paper Scissors
You played: paper
Computer played: paper
It's a tie
Player.h
#include<string>
using namespace std;
class Player{
private:
int play;//variable
public:
string getPlay();//variables
void setPlay(string play);
void setPlay(int play);
}
string Player = getPlay(){//error
if(this.play == 1){
return "rock";
}
else if(this.play == 2){
return
"paper";
}
else if(this.play == 3){
return
"scissors";
}
return "invalid";
}
void Player :: setPlay(stringPlay){//set players
string play
if(play == "rock"){
this.play = 1;
}
else if(play == "paper"){
this.play = 2;
}
else if(play == "scissors"){
this->play = 3;
}
}
void Player :: setPlay(int play){//set players int
play
this.play = play;
}
};
RSPDriver.cpp
#include <iostream>
#include"Player.h"
using namespace std;
int main() {
int comupterPlay;//varriables
int lastPlay;
string userPlay;
Player computer;
Player player;
do{
cout<<"Type rock, paper,
scissors: ";//get users play
cin>>userPlay;
player.setPlay(userPlay);//set
users play
}
do{
computerPlay =
rand() % 3 + 1;//get computers random play
}
while(computerPlay == lastPlay);//set computers play
lastPlay =
computerPlay;
computer.setPlay(computerPlay);
cout<<"Computer played:
"<<computer.getPlay()<<endl;
displayWinner(computer.getPlay(),
player.getPlay())<<endl;
}
return
0:
}
void displayWinner(string com, string player){//define
winner
if(com== "rock" && player
== "rock")
cout<<"It's a tie"<<endl;
else if(com=="rock" && player =="paper")
cout<<"Player wins"<<endl;
else if(com=="rock" && player=="scissors")
cout<<"Computer wins"<<endl;
else if(com=="paper" && player=="paper")
cout<<"It's a tie"<<endl;
else if(com=="paper" && player=="scissors")
cout<<"Player wins"<<endl;
else if(com=="paper" && player=="rock")
cout<<"Computer wins"<<endl;
else if(com=="scissors" && player=="scissors")
cout<<"It's a tie"<<endl;
else if(com=="scissors" && player=="rock")
cout<<"Player wins"<<endl;
else if(com=="scissors" && player=="paper")
cout<<"Computer wins"<<endl;
}
}
Solution:
Player.h file:
#include<string>
using namespace std;
class Player{
private:
int play;//variable
public:
string getPlay();//variables
void setPlay(string Play);
void setPlay(int play);
};
string Player::getPlay(){
if(this->play == 1){
return "rock";
}
else if(this->play == 2){
return "paper";
}
else if(this->play == 3){
return "scissors";
}
return "invalid";
}
void Player :: setPlay(string Play){//set players string play
if(Play == "rock"){
this->play = 1;
}
else if(Play == "paper"){
this->play = 2;
}
else if(Play == "scissors"){
this->play = 3;
}
}
void Player :: setPlay(int play){//set players int play
this->play = play;
}
Drivercode.cpp
#include <iostream>
#include "player.h"
using namespace std;
void displayWinner(string com, string player){//define winner
if(com== "rock" && player == "rock")
cout<<"It's a tie"<<endl;
else if(com=="rock" && player =="paper")
cout<<"Player wins"<<endl;
else if(com=="rock" && player=="scissors")
cout<<"Computer wins"<<endl;
else if(com=="paper" && player=="paper")
cout<<"It's a tie"<<endl;
else if(com=="paper" && player=="scissors")
cout<<"Player wins"<<endl;
else if(com=="paper" && player=="rock")
cout<<"Computer wins"<<endl;
else if(com=="scissors" && player=="scissors")
cout<<"It's a tie"<<endl;
else if(com=="scissors" && player=="rock")
cout<<"Player wins"<<endl;
else if(com=="scissors" && player=="paper")
cout<<"Computer wins"<<endl;
}
int main() {
int computerPlay;//varriables
int lastPlay;
string userPlay;
Player computer;
Player player;
cout<<"Type rock, paper, scissors: ";//get users play
cin>>userPlay;
player.setPlay(userPlay);//set users play
do{
computerPlay = rand() % 3 + 1;//get computers random play
}while(computerPlay == lastPlay);//set computers play
lastPlay = computerPlay;
computer.setPlay(computerPlay);
cout<<"Computer played: "<<computer.getPlay()<<endl;
displayWinner(computer.getPlay(), player.getPlay());
cout<<endl;
return 0;
}
Code screenshot:
Code output screenshot: