Problems Understanding Final Program

I desperately need help with my final program in a C++ Programming class. I'm sorry, I know that it's frowned upon to bring problems like this to the forum, but I'm in a time crunch and need to understand it better.
Program Details:

For your final project, you will create an application for use by a manager of a race barn. You will start with the Horse class as your starter class. It should look very much like the Horse class you created in the lesson on arrays. The class needs variables to represent the horse name, the horse lane, and the horse time. There should be a null constructor to set the horse name to a space, the lane to zero and the time to zero and a three argument constructor to allow the user to create a horse. The class should have set and get functions for each class variable and a display function to display one Horse.

You will then create a HorseBarn class that will have 20 horse stalls. It will have one null constructor that will fill the stalls with an empty horse (there will be no horses in the barn at this point). There will be a fillStall function to allow the user to assign one horse to a particular stall. It will accept a Horse object and an integer that will represent the stall number. There should also be a printBarn function to print all the stalls and show which stalls have horses in them and which are still available. Now you will need a findStall function so the manager can determine if a stall is empty or if there is a horse in the stall. It will accept a stall number and return the name of the horse in the stall or a blank if the stall is empty. The last function will allow the manager to compact his barn and put all the horses in adjacent stalls to make caring for the horses more efficient.

Your main function will create a horse barn and an array of ten horses. You can use the ten horses you used in your Horse program. In the beginning you are to put the horses in alternate stalls with an empty stall between each horse. You will print out the contents of the barn after you put the horses in the stalls. Now you will have a loop to ask the manager for a stall number and either print out the horse name in that stall or a statement saying the stall is empty. The loop will continue until the manager doesn't want to continue (you can have the manager enter a number like a -1 to tell the loop to stop). You will now compact the barn by moving all the horses so they occupy the first 10 stalls and the last 10 stalls are empty. Now you will print the contents of the barn again to show that the barn has been compacted.

I put the Horse program that was referenced to inside the code area.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  /*
Daryian Johnson
Horses.cpp
This program should output the data of ten different horses saved into an array created in a single class.
It should then display the best, worst, and average times of all of the horses.
*/
//////////////////////////////////////
#include <iostream>
#include <cstdlib>
using namespace std;
//////////////////////////////////////

    class Horse
    {
    private:
        string name;
        int lane;
        double Time;

    public:
        Horse()
        {
            name = "";
            lane = 0;
            Time = 0.0;
        }

    Horse(string n, int l, double t)
    {
        name = n;
        lane = l;
        Time = t;
    }
/////////////////////GET FUNCTIONS//////////////////////////////////
    string getName()
    {
        return name;
    }

    int getLane()
    {
        return lane;
    }

    double getTime()
    {
        return Time;
    }
////////////////////SET FUNCTIONS////////////////////////////////////

    void setName(string n)
    {
        name = n;
    }

    void setLane(int l)
    {
        lane = l;
    }

    void setTime(double t)
    {
        Time = t;
    }
//////////////////////PRINT FUNCTION///////////////////////////////////

    void printHorse()
    {
        cout << "Horse " << name << " is in lane " << lane;
        cout << " with a time of " << Time << "." << endl;
        cout << "--------------------------------------------------------------\n";
    }
    };
//////////////////////BEST TIME FUNCTION///////////////////////////////////

    double leBest(Horse horse[])
    {
        double best = 50.0;
    for(int x=0;x<10;x++)
    {
        if(best >= horse[x].getTime())
           best = horse[x].getTime();
    }
    return best;
    }
//////////////////WORST TIME FUNCTION/////////////////////////////////////

    double leWorst(Horse horse[])
    {
    double worst = 40;
    for(int x=0;x<10;x++)
    {
        if(worst <= horse[x].getTime())
            worst = horse[x].getTime();
    }
    return worst;
    }
///////////////////AVERAGE FUNCTION////////////////////////////////////

    double leAverage(Horse horse[])
    {
        double ave = 0;
        for(int x=0; x<10; x++)
        {
            ave += horse[x].getTime();
        }
        ave /= 10;
        return ave;
    }
///////////////////MAIN FUNCTION////////////////////////////////////

    int main()
    {

    Horse horse[10];

    horse[0] = Horse("American", 1, 45.41);
     horse[1] = Horse("Bababooey", 2, 42.42);
      horse[2] = Horse("Charlie", 3, 40.94);
       horse[3] = Horse("Dog", 4, 43.55);
        horse[4] = Horse("Echo", 5, 41.41);
         horse[5] = Horse("Firefox", 6, 42.58);
          horse[6] = Horse("Google", 7, 41.98);
           horse[7] = Horse("Hoof", 8, 44.57);
            horse[8] = Horse("Ima", 9, 41.57);
             horse[9] = Horse("Just", 10, 45.88);

    double aveTime = leAverage(horse);
    double bestTime = leBest(horse);
    double worstTime = leWorst(horse);

    cout << "Welcome to the Horse Track! Place your bets and take your seats!\n" << endl;
    system("PAUSE");
    cout << endl;

     for(int x=0;x<10;x++)
    {
     horse[x].printHorse();
    }

    cout << "Average Time = " << aveTime << endl;
    cout << "Best Time = " << bestTime << endl;
    cout << "Worst Time = " << worstTime << endl;



    cin.get();
    return EXIT_SUCCESS;
    }

I really hate doing this, but I'm going to be selfish and bump this thread in an effort to get a response. I promise to not do it again.
And what exactly is your problem?
ps whats up with your indentation on 117-126
Last edited on
I didn't mean for the indentation on 117-126, it did that on its own, and I honestly just kept it because it looked cool. I'm having a hard time understanding the requirements and how to meet them, though. Could you explain them to me in simpler terms, since I'm lacking when it comes to intelligence? Any help is appreciated. Thanks
Now that I read through it again and try to understand it, I come across something that I don't think I know how to do. How do you create the barn class with 20 empty stalls, and then fill it with horses and redirect horses into empty stalls once they are put in?

The most recent Email on the subject that I've received from my instructor on this is:

"No you don't have to do any sorting. What you have to do is to have a loop through the 20 stalls. You will also have a variable (integer) that will keep track or the next empty stall. When you find an empty stall, you will have to search for the next stall with a horse and then move that horse to the empty stall. Then you have to make the stall that had the horse empty (put " " as the name, 0 as the lane, and 0.0 as the Time). It is a little tricky as you need two variables, one the loop variable and one to keep track of where the next empty stall is. Give the pseudocode a try. I do allow questions on the final project so you can show me your pseudocode for me to comment on. I will also comment on the code if you have trouble. Compacting the barn is the hardest part and you will only lose 5 points if you can't make it work. The compacting function is a question that was on an past Computer Science AP exam. It does not take a lot of code, just a lot of logic."

I am starting to have even more trouble comprehending the program now. If I send in a pseudocode with the parts that I don't know how to do missing, will she be offended, or do you think she would help?
Last edited on
This will get you started. See if you can map this skeleton to the assignment you posted to see how the requirements translate to code. Put this after your Horse class declaration. Ideally, you would have split these classes into header and implementation files. In fact, not doing this is bad practice. However, just to be consistent with what you have to far, drop this in after your Horse class (which ends on line 73).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class HorseBarn
{
private:
    Horse stalls[20];
public:
    HorseBarn()
    {
        //loop over the stalls array and fill it with empty horses
    }

    void fillStall(Horse h, int stallNumber)
    {
        //assign h to stalls[stallNumber]
    }

    void printBarn()
    {
        //loop over the stalls and print something for each
        //depending on whether or not there is a horse in it
    }

    std::string findStall(int stallNumber)
    {
        //if stalls[stallNumber] has a horse in it
        //    return the horse's name
        //else
        return "";
    }

    void compactBarn()
    {
        //loop over stalls until you come across an empty one
        //remember the position of the empty one and seek forward
        //    for a horse in a later stall
        //if you find another horse, swap it with the first empty one
    }
};
Is this any better? I put my questions in as notes, and deleted the notes for the parts that I think I got right.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
///////////////////////HORSE BARN CLASS/////////////////////////////////////////

  class HorseBarn
{
private:
    Horse stalls[20];
public:
    HorseBarn()
    {
       for(int x=0;x<20;x++)
            stalls[x] = ("", 0, 0.0);
    }

    void fillStall(Horse h, int stallNumber)
    {
        //assign h to stalls[stallNumber]
        /* What do you mean by this? 
        Sorry for being ridiculously dumb when it comes to this.*/
    }

    void printBarn()
    {
        for(int x=0;x<20;x++)
        {    //loop over the stalls and print something for each
          if(stalls[x] != ("", 0, 0.0))/*Did I do this right?*/
            cout << "Horse" << name << "is in this stall.\n";
            else
            cout << "This stall is empty.\n";
        }

    std::string findStall(int stallNumber)//What does std::string do?
    {
    if (stalls[stallNumber] = has a horse in it)
    return name;
    else
        return "";/*I also feel like I did this incorrectly as well.*/
    }

    void compactBarn()
    {
        for(int x=0;x<20;x++)
        if( stall[x] != ("", 0, 0.0))/*If I did the other part wrong,
 I did this part wrong, too.*/
            {
            int Swap = stall[x];
            /*How do I continue on to find another full stall
 and save it's information as well?*/
            }

    }
Bump... I'm so sorry >_<
This doesn't compile. Your printBarn() function is missing a closing brace (line 30) and class declarations must end with a semicolon. (So, new line at the bottom with just }; on it)

This syntax stall[x] != ("", 0, 0.0) will not work, you need to compare the fields directly. Or better yet, make an isEmpty() function in your Horse class.
1
2
3
4
5
6
//This would go with the other functions inside of Horse
    bool isEmpty()
    {
        //the std::abs is there because I never do '==' on floating point
        return (name == "" && lane == 0 && std::abs(Time) <= 0.0001);
    }

Then, instead of doing stall[x] != ("", 0, 0.0) you could say !stall[x].isEmpty()

Also, you cannot just say 'name' inside of class HorseBarn. HorseBarn doesn't have a name, it has horses. You have to refer to the horses themselves when trying to get the name. Here's a complete printBarn() function that shows you how to use the isEmpty() method I showed above, and how to access the Horse's data.
1
2
3
4
5
6
7
8
9
10
void printBarn()
    {
        for(int x=0;x<20;x++)
        {    //loop over the stalls and print something for each
            if(!stalls[x].isEmpty())
                cout << "Stall # " << x << ": " << stalls[x].getName() << "is in this stall.\n";
            else
                cout << "This stall is empty.\n";
        }
    }
Last edited on
Topic archived. No new replies allowed.