C++ trouble with a sorted list program?

Implement a class SortedList as defined by the following skeleton:

#define MAX_ITEMS 10
typedef float ItemType;

class SortedList
{
private:
int length;
ItemType values[MAX_ITEMS];
int currentPos;
public:
SortedList( ); // default constructor: lenght=0, currentPos=-1
void MakeEmpty; // let length=0
void InsertItem(ItemType x); // insert x into the list
void DeleteItem(ItemType x); // delete x from the list
bool IsFull( ); // test if the list is full
int Lengthls( ); // return length
void RetrieveItem(ItemType &x, bool &found); // retrieve x from the list, the
// boolean result is stored in found
void ResetList( ); // currentPos=-1
void GetNextItem(ItemType &x); // get the next element from the list with
// respect to the currentPos
};
Task 1: Create one instance of this class. You also need to read in data from one data file: float.txt, which contains the following numbers:
5.5
6.2
7.1
8.0
9.0
10.0
1.0
2.0
3.3
4.4

Data in float.txt contains floating numbers, which should be inserted into the object of SortedList. Note that you do not have any prior knowledge about data values in float.dat, but we assume that there are 10 elements in the data file.

Task 2: Use GetNextItem( ) to print out all the elements in the list in sorted sequence on computer screen.

My code:
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
#include <iostream>
#include <fstream>
using namespace std;
#define MAX_ITEMS  10
typedef float ItemType;
class SortedList {
  private:
    int length;
    ItemType values[MAX_ITEMS];
    int currentPos;
  public:
     SortedList() {
	length = 0;
	currentPos = -1;
    } void MakeEmpty() {
	length = 0;
    }
    void InsertItem(ItemType x) {
	bool moreToSearch;
	int location = 0;
	int midpoint;
	int first = 0;
	int last = length - 1;
	midpoint = (first + last) / 2;

	moreToSearch = (first <= last);
	while (moreToSearch) {
	    switch (x.comparedTo(values[location])) {
	    case LESS:		//search in 1st half
		moreToSearch = (first <= last);
		break;
	    case GREATER:
		location++;
		moreToSearch = (location < length);
		break;
	    }
	}
	for (int index = length; length > location; index--) {
	    values[index] = values[index - 1];
	}
	values[location] = x;
	length++;

    }

    void DeleteItem(ItemType x) {
	int location = 0;
	while (x.ComparedTo(values[location]) != EQUAL)
	    location++;
	for (int index = location + 1; index < length; index++)
	    values[index - 1] = values[index];
	length--;
    }

    void RetrieveItem(ItemType & x, bool & found) {
	int midpoint;
	int first = 0, last = length - 1;
	bool moreToSearch = (first <= last);
	found = false;
	while (moreToSearch && !found) {
	    midpoint = (first + last) / 2 switch (x.comparedTo(values[index])) {
	    case LESS:		//search in 1st half
		moreToSearch = (first <= last);
		last = midpoint - 1;
		break;
	    case GREATER:	//Search in 2nd half
		first = midpoint + 1;
		moreToSearch = (first <= last);
		break;
	    case EQUAL:	//x has been found
		found = true;
		break;
	    }
	}
    }
    int LengthIs() {
	return length;
    }
    void ResetList() {
	currentPos = -1;
    }
    bool IsFull() {
	if (length < 9)
	    return false;
	else
	    return true;
    }
    void GetNextItem(ItemType & x) {
	currentPos++;
	x = values[currentPos];
	cout << x;
    }

};

int main()
{
    ifstream indata;
    int i = 0;
    int size = 0;
    indata.open("float.txt");

    float values[10];
    while (!indata.eof())	// write or read data from indatac into values
    {
	indata >> values[i];
	i++;
	size++;			// this will count how many values there are in the array
    }
    SortedList a;
    a.GetNextItem(x);




    indata.close();

    return 0;
}


When I try running my program, I get a bunch of compiler errors

1>c:\users\adam\documents\visual studio 2008\projects\cis200proj2\cis200proj2\ci… : error C2228: left of '.comparedTo' must have class/struct/union
1> type is 'ItemType'
1>c:\users\adam\documents\visual studio 2008\projects\cis200proj2\cis200proj2\ci… : error C2065: 'LESS' : undeclared identifier
1>c:\users\adam\documents\visual studio 2008\projects\cis200proj2\cis200proj2\ci… : error C2051: case expression not constant
1>c:\users\adam\documents\visual studio 2008\projects\cis200proj2\cis200proj2\ci… : error C2065: 'GREATER' : undeclared identifier
1>c:\users\adam\documents\visual studio 2008\projects\cis200proj2\cis200proj2\ci… : error C2051: case expression not constant
1>c:\users\adam\documents\visual studio 2008\projects\cis200proj2\cis200proj2\ci… : warning C4060: switch statement contains no 'case' or 'default' labels
1>c:\users\adam\documents\visual studio 2008\projects\cis200proj2\cis200proj2\ci… : error C2228: left of '.ComparedTo' must have class/struct/union
1> type is 'ItemType'
1>c:\users\adam\documents&#9

Can anyone help me fix this program?


Thanks for the post.
Hi guys, Im a newbie. Nice to join this forum.




__________________
watch free movies online
Last edited on
Can't you just use a map<float,whateverYouWant> ?
Items will be sorted automatically at insert time, and you can use find() do do something similar as your RetrieveItem method (and erase for DeleteItem)
x.comparedTo(values[location]) What is comparedTo? the basic types are not objects, but why you suppose it would have this method. Maybe you want
1
2
enum compareValue{LESS, GREATER, EQUAL};
compareValue comparedTo(const ItemType &a, const ItemType &b)
Last edited on
Topic archived. No new replies allowed.