buckets ADT

So i have to code a program that creates a series of "buckets" that holds the color pebble red, blue, or white. I did some poking around on here and found a similar program that someone else did but I want to understand what I'm doing wrong for mine first.
first there is the buckets.cpp
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
#include "buckets.h"
#include <cstdlib>
#include <iostream>
using namespace std;

struct BuckStuff;
{
    int numBuckets;
   Pebble* rowOfBuckets;  
};
   //@gets a default number of buckets for the robot
   //@     and filled with randomly assigned pebbles
   Buckets()
   {
   }
   
   //@gets a number of buckets for the robot
   void Buckets::Buckets(int number)
   {
      buckets = new BuckStuff;
      buckets -> numBuckets = 11;
      buckets -> rowOfBuckets = new Pebble[11];
      for (int k = 0; k < 11, k++)
      {
         int num = rand() % 3;
         Pebble color;
         if ( num == 0)
            color = RED;
         if ( num == 1)
            color = WHITE;
         if ( num == 2)
            color = BLUE;
      }
   }
   
   //@all of the buckets are thrown in the trash
   void Buckets::~Buckets()
   {
      delete [] buckets -> rowOfBuckets;
      buckets -> rowOfBuckets = NULL;
      delete buckets;
      buckets = NULL;
   }
   
   //@fills all of the buckets with pebbles
   void Buckets::FillBuckets()
   {
      char colorChar;
      Pebble color;
      for (int k = 0; k< buckets -> numBuckets; k++)
      {
         cout << "enter color (r, w, b) for pebble in bucket" << k << "->";
         cin >> colorChar;
         if (colorChar =='r')
            color = RED;
         else if(colorChar =='w')
            color = WHITE;
         else
            color = BLUE;
         buckets -> rowOfBuckets[k] = color;
      }

   }  
   //@robot uses its eye to view the color of the pebble
   Pebble Buckets:: GetColor(int bucketNumber) const
   {
      return buckets -> rowOfBuckets[bucketNumber];
   }  
   //@robot swaps the pebbles in two buckets with its arms
   void Buckets::Swap (int bucketI, int bucketJ)
   {
      Pebble color;
      color = buckets -> rowOfBUckets[bucketI];
      buckets -> rowOFBuckets[bucketI] = *[bucketJ];
      *[bucketJ] = color
   }     
   //@counts the number of buckets placed before the robot
   int Buckets::GetNumber() const
   {
      return buckets -> numBuckets;
   }
}


then buckets.h

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
#ifndef BUCKETS_H
#define BUCKETS_H

enum Pebble {RED, WHITE, BLUE};
struct BuckStuff;

class Buckets
{
public:
   //@gets a default number of buckets for the robot
   Buckets();
   
   //@gets a number of buckets for the robot
   Buckets(int number);
   
   //@all of the buckets are thrown in the trash
   ~Buckets();
   
   //@fills all of the buckets with pebbles
   void FillBuckets();
   
   //@robot uses its eye to view the color of the pebble
   //@pre i is the position of the bucket
   //@    positions of the buckets are labeled 0, 1, 2, ....
   //@post the color of the pebble is returned
   Pebble GetColor(int bucketNumber) const;
   
   //@robot swaps the pebbles in two buckets with its arms
   //@pre  bucketI and bucketJ are the positions of the two buckets
   //@     buckets are given positions 0, 1, 2, 3,...
   //@post the pebbles in buckets bucketI and bucketJ are swapped
   void Swap (int bucketI, int bucketJ);
      
   //@counts the number of buckets placed before the robot
   //@pre   there are buckets
   //@post  returns the number of buckets
   int GetNumber() const;
private:
   BuckStuff* buckets;
};
#endif 


and lastly bucketstester.cpp

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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "buckets.h"
#include <ctime>
#include <iostream>
using namespace std;

//@gets the number of buckets from the user
//@usage: num = getNumberOfBuckets();
int getNumberOfBuckets();

//@gets the answer to continue or not from the user
//@usage userSays = getUsersAnswer();
char getUsersAnswer();

//@prints the contents of the buckets
//@usage print(buckets);
void print(Buckets buckets);

//@sorts the pebbles into red section, white section, blue section
//@usage sort(buckets);
void sort(Buckets& buckets);

//@prints a number of newlines
//@usage  printNewLines(4);
void printNewLines(int num);

//@ checks that a bucket label exists
//@usage if (isOkBucket(label))
bool isOkBucket(Buckets buckets, int label);

//@prints a message if a nonexisting bucket is accessed
//@usage printBadBucketMessage(label);
void printBadBucketMessage(int label);

int main()
{
   Buckets buckets;
   char userSays;
   int number;
   
   userSays = 'y';
   printNewLines(3);
   while (userSays == 'y')
   {
//      number = getNumberOfBuckets();
      Buckets.buckets();
      printNewLines(3);
      //buckets.FillBuckets();
      cout << "I got here!" << endl;
      printNewLines(3);
      print(buckets);
//      sort(buckets);
      printNewLines(3);
//      print(buckets);
      //buckets.TrashBuckets();
      printNewLines(2);
      userSays = getUsersAnswer();
      printNewLines(2);
   }
   return 0;
}

//@gets the number of buckets from the user
//@usage: num = getNumberOfBuckets();
int getNumberOfBuckets()
{
   int number;
   cout << "Enter the number of buckets -> ";
   cin >> number;
   return number;
}

//@gets the answer to continue or not from the user
//@usage userSays = getUsersAnswer();
char getUsersAnswer()
{
   cout << "Do you want to run again <y> or <n>? ";
   char answer;
   cin >> answer;
   return answer;
}

//@prints the contents of the buckets
//@usage print(buckets);
void print(Buckets buckets)
{
   for (int i = 0; i < buckets.GetNumber(); i++)
   {
      if (buckets.GetColor(i) == RED)
         cout << i << " : " << "Red" << endl;
      else if (buckets.GetColor(i) == WHITE)
         cout << i << " : " << "White" << endl;
      else
         cout << i << " : " << "Blue" << endl;
   }
}

//@sorts the pebbles into red section, white section, blue section
//@usage sort(buckets);
void sort(Buckets& buckets)
{
   int lastRed = -1;
   int lastWhite = -1;
   int firstMixed = 0;
   while (firstMixed < buckets.GetNumber())
   {
      if (isOkBucket(buckets, firstMixed))
      {
         if (buckets.GetColor(firstMixed) == BLUE)
         {
            firstMixed++;
         } else if (buckets.GetColor(firstMixed) == WHITE)
         {
            lastWhite++;
            if (isOkBucket(buckets, lastWhite))
               buckets.Swap(lastWhite, firstMixed);
            else
               printBadBucketMessage(lastWhite);
            firstMixed++;
         } else 
         {
            lastWhite++;
            if (isOkBucket(buckets, lastWhite))
               buckets.Swap(lastWhite, firstMixed);
            else
               printBadBucketMessage(lastWhite);
            lastRed++;
            if (isOkBucket(buckets, lastRed))
               buckets.Swap(lastRed, lastWhite);
            else
               printBadBucketMessage(lastRed);
            firstMixed++;
         }
      } else {
         printBadBucketMessage(firstMixed);
      }
   }
}

//@prints a number of newlines
//@usage  printNewLines(4);
void printNewLines(int num)
{
   for (int i = 0; i < num; i++)
   {
      cout << endl;
   }
}

//@ checks that a bucket label exists
//@usage if (isOkBucket(label))
bool isOkBucket(Buckets buckets, int label)
{
   if (label >= 0 && label < buckets.GetNumber())
   {
      return true;
   } else {
      return false;
   }
}

//@prints a message if a nonexisting bucket is accessed
//@usage printBadBucketMessage(label);
void printBadBucketMessage(int label)
{
   cout << "bucket with label " << label << "does NOT exist!" << endl;
}


I believe my main problem lies in the buckets.cpp but i'm new to c++ and our class moves swiftly so I don't have time to muddle through it. correct me if i'm wrong, but i've been compiling it in putty using g++ buckets.cpp bucketstester.cpp -o buckets. the biggest problem is that my errors tend to be things like "extra qualification on Buckets" and it not recognizing the RED, BLUE and WHITE from the buckets.h
thanks in advance!
Last edited on
Topic archived. No new replies allowed.