Feedback + Bad output
- by user1770094
So I've got an assignment I think I'm more or less done with, but there is something which is messing up the output badly somewhere down the line, or even the calculation, and I don't see where the problem is.
The assignment is to make a game in which a certain ammount of players run up through a tunnel towards a spot,where they will stop and spin around it,and then their dizziness is supposed to make them randomly either progress towards goal or regress back towards start.And each time they get another spot closer to goal,they get another "marking",and it goes on like this until one of them reaches goal.
The program includes three files: one main.cpp,one header file and another cpp file.
The header file:
#ifndef COMPETITOR_H
#define COMPETITOR_H
#include <string>
using namespace std;
class Competitor
{
public:
void setName();
string getName();
void spin();
void move();
int checkScore();
void printResult();
private:
string name;
int direction;
int markedSpots;
};
#endif // COMPETITOR_H
The second cpp file:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include "Competitor.h"
using namespace std;
void Competitor::setName()
{
cin>>name;
}
string Competitor::getName()
{
return name;
}
void Competitor::spin()
{
srand(time(NULL));
direction = rand()%1+0;
}
void Competitor::move()
{
if(direction == 1)
{
markedSpots++;
}
else if(direction == 0 && markedSpots != 0)
{
markedSpots--;
}
}
int Competitor::checkScore()
{
return markedSpots;
}
void Competitor::printResult()
{
if(direction == 1)
{
cout<<" is heading towards goal and has currently "<<markedSpots<<" markings.";
}
else if(direction == 0)
{
cout<<"\n"<<getName()<<" is heading towards start and has currently "<<markedSpots<<" markings.";
}
}
The main cpp file:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include "Competitor.h"
using namespace std;
void inputAndSetNames(Competitor comps[],int nrOfComps);
void makeTwist(Competitor comps[],int nrOfComps);
void makeMove(Competitor comps[],int nrOfComps);
void showAll(Competitor comps[],int nrOfComps);
int winner(Competitor comps[],int nrOfComps, int nrOfTwistPlaces);
int main()
{
int nrOfTwistPlaces;
int nrOfComps;
int noWinner = -1;
int laps = 0;
cout<<"How many spinning places should there be? ";
cin>>nrOfTwistPlaces;
cout<<"How many competitors should there be? ";
cin>>nrOfComps;
Competitor * comps = new Competitor[nrOfComps];
inputAndSetNames(comps, nrOfComps);
do
{
laps++;
cout<<"\nSpin "<<laps<<":";
makeTwist(comps, nrOfComps);
makeMove(comps, nrOfComps);
showAll(comps, nrOfComps);
}while(noWinner == -1);
delete [] comps;
return 0;
}
void inputAndSetNames(Competitor comps[],int nrOfComps)
{
cout<<"Type in the names of the "<<nrOfComps<<" competitors:\n";
for(int i=0;i<nrOfComps;i++)
{
comps[i].setName();
}
cout<<"\n";
}
void makeTwist(Competitor comps[],int nrOfComps)
{
for(int i=0;i<nrOfComps;i++)
{
comps[i].spin();
}
}
void makeMove(Competitor comps[],int nrOfComps)
{
for(int i=0;i<nrOfComps;i++)
{
comps[i].move();
}
}
void showAll(Competitor comps[],int nrOfComps)
{
for(int i=0;i<nrOfComps;i++)
{
comps[i].printResult();
}
cout<<"\n\n";
system("pause");
}
int winner(Competitor comps[],int nrOfComps, int nrOfTwistPlaces)
{
int end = 0;
int score = 0;
for(int i=0;i<nrOfComps;i++)
{
score = comps[i].checkScore();
if(score == nrOfTwistPlaces)
{
end = 1;
}
else end = -1;
}
return end;
}
I'd be grateful if you would point out other mistakes if you see any.Thanks in advance.