C++ PROGRAM When I input 3 S P R, it was suppoesed to pop up L W T. But it showed L L L.IDK why the moveNo is not working. I am asking for help, plz dont put some random things on it.
main.cpp
#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;
}
computer.cpp
#include<iostream>
#include "computer.h"
using namespace std;
//dumb computer, only choose R
char computer:: move(){
return 'R';
}
computer.h
#ifndef COMPUTER_H
#define COMPUTER_H
class computer{
public:
char move();
};
#endif // COMPUTER_H
human.cpp
#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];
}moveNo=0;
}
char human::move(){
char returnH=moves[moveNo];
moveNo=MoveNo+1;
return returnH;
}
human.h
#ifndef HUMAN_H
#define HUMAN_H
class human
{
public:
char*moves;
int moveNo;
int totalMoves;
human();
char move();
};
#endif // HUMAN_H
referee.cpp
#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;
}
referee.h
#ifndef REFEREE_H
#define REFEREE_H
#include "computer.h"
#include "human.h"
class referee{
public:
void compare(human h, computer
c);
};
#endif // REFEREE_H
The problem with this code is very simple. The issue here is in file "referee.cpp"
Your code:
#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;
}
Your Code ENDS here!
char humanMove=h.move();
You declared the statement outside of the loop. As a result, the move() method is called only once and returns the move at initial index=0 which is 'S' in the variable humanMove. Now coming to the loop, for every pass of the loop, the program checks for the value humanMove (which, in your case, remains same everytime,, as you called the move function only once) and gives the result for it, which happens to be 'L' everytime and your program prints out "L L L" Now you know why. Because the value of humanMove is never changed.
You only need to move the statement,
char humanMove=h.move();
inside the 'for' loop in "referee.cpp". What this does is, now it calls the move() function for every pass of the loop and gets the next move in humanMove from the move() method in human.cpp. Below is a revised version of Your Code. Check it out:
#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(); // humanMove Statement
//char computerMove=c.move();
int totalMoves=h.totalMoves;
for(int i=0;i<totalMoves;i++){ // For Loop
char humanMove=h.move(); // Moved the "humanMove Statement" to
here
if(humanMove=='R'){
cout<<'T'<<" ";
}
if(humanMove=='P'){
cout<<'W'<<" ";
}
if(humanMove=='S'){
cout<<'L'<<" ";
}
}
cout<<endl;
}
My Code ENDS here!
If the code in text looks confusing, here's a screenshot of the referee.cpp in Code::Blocks:
Following is a Screenshot of the Output in the Console Screen for input: 3 S P R
The program was very nicely and neatly written. Thanks to that, I could understand it faster. Good job there!
Hope this helps. Have a good time!
Get Answers For Free
Most questions answered within 1 hours.