Question

C++ PROGRAM When I input 3 S P R, it was suppoesed to pop up L...

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

Homework Answers

Answer #1

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!

  • Take a look at this statement :

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.

  • What you need to do to solve this problem?:

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!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Menu.cpp isn't working. C++  utilize inheritance to create a banking app. The architecture of the app is...
Menu.cpp isn't working. C++  utilize inheritance to create a banking app. The architecture of the app is up to you as long as you implement some form of inheritance. Below is my code however the credit and the actual menu isn't working. Can i get some help on getting the menu to work properly. // BankAccount.h #ifndef ACCOUNT_H #define ACCOUNT_H #include <string> #include <iostream> using namespace std; class BankAccount { protected : int noOfWithdrawls; private: // Declaring variables    float balance;...
How to trace a c++ program by hand #include<iostream> using namespace std;    class Test {...
How to trace a c++ program by hand #include<iostream> using namespace std;    class Test {     int value; public:     Test(int v); };    Test::Test(int v) {     value = v; }    int main() {     Test t[100];     return 0; } _______________ #include <iostream> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { for(j=1; j<=i; j++ ) { cout<<"*"; } cout << "\n";   } return 0; }
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath>...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath> using namespace std; void divisors(int num); int main () {    char repeat;    int num;       while (repeat !='n')    {        cout << "Enter a number: ";        cin >> num;        divisors(num);        cout << "Continue? (y or n): ";        cin >> repeat;    }    return 0; } void divisors(int num) {   ...
No matter what I do I cannot get this code to compile. I am using Visual...
No matter what I do I cannot get this code to compile. I am using Visual Studio 2015. Please help me because I must be doing something wrong. Here is the code just get it to compile please. Please provide a screenshot of the compiled code because I keep getting responses with just code and it still has errors when I copy it into VS 2015: #include <iostream> #include <conio.h> #include <stdio.h> #include <vector> using namespace std; class addressbook {...
Complete the missing code for the constructors as indicated in the comments in all three header...
Complete the missing code for the constructors as indicated in the comments in all three header files. C++ mainDriver.cpp #include <string> #include "Address.h" #include "Date.h" #include "Person.h" using namespace std; int main() {    Person p1;    Person p2("Smith", "Bobby", "[email protected]", 111, "Main St", "Clemson",            "SC", 29630, 1, 31, 1998);    cout << endl << endl;    p1.printInfo();    p2.printInfo();    return 0; } Person.h #ifndef PERSON_H #define PERSON_H #include <iostream> #include <string> using namespace std; class...
In main.cpp, write a templated function more which takes in two variables of the same type...
In main.cpp, write a templated function more which takes in two variables of the same type and returns whichever variable is greater than (>) the other. You can and may need to add something to the food class in order for more to be able to be called properly. //food.h #ifndef _FOOD_H #define _FOOD_H #include <iostream> #include <string> using namespace std; class Food { private: string name; int quantity; public: Food(); void setName(string newName); void setQuantity(int newQuantity); string getName(); int...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Your code here ----------------- } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to...
Quick sort func in C++ #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc...
Quick sort func in C++ #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Code here } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to manipulate the elements...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream>...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream> using namespace std; void shownumbers(int, int); int main() { int x, y; cout << "Enter first number : "; cin >> x; cout << "Enter second number : "; cin >> y; shownumbers(x, y); return 0; } void shownumbers(int a, int b) { bool flag; for (int i = a + 1; i <= b; i++) { flag = false; for (int j =...
Please fill in the blank bolded below that would be the best choice for this program....
Please fill in the blank bolded below that would be the best choice for this program. #include <iostream> using namespace std; const int size = 100000; class TheBig { public: double operator[](int index) const {return (theData[index]);} private: double theData[size]; }; void firstToBeThe( ______________________________________________________) { for (int i = 0; i <size; i++) cout << value[i] <<endl; } int main() { TheBig one; firstToBeThe(one); }
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT