Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Code not working, thought it kept getting stuck in the infinite loop, but fixes

ID: 3662636 • Letter: C

Question

Code not working, thought it kept getting stuck in the infinite loop, but fixes didn't work

#include "gameAdmin.hpp"
#include <iostream>
#include <ctime>

int main()
{

std::srand(std::time(0));

std::cout << "Do you want Player 1 to use a loaded die? press 1 for yes 2 for no"<<endl;
int c;
std::cin >> c;
  
  
std::cout << "Do you want Player 1 to use a loaded die? press 1 for yes 2 for no"<<endl;
int a;
std::cin>>a;

std::cout << "How many rounds would you like to play? ";
int rounds=-1;
  
do
{
std::cin >> rounds;
if (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(5000, ' ');
}
}
   while (rounds < 1);

Game p(rounds, 6, c, a);
p.play();
  

   return 0;
  
}

#ifndef GAMEADMIN_HPP
#define GAMEADMIN_HPP
#include "die.hpp"
#include "loadedDie.hpp"
using namespace std;

class Game
{
   private:
   int s1;
   int s2;
   Die *p1;
   Die *p2;
   int rd;
   public:
Game(int, int, int, int);
void play();
void round();
void print();  
};

#include "gameAdmin.hpp"
#include "die.hpp"
#include "loadedDie.hpp"

Game::Game(int rounds, int sides, int c, int a)
{
   rd=1;
   s1=0;
   s2=0;
if (c==1)
{
    new LoadedDie(sides);
    new Die(sides);
}
  
else if (a==1)
{
        new LoadedDie(sides);
    new Die(sides);
}
}

void Game::round()
{
   int roundcount=1;
std::cout << "Round " << roundcount << ": Player 1: ";
  
int r1 = p1->throwdice();
std::cout << " Player 2: ";
int r2 = p1->throwdice();

std::cout << " ";
  
if (r1 > r2)
{
++s1;
}
else if (r1 < r2)
{
    ++s2;
}

std::cout << "Player 1 score: " << s1 << ' ' <<
"Player 2 score: " << s2 << ' ';

++roundcount;
}

void Game::play()
{
std::cout << "New game. ";

while (rd <= 10)
{
   round();
}

print();
}


void Game::print()
{
   std::cout << "Result: ";
if (s1 > s2)
std::cout << "Player 1 wins! ";
else if (s1 < s2)
std::cout << "Player 2 wins! ";
else
std::cout << "It's a draw! ";
}

#include <iostream>
#include <cstdlib>
#include <time.h>
#ifndef DIE_HPP
#define DIE_HPP


using namespace std;

class Die
{
   private:
   int numsides1;
   public:
   Die();
   Die(int sides);
virtual int throwdice();

   
};

#endif

#include "die.hpp"
#include <cstdlib>

using namespace std;

Die::Die()
{
   int sides=6;
}
Die::Die(int sides)
{
   int numsides1=sides;
}
int Die::throwdice()
{
int o = rand() % numsides1 + 1;
std::cout << " " << o << ' ';
return o;
}

#ifndef LOADEDDIE_HPP
#define LOADEDDIE_HPP

class LoadedDie: public Die
{
private:
int numsides;
public:
LoadedDie(int sides);
virtual int throwdice();
  
};

#endif

#include "loadedDie.hpp"
#include <cstdlib>
#include <time.h>
#include <iostream>
using namespace std;

LoadedDie::LoadedDie(int sides)
{
   int numsides=sides;
}
int LoadedDie::throwdice()
{
   if (rand()%2==0)
   {
       int j=0;
       j=rand()%2+1;
       cout<< "roll was "<<j<<endl;
       return j;
   }
   else
   {
       int u=0;
       u=rand()%numsides+1;
       std::cout<<"roll was "<<u<<endl;
       return u;
   }
}

Explanation / Answer

The change is given in bold and underlined:

#include "gameAdmin.hpp"
#include <iostream>
#include <ctime>

int main()
{

std::srand(std::time(0));

std::cout << "Do you want Player 1 to use a loaded die? press 1 for yes 2 for no"<<endl;
int c;
std::cin >> c;
  
  
std::cout << "Do you want Player 1 to use a loaded die? press 1 for yes 2 for no"<<endl;
int a;

int rounds=-1;
std::cin>>a;

std::cout << "How many rounds would you like to play? ";

std::cin >> rounds;
do
{
if (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(5000, ' ');
}
}
   while (rounds < 1);

Game p(rounds, 6, c, a);
p.play();
  

   return 0;
  
}

#ifndef GAMEADMIN_HPP
#define GAMEADMIN_HPP
#include "die.hpp"
#include "loadedDie.hpp"
using namespace std;

class Game
{
   private:
   int s1;
   int s2;
   Die *p1;
   Die *p2;
   int rd;
   public:
Game(int, int, int, int);
void play();
void round();
void print();  
};

#include "gameAdmin.hpp"
#include "die.hpp"
#include "loadedDie.hpp"

Game::Game(int rounds, int sides, int c, int a)
{
   rd=1;
   s1=0;
   s2=0;
if (c==1)
{
    new LoadedDie(sides);
    new Die(sides);
}
  
else if (a==1)
{
        new LoadedDie(sides);
    new Die(sides);
}
}

void Game::round()
{
   int roundcount=1;
std::cout << "Round " << roundcount << ": Player 1: ";
  while(roundcount <= rounds)

{
int r1 = p1->throwdice();
std::cout << " Player 2: ";
int r2 = p1->throwdice();

std::cout << " ";
  
if (r1 > r2)
{
++s1;
}
else if (r1 < r2)
{
    ++s2;
}

std::cout << "Player 1 score: " << s1 << ' ' <<
"Player 2 score: " << s2 << ' ';

++roundcount;

}
}

void Game::play()
{
std::cout << "New game. ";

while (rd <= 10)
{
   round();
}

print();
}


void Game::print()
{
   std::cout << "Result: ";
if (s1 > s2)
std::cout << "Player 1 wins! ";
else if (s1 < s2)
std::cout << "Player 2 wins! ";
else
std::cout << "It's a draw! ";
}

#include <iostream>
#include <cstdlib>
#include <time.h>
#ifndef DIE_HPP
#define DIE_HPP


using namespace std;

class Die
{
   private:
   int numsides1;
   public:
   Die();
   Die(int sides);
virtual int throwdice();

   
};

#endif

#include "die.hpp"
#include <cstdlib>

using namespace std;

Die::Die()
{
   int sides=6;
}
Die::Die(int sides)
{
   int numsides1=sides;
}
int Die::throwdice()
{
int o = rand() % numsides1 + 1;
std::cout << " " << o << ' ';
return o;
}

#ifndef LOADEDDIE_HPP
#define LOADEDDIE_HPP

class LoadedDie: public Die
{
private:
int numsides;
public:
LoadedDie(int sides);
virtual int throwdice();
  
};

#endif

#include "loadedDie.hpp"
#include <cstdlib>
#include <time.h>
#include <iostream>
using namespace std;

LoadedDie::LoadedDie(int sides)
{
   int numsides=sides;
}
int LoadedDie::throwdice()
{
   if (rand()%2==0)
   {
       int j=0;
       j=rand()%2+1;
       cout<< "roll was "<<j<<endl;
       return j;
   }
   else
   {
       int u=0;
       u=rand()%numsides+1;
       std::cout<<"roll was "<<u<<endl;
       return u;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote