Question

In: Computer Science

Im getting an error on the Player.h class. Error reads expected initializer before player. I have...

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

Solutions

Expert Solution

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:


Related Solutions

I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
I keep getting this error "LetterDemo.cs(21,14): error CS1519: Unexpected symbol `string' in class, struct, or interface...
I keep getting this error "LetterDemo.cs(21,14): error CS1519: Unexpected symbol `string' in class, struct, or interface member declaration" Can someone please help me. Here is my code: using static System.Console; class LetterDemo {    static void Main()    {      Letter letter1 = new Letter();      CertifiedLetter letter2 = new CertifiedLetter();      letter1.Name = "Electric Company";      letter1.Date = "02/14/18";      letter2.Name = "Howe and Morris, LLC";      letter2.Date = "04/01/2019";      letter2.TrackingNumber = "i2YD45";      WriteLine(letter1.ToString());      WriteLine(letter2.ToString() +       " Tracking number: " + letter2.TrackingNumber);    } } class Letter {...
HI. I have been trying to run my code but I keep getting the following error....
HI. I have been trying to run my code but I keep getting the following error. I can't figure out what I'm doing wrong. I also tried to use else if to run the area of the other shapes but it gave me an error and I created the private method. Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at project2.areacalculation.main(areacalculation.java:26) My code is below package project2; import java.util.Scanner; public class areacalculation { private static...
I have asked this question before however I am not getting the right answer. What is...
I have asked this question before however I am not getting the right answer. What is the evolutionary advantage to having the conduction velocity of neural impulses change with temperature, and which of the five major groups of vertebrate organisms might benefit most from this phenomenon? Explain thoroughly please.
I am implementing a generic List class and not getting the expected output. My current output...
I am implementing a generic List class and not getting the expected output. My current output is: [0, 1, null] Expected Output in a separate test class: List list = new SparseList<>(); list.add("0"); list.add("1"); list.add(4, "4"); will result in the following list of size 5: [0, 1, null, null, 4]. list.add(3, "Three"); will result in the following list of size 6: [0, 1, null, Three, null, 4]. list.set(3, "Three"); is going to produce a list of size 5 (unchanged): [0,...
I am implementing a generic List class and not getting the expected output. My current output...
I am implementing a generic List class and not getting the expected output. My current output is: [0, 1, null] Expected Output in a separate test class: List list = new SparseList<>(); list.add("0"); list.add("1"); list.add(4, "4"); will result in the following list of size 5: [0, 1, null, null, 4]. list.add(3, "Three"); will result in the following list of size 6: [0, 1, null, Three, null, 4]. list.set(3, "Three"); is going to produce a list of size 5 (unchanged): [0,...
This is in Python I am getting an error when I run this program, also I...
This is in Python I am getting an error when I run this program, also I cannot get any output. Please help! #Input Section def main(): name=input("Please enter the customer's name:") age=int(input("Enter age of the customer: ")) number_of_traffic_violations=int(input("Enter the number of traffic violations: ")) if age <=15 and age >= 105: print('Invalid Entry') if number_of_traffic_violations <0: print('Invalid Entry') #Poccessing Section def Insurance(): if age < 25 and number_of_tickets >= 4 and riskCode == 1: insurancePrice = 480 elif age >=...
I get an error when im trying to run this java program, I would appreciate if...
I get an error when im trying to run this java program, I would appreciate if someone helped me asap, I will make sure to leave a good review. thank you in advance! java class Node public class Node { private char item; private Node next; Object getNext; public Node(){    item = ' '; next = null; } public Node(char newItem) { setItem(newItem); next = null; } public Node(char newItem, Node newNext){ setItem(newItem); setNext(newNext); } public void setItem(char newItem){...
Syntax error in C. I am not familiar with C at all and I keep getting...
Syntax error in C. I am not familiar with C at all and I keep getting this one error "c error expected identifier or '(' before } token" Please show me where I made the error. The error is said to be on the very last line, so the very last bracket #include #include #include #include   int main(int argc, char*_argv[]) {     int input;     if (argc < 2)     {         input = promptUserInput();     }     else     {         input = (int)strtol(_argv[1],NULL, 10);     }     printResult(input);...
I keep getting the same error Error Code: 1822. Failed to add the foreign key constraint....
I keep getting the same error Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'test_ibfk_5' in the referenced table 'appointment', can you please tell me what is wrong with my code: -- Table III: Appointment = (site_name [fk7], date, time) -- fk7: site_name -> Site.site_name DROP TABLE IF EXISTS appointment; CREATE TABLE appointment (    appt_site VARCHAR(100) NOT NULL, appt_date DATE NOT NULL, appt_time TIME NOT NULL, PRIMARY KEY (appt_date, appt_time), FOREIGN KEY (appt_site)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT