C++ PROGRAM When I input 3 S P R, it was suppoesed to pop up L W T. But it showe
ID: 3804223 • Letter: C
Question
C++ PROGRAM When I input 3 S P R, it was suppoesed to pop up L W T. But it showed L L L. The moveNo is not working.
#include <iostream>
#include "computer.h"
#include "human.h"
#include "referee.h"
using namespace std;
int main()
{
human h;
computer c;
referee r;
r.compare(h,c);
return 0;
}
#include<iostream>
#include "computer.h"
using namespace std;
//dumb computer, only choose R
char computer:: move(){
return 'R';
}
#ifndef COMPUTER_H
#define COMPUTER_H
class computer{
public:
char move();
};
#endif // COMPUTER_H
#include<iostream>
#include "human.h"
using namespace std;
human::human(){
cin>>totalMoves;
moves=new char[totalMoves];
for(int i=0;i<totalMoves;i++){
cin>>moves[i];
}
}
char human::move(){
char returnH=moves[moveNo];
return returnH;
}
#ifndef HUMAN_H
#define HUMAN_H
class human
{
public:
char*moves;
int moveNo;
int totalMoves;
human();
char move();
};
#endif // HUMAN_H
#include<iostream>
#include "referee.h"
#include "computer.h"
#include "human.h"
using namespace std;
void referee::compare(human h, computer c){
//char result='T';
char humanMove=h.move();
char computerMove=c.move();
int totalMoves=h.totalMoves;
for(int i=0;i<totalMoves;i++){
if(humanMove=='R'){
cout<<'T'<<" ";
}
if(humanMove=='P'){
cout<<'W'<<" ";
}
if(humanMove=='S'){
cout<<'L'<<" ";
}
}cout<<endl;
}
#ifndef REFEREE_H
#define REFEREE_H
#include "computer.h"
#include "human.h"
class referee{
public:
void compare(human h, computer c);
};
#endif // REFEREE_H
Explanation / Answer
#include <iostream>
#include "computer.h"
#include "human.h"
#include "referee.h"
using namespace std;
int main()
{
human h;
computer c;
referee r;
r.compare(h,c);
return 0;
}
#include<iostream>
#include "computer.h"
using namespace std;
//dumb computer, only choose R
char computer:: move(){
return 'R';
}
#ifndef COMPUTER_H
#define COMPUTER_H
class computer{
public:
char move();
};
#endif // COMPUTER_H
#include<iostream>
#include "human.h"
using namespace std;
human::human(){
cin>>totalMoves;
moves=new char[totalMoves];
for(int i=0;i<totalMoves;i++){
cin>>moves[i];
}
}
char human::move(){
char returnH=moves[moveNo];
return returnH;
}
#ifndef HUMAN_H
#define HUMAN_H
class human
{
public:
char*moves;
int moveNo;
int totalMoves;
human();
char move();
};
#endif // HUMAN_H
#include<iostream>
#include "referee.h"
#include "computer.h"
#include "human.h"
using namespace std;
void referee::compare(human h, computer c){
//char result='T';
char humanMove=h.move();
char computerMove=c.move();
int totalMoves=h.totalMoves;
for(int i=0;i<totalMoves;i++){
if(humanMove=='R'){
cout<<'T'<<" ";
}
if(humanMove=='P'){
cout<<'W'<<" ";
}
if(humanMove=='S'){
cout<<'L'<<" ";
}
}cout<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.