反馈+输出错误

时间:2012-10-24 03:46:06

标签: c++ class header output

所以我有一个任务,我认为我或多或少已经完成了,但有些东西在某个地方甚至计算中严重搞乱了输出,而且我没有看到问题是。

任务是制作一个游戏,其中一定数量的玩家通过隧道向一个地方跑去,在那里他们会停下来旋转它,然后他们的头晕应该让他们随机地向目标前进或者回到开始回归。每当他们得到另一个接近目标的位置时,他们会得到另一个“标记”,并且它会继续这样,直到其中一个达到目标。

该程序包括三个文件:一个main.cpp,一个头文件和另一个cpp文件。

标题文件:

    #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

第二个cpp文件:

    #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.";
        }
    }

主cpp文件:

    #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;
    }

如果你发现任何其他错误,我将不胜感激。谢谢你。

1 个答案:

答案 0 :(得分:0)

您需要初始化竞争对手对象 - 添加构造函数:

Competitor::Competitor(void)
{
    direction = 0;
    markedSpots = 0;
}

此外,您调用 rand(),然后执行&#34; mod&#34;函数使用%运算符 - rand()%1 + 0 。首先,虽然我们在这里 - 为什么你加0?

但这里最大的问题是你正在做%1 ,这会给剩下的除以1.当然,这总是会得到0。

并且,您的主 while 语句永远不会退出 - 您永远不会修改 noWinner