Array-base List/ Menu driven

Hi,
I have been working on a very huge assignment, and I am kinda lost. I need help writing the definitions of some functions. I know what I want, but I don't know how to do it.

Thank you!
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
//This is the header file

#include "arrayListType.h"
#ifndef FRUITARRAYTYPE_H
#define FRUITARRAYTYPE_H
#include<iostream>
using namespace std;

class fruitArrayType: public arrayListType<Fruit>
{
private:
    string kind;
    int location;
    double quan;
    double newUp;

public:
    void print1(string kind);
    void print2();
    void print3(int location);
    void updateL(int location, double quan);
    void updateUp(string kind, newUp);
    double getTotalCost();

};
#endif // FRUITARRAYTYPE_H

// This is the .cpp file. This is what I am having trouble with.
// Any help is appreciated.

#include "fruitArrayType.h"
#include<iostream>
using namespace std;

//Print the number, the name and unit price of fruit, the quantity and cost of each item in the fruit list.
void fruitArrayType::print1(string kind)
{
    cout <<name<<"\t\t$"
        <<price<<"\t\t "
        <<quantity<<" lb\t\t "
        <<price * ob.quantity <<"\n";

}
// Print the name and unit price of fruit, the quantity and cost of a given item. Ex: print(3)- print out the third item in the list.
void fruitArrayType::print2()
{

}

//Print the name and unit prices of fruit, the quantities and cost of the items of a given kind of fruit. Ex: print ("Apple") -- print out all apple items.
void fruitArrayType::print3(int location)
{

}

//Update the quantity of a given item
void fruitArrayType::updateL(int location, double quan)
{


}

//Update the unit price of a given fruit
void updateUp(string kind, newUp)
{

}
//Get the total cost.
double getTotalCost();
{

}
You got me a little confused... Can you post what's in arrayListType.h? Also the definition of struct Fruit would be helpful. And did you finish fruitArrayType::print1()? The code inside doesn't make any sense to me...
Sure; let me add the arrayListType.h. It's a pretty long file

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#ifndef ARRAYLISTTYPE_H
#define ARRAYLISTTYPE_H

#include <iostream>
#include <cassert>

using namespace std;

template <class elemType>
class arrayListType
{
public:
    const arrayListType<elemType>& operator=(const arrayListType<elemType>&);               //Overloads the assignment operator
    bool isEmpty();
    bool isFull();
    int listSize();
    int maxListSize();
    void print() const;
    bool isItemAtEqual(int location, const elemType& item);
    void insertAt(int location, const elemType& insertItem);
    void insertEnd(const elemType& insertItem);
    void removeAt(int location);
    void retrieveAt(int location, elemType& retItem);
    void replaceAt(int location, const elemType& repItem);
    void clearList();
    int seqSearch(const elemType& item);
    void insert(const elemType& insertItem);
    void remove(const elemType& removeItem);
    arrayListType(int size = 100);
    arrayListType(const arrayListType<elemType>& otherList);
    ~arrayListType();

protected:
    elemType *list; 	//array to hold the list elements
    int length;		//to store the length of the list
    int maxSize;		//to store the maximum size of the list
};



template <class elemType>
bool arrayListType<elemType>::isEmpty()
{
	return (length == 0);
}

template <class elemType>
bool arrayListType<elemType>::isFull()
{
	return (length == maxSize);
}

template <class elemType>
int arrayListType<elemType>::listSize()
{
	return length;
}

template <class elemType>
int arrayListType<elemType>::maxListSize()
{
	return maxSize;
}

template <class elemType>
void arrayListType<elemType>::print() const
{
	for(int i = 0; i < length; i++)
		cout<<list[i]<<" ";
	cout<<endl;
}

template <class elemType>
bool arrayListType<elemType>::isItemAtEqual
                   (int location, const elemType& item)
{
	return(list[location] == item);
}

template <class elemType>
void arrayListType<elemType>::insertAt
                   (int location, const elemType& insertItem)
{
	if(location < 0 || location >= maxSize)
		cerr<<"The position of the item to be inserted "
			<<"is out of range."<<endl;
	else
		if(length >= maxSize)  //list is full
			cerr<<"Cannot insert in a full list."<<endl;
		else
		{
			for(int i = length; i > location; i--)
				list[i] = list[i - 1];	//move the elements down

			list[location] = insertItem;	//insert the item at the
 										//specified position

			length++;	//increment the length
		}
} //end insertAt

template <class elemType>
void arrayListType<elemType>::insertEnd(const elemType& insertItem)
{
	if(length >= maxSize)  //the list is full
		cerr<<"Cannot insert in a full list."<<endl;
	else
	{
		list[length] = insertItem;	//insert the item at the end
		length++;	//increment length
	}
} //end insertEnd

template <class elemType>
void arrayListType<elemType>::removeAt(int location)
{
	if(location < 0 || location >= length)
    	cerr<<"The location of the item to be removed "
			<<"is out of range."<<endl;
	else
	{
   		for(int i = location; i < length - 1; i++)
	 		list[i] = list[i+1];

		length--;
	}
} //end removeAt

template <class elemType>
void arrayListType<elemType>::retrieveAt
                     (int location, elemType& retItem)
{
	if(location < 0 || location >= length)
    	cerr<<"The location of the item to be retrieved is "
			<<"out of range."<<endl;
	else
		retItem = list[location];
} // retrieveAt

template <class elemType>
void arrayListType<elemType>::replaceAt
                    (int location, const elemType& repItem)
{
	if(location < 0 || location >= length)
    	cerr<<"The location of the item to be replaced is "
			<<"out of range."<<endl;
	else
		list[location] = repItem;

} //end replaceAt

template <class elemType>
void arrayListType<elemType>::clearList()
{
	length = 0;
} // end clearList

template <class elemType>
int arrayListType<elemType>::seqSearch(const elemType& item)
{
	int loc;
	bool found = false;

	for(loc = 0; loc < length; loc++)
	   if(list[loc] == item)
	   {
		found = true;
		break;
	   }

	if(found)
		return loc;
	else
		return -1;
} //end seqSearch

template <class elemType>
void arrayListType<elemType>::insert(const elemType& insertItem)
{
	int loc;

	if(length == 0)					 //list is empty
		list[length++] = insertItem; //insert the item and
 									 //increment the length
	else
		if(length == maxSize)
			cout<<"Cannot insert in a full list."<<endl;
		else
		{
			loc = seqSearch(insertItem);

			if(loc == -1)   //the item to be inserted
							//does not exist in the list
				list[length++] = insertItem;
			else
				cerr<<"the item to be inserted is already in "
 					<<"the list. No duplicates are allowed."<<endl;
	}
} //end insert

template <class elemType>
void arrayListType<elemType>::remove(const elemType& removeItem)
{
	int loc;

	if(length == 0)
		cerr<<"Cannot delete from an empty list."<<endl;
	else
	{
		loc = seqSearch(removeItem);

		if(loc != -1)
			removeAt(loc);
		else
			cout<<"The tem to be deleted is not in the list."
				<<endl;
	}

} //end remove


template <class elemType>
arrayListType<elemType>::arrayListType(int size)
{
	if(size < 0)
	{
		cerr<<"The array size must be positive. Creating "
 			<<"an array of size 100. "<<endl;

 	   maxSize = 100;
 	}
 	else
 	   maxSize = size;

	length = 0;

	list = new elemType[maxSize];
	assert(list != NULL);
}

template <class elemType>
arrayListType<elemType>::~arrayListType()
{
	delete [] list;
}

	//copy constructor
template <class elemType>
arrayListType<elemType>::arrayListType
                   (const arrayListType<elemType>& otherList)
{
   maxSize = otherList.maxSize;
   length = otherList.length;
   list = new elemType[maxSize]; 	//create the array
   assert(list != NULL);	//terminate if unable to allocate
 							//memory space

   for(int j = 0; j < length; j++)  //copy otherList
 	   list [j] = otherList.list[j];
}//end copy constructor


template <class elemType>
const arrayListType<elemType>& arrayListType<elemType>::operator=
			(const arrayListType<elemType>& otherList)
{
	if(this != &otherList)	//avoid self-assignment
	{
	   delete [] list;
	   maxSize = otherList.maxSize;
       length = otherList.length;

       list = new elemType[maxSize];
	   assert(list != NULL);

       for(int i = 0; i < length; i++)
	   	    list[i] = otherList.list[i];
	}

    return *this;
}


#endif

The Fruit is actually a class def. Let me include that too.
As for the fruitArrayType::print1(), I didn't know what to do with that, but the function should print the number, the name and unit price of fruit, the quantity and cost of each item in the fruit list.

Thanks for the help man.
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
//fruit.h
#ifndef FRUIT_H
#define FRUIT_H
#include<iostream>
#include<string>
using namespace std;

class Fruit                                         //Class Declaration
{
public:
    string name;                                     //Class variables
    double price;
    double quantity;

    void setAll(string n, double p, double q);          //Class functions
    void setN(string n);
    void SetP(double p);
    void setQ(double q);

    string getN(){return name;}
    double getP(){return price;}
    double getQ(){return quantity;}
    double getC(){ return price * quantity;}

    Fruit(string = " ", double = 0.0, double = 0.0);        //Constructor with default parameters
    friend ostream& operator<<(ostream & os ,const Fruit  & ob);
};

#endif // FRUIT_H

//fruit.cpp

#include "Fruit.h"
#include<iostream>
using namespace std;

void Fruit::setAll(string n, double p, double q)
{
    name = n;
    price = p;
    quantity = q;
}

void Fruit::setN(string n)
{
    name = n;
}
void Fruit::SetP(double p)
{
    price = p;
}
void Fruit::setQ(double q)
{
    quantity = q;
}

Fruit::Fruit(string n, double p, double q)                   //Constructor with default parameters
{
    setAll(n, p, q);
}

ostream& operator<<(ostream & os , const Fruit  & ob)           //Overloading (<<) definition
{
    os <<ob.name<<"\t\t$"
       <<ob.price<<"\t\t "
       <<ob.quantity<<" lb\t\t "
       << ob.price * ob.quantity <<"\n";
    return os;
}
I think you got the functions and what they are supposed to do mixed up. If I'm correct, the prototypes should be like this:
1
2
3
void print1();
void print2(int location);
void print3(string kind);

So, they should be like 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
void fruitArrayType::print2(int location) {
	Fruit target;
	retrieveAt(location, target);
	cout << "Name:\t" << target.getN() << "\nUnit Price:\t" << target.getP()
		<< "\nQuantity:\t" << target.getQ() << "lbs" << "\nTotal Price:\t" << target.getC() << endl;
}

void fruitArrayType::print1() {
	for(int location = 0; location < length; location ++) {
		cout << "Fruit #" << location + 1 << ":" << endl;
		print2(location);
	}
}

void fruitArrayType::print3(string kind) {
	int number = 1;
	for(int location = 0; location < length; location ++) {
		Fruit target;
		retrieveAt(location, target);
		if(target.name == kind) {
			cout << kind << " #" << number ++ << ":\n" << endl;
			print2(location);
		}
	}
}

void fruitArrayType::updateL(int location, double quan) {
	Fruit target;
	retrieveAt(location, target);
	target.quantity = quan;
	replaceAt(location, target);
}

void fruitArrayType::updateUP(string kind, double newUP) {
	for(int location = 0; location < length; location ++) {
		Fruit target;
		retrieveAt(location, target);
		if(target.name == kind) {
			target.price = newUP;
			replaceAt(location, target);
		}
	}
}

double fruitArrayType::getTotalCost() {
	double totalCost;
	for(int location = 0; location < length; location ++) {
		Fruit target;
		retrieveAt(location, target);
		totalCost += target.getC();
	}
	return totalCost;
}

Hope this helps.
Thanks mate!
Very helpful. Can you please take a look at my main file.

This is what needs to be done with it:
Create an ob fruitArrayType as a fruit list.
Ask the user to select a kind of fruit to buy and insert the newitem to the list by using the insertEnd()
Show all item in the shopping list
Change the quantity of the selected item by inputting the number of the item and the new quantity.
Change the unit price of a given fruit and show the receipt again.

Thank again.

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
#include"Fruit.h"
#include"arrayListType.h"
#include"fruitArrayType.h"
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;


//Function prototypes
void showMenu(Fruit MenuList[],int s);

int main()
{
    const int SIZE = 8;
    int choice;
    double Quan;
    char changeQty;
    int newqty;

    Fruit newItem;
    fruitArrayType ShoppingList;
    Fruit MenuList[SIZE]= {Fruit("Apple", 0.99), Fruit("Apricot", 0.45), Fruit("Avocado", 1.50), Fruit("Banana", 0.75),
                Fruit("Blueberry", 1.00), Fruit("Grape", 1.25), Fruit("Orange", 1.50), Fruit("Pear      ", 1.30)};

do
{
    showMenu(MenuList,SIZE);
    cout << "\nPlease enter the number of your selection(1--9). \n";
    cin >> choice;
    if(choice ==9){break;}
        if(choice < 1 || choice > 9)
		{
        cout << "\a\a Invalid Input \n";
        cout << "\nPlease enter the number of your selection(1--9). \n";
        cin >> choice;
		}
    newItem = MenuList[choice -1];

    cout << "\nPlease enter the quantity of " << MenuList[choice-1].getN() <<":\n";
    cin >> Quan;
        if(Quan < 0)
            {
            cout <<"\a\a Invalid quantity. \n";
            cout << "\nPlease enter the quantity of " << MenuList[choice-1].getN() <<":\n";
            cin >> Quan;
            }
    newItem.setQ(Quan);
    ShoppingList.insertEnd(newItem);

    while (choice != 9 && Quan >= 0)
    {
        ShoppingList.print();
        cout << "\nDo you want to change the quantity of the item? (Y/N). \n";
        cin>> changeQty;

        if (changeQty == 'Y')
        {
            cout << "Which item number? \n";
            cin>> newqty;

            ShoppingList.updateL(num, newqty);
            ShoppingList.print(num);
        }
        else
            break;

    }

}while (choice != 9);


    return 0;
}

void showMenu(Fruit MenuList[], int s)                                  //Show menu function definition
{
    cout << "Welcome to Cooper's Fresh Fruit Store! \n\n";
    cout << "   Items " << setw(25) << "Unit Price \n";
    for(int count = 0; count < s; count++)
    {
        cout << "#" << count+1 <<" "
             << MenuList[count].name << "\t\t$"
             << MenuList[count].price << "/lb\n";
    }
        cout <<"#9 Exit \n";

}

/*
void TotalCost(Fruit ShoppingList[], int counter)                             //Total Cost function def.
{
    double FinalTotal;

    for(int count = 0; count < counter; count++)
    {
        FinalTotal+= ShoppingList[count].getC();
    }
    cout << "****************************************************************** \n";
    cout << setw(25) << left << "Total Cost" <<fixed << showpoint <<setprecision(2)<<"$"<<FinalTotal << "\n";
}
*/
I don't see any obvious errors, except this one:
1
2
ShoppingList.print();//line 53
ShoppingList.print(num);//line 63 

There is only FruitArrayList::print1(), FruitArrayList::print2() and FruitArrayList.print3().
I get what you are trying to do. But if you want to overload the FruitArrayList::print(), you have to change your function prototypes to this:
1
2
3
void print();
void print(int location);
void print(string kind);


Other than that there are no errors, but there are some things you can improve. If you are checking the conditions inside the while loops and using break;, your loops should not be while() {} and do {} while();. It should be while(true) because you are breaking out manually inside the loop.

Change the unit price of a given fruit and show the receipt again.

Are you having trouble with it?
Last edited on
Topic archived. No new replies allowed.