Bus Error

I keep getting a bus error when I run the program with its client (robot.cpp). Something to do with curptr -> info in FillBuckets, I think, or possibly the typedef Pebble. Also, I am having trouble coding TrashBuckets.

Please note that the only file I am allowed to edit is buckets.cpp.

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
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
class Buckets;
#include "buckets.h"
#include <cstdlib>
#include <iostream>
using namespace std;

typedef Pebble ItemType;

struct NodeType
{
	Pebble info;
	NodeType* next;
};

struct BuckStuff
{
	NodeType* firstptr;
	int num;
};

//@gets a number of buckets for the robot
//@pre number is assigned the desired number of buckets
//@post number buckets are placed in front of the robot
//@param number is the number of buckets desired
//@usage  b.GetBuckets(5);
void Buckets::SetUpBuckets(int number)
{
	buckets = new(nothrow) BuckStuff;
	buckets -> num = number;
	buckets -> firstptr = NULL;
	if (number > 0)
	{
		buckets -> firstptr = new(nothrow) NodeType;
		NodeType* curptr;
		curptr = buckets -> firstptr;
		curptr -> next = NULL;
		for (int i; i < number - 1; i++)
		{
			curptr -> next = new(nothrow) NodeType;
			curptr = curptr -> next;
			curptr -> next = NULL;
		}
	}
}

//@fills all of the buckets with pebbles
//@pre buckets are placed in front of the robot
//@post one colored pebble is placed in each bucket
//@     according to the user's input
//@usage   b.FillBuckets();
void Buckets::FillBuckets()
{
	char color;
	NodeType* curptr;

	curptr = buckets -> firstptr;
	cout << "Enter R for red, W for white, B for Blue. " << endl;
	for(int i = 0; i < buckets -> num; i++)
	{
		cout << "Enter color for bucket " << i + 1 << " -> ";
		cin >> color;
		if(color == 'R' || color == 'r')
			curptr -> info = RED;
		else if(color == 'W' || color == 'w')
			curptr -> info = WHITE;
		else
			curptr -> info = BLUE;
		curptr = curptr -> next;
		cout << curptr -> info;
	}
}

//@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
//@param i
//@return the color of the pebble
//@usage  pebble = b.GetColor(2);
Pebble Buckets::GetColor(int i)
{
	NodeType* curptr = buckets -> firstptr;
	for(int k=0; k< i; k++)
		curptr = curptr -> next;
	return curptr -> info;
}

//@robot swaps the pebbles in two buckets with its arms
//@pre  i and j are the positions of the two buckets
//@     buckets are given positions 0, 1, 2, 3,...
//@post the pebbles in buckets i and j are swapped
//@param i
//@param j
//@usage    b.Swap(i,j);
void Buckets::Swap (int i, int j)
{
	int temp;
	temp = i;
	i = j;
	j = temp;
}

//@all of the buckets are thrown in the trash
//@pre  there are buckets placed before the robot
//@post there are no buckets placed before the robot
//@usage   b.TrashBuckets();
/*void Buckets::TrashBuckets()
{
   NodeType* delptr;

   while (buckets -> firstptr != NULL)
   {
      delptr = buckets;
      buckets = buckets -> firstptr;
      delptr -> next = NULL;
      delete delptr;
   }
}*/

//@counts the number of buckets placed before the robot
//@pre   there are buckets
//@post  returns the number of buckets
//@usage   num = b.GetNum();
int Buckets::GetNumber()
{
	return buckets -> num;
}


buckets.h - NOTE - CANNOT EDIT THIS.
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
enum Pebble {RED, WHITE, BLUE};
struct BuckStuff;

class Buckets
{
public:
   //@gets a number of buckets for the robot
   //@usage  b.GetBuckets(5);
   void SetUpBuckets(int number);
   
   //@fills all of the buckets with pebbles
   //@usage   b.FillBuckets();
   void FillBuckets();
   
   //@robot uses its eye to view the color of the pebble
   //@usage  pebble = b.GetColor(2);
   Pebble GetColor(int i);
   
   //@robot swaps the pebbles in two buckets with its arms
   //@usage    b.Swap(i,j);
   void Swap (int i, int j);
   
   //@all of the buckets are thrown in the trash
   //@usage   b.TrashBuckets();
   //void TrashBuckets();
   
   //@counts the number of buckets placed before the robot
   //@usage   num = b.GetNum();
   int GetNumber();
private:
   BuckStuff* buckets;
};


robot.cpp - NOTE - CANNOT EDIT THIS
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 <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.SetUpBuckets(number);
      printNewLines(2);
      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;
}


Any help would be appreciated, thanks!
Last edited on
In void Buckets::SetUpBuckets(int number), you have construct for (int i; i < number - 1; i++). i is uninitialised.
Topic archived. No new replies allowed.