Array Problem

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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304

//Driver.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>

using namespace std;

#include "List.h"
 

int main() {

	ifstream inFile;
	ofstream outFile;
	lens temp;
	List list;
	//List list;
	int count = 0;
	int i = 0;
	int answer;
	int variable;
	int ans;
	string itemname;
	int data;
	lens data2;
	int itemnumber;


	inFile.open("S:\\mackay\\CS132 - 51\\CS132 Lab 3.txt");
	outFile.open("NewInventory.txt");

	while (answer = true) {

		while (!inFile.eof())
		{
			list.insert(temp, i);
		}

		cout << "What Would You Like to Do.\n 1. Display, 2. Erase, 3. Search, 4. Insert";
		cin >> answer;

		switch (answer) {
		case 1:
			list.display(cout);
			break;
		case 2:
			cout << "What Line Would You Like to Erase: ";
			cin >> variable;
			list.erase(variable);
			break;
		case 3:
			cout << "Would You Like to Search? \n 1. Item Name, 2. Item Number: ";
			cin >> ans;
			switch (ans) {
			case 1:
				cout << "Please Type Out the Item Name: ";
				cin >> itemname;
				list.search1(itemname);
				break;
			case 2:
				cout << "Please Type Out the Item Number: ";
				cin >> itemnumber;
				list.search1(itemnumber);
				break;
			}
			break;
		case 4:
			cout << "What Would You Like to Insert: ";
			cin >> data;
			list.insert(data2, data);
			break;
		}

		cout << "Do you want to continue?";
		cin >> answer;

		if (answer = 'Yes' || 'yes') {
			return true;
		}
		else {
			return false;
		}
	}
}

//List.h
#include <iostream>

#ifndef LIST
#define LIST

using namespace std;

struct lens 
{
	int number;
	int stock;
	float price;
	int min;
	string name;
};

const int CAPACITY = 1024;
typedef lens ElementType;


class List
{
 public:
 /******** Function Members ********/
   /***** Class constructor *****/
   List();
   /*----------------------------------------------------------------------
     Construct a List object.

     Precondition:  None
     Postcondition: An empty List object has been constructed; mySize is 0.
   -----------------------------------------------------------------------*/

   /***** empty operation *****/
   bool empty() const;
   /*----------------------------------------------------------------------
     Check if a list is empty.

     Precondition:  None
     Postcondition: true is returned if the list is empty, false if not.
   -----------------------------------------------------------------------*/

   /***** insert and erase *****/
   void insert(ElementType item, int pos);
   /*----------------------------------------------------------------------
     Insert a value into the list at a given position.

     Precondition:  item is the value to be inserted; there is room in
         the array (mySize < CAPACITY); and the position satisfies
         0 <= pos <= mySize. 
     Postcondition: item has been inserted into the list at the position
         determined by pos (provided there is room and pos is a legal
         position).
   -----------------------------------------------------------------------*/
  
   void erase(int number);
   /*----------------------------------------------------------------------
     Remove a value from the list at a given position.

     Precondition:  The list is not empty and the position satisfies
         0 <= pos < mySize.
     Postcondition: element at the position determined by pos has been
         removed (provided pos is a legal position).
   ----------------------------------------------------------------------*/

   /***** output *****/
   void display(ostream & out) const;
   /*----------------------------------------------------------------------
     Display a list.

     Precondition:  The ostream out is open. 
     Postcondition: The list represented by this List object has been
         inserted into out. 
   -----------------------------------------------------------------------*/

   void search1(string name);

   void search2(int number);

 private:
 /******** Data Members ********/
   int mySize;                     // current size of list stored in myArray
   ElementType myArray[CAPACITY];  // array to store list elements

}; //--- end of List class

//------ Prototype of output operator
ostream & operator<< (ostream & out, const List & aList);

#endif

//List.cpp
#include <cassert>
#include <string>
using namespace std;

#include "List.h"

//--- Definition of class constructor
List::List()                 
: mySize(0)
{}

//--- Definition of empty()
bool List::empty() const
{
   return mySize == 0;
}

//--- Definition of display()
void List::display(ostream & out) const
{
	for (int i = 0; i < mySize; i++)
		out << myArray[i].number << "  " << myArray[i].stock << "  " << myArray[i].price << "  " << myArray[i].min << "  " << myArray[i].name << endl;
}

//--- Definition of output operator
ostream & operator<< (ostream & out, const List & aList)
{
   aList.display(out);
   return out;
}

//--- Definition of insert()
void List::insert(ElementType item, int pos)
{
   if (mySize == CAPACITY)
   {
      cerr << "*** No space for list element -- terminating "
              "execution ***\n";
      exit(1);
   }
   if (pos < 0 || pos > mySize)
   {
      cerr << "*** Illegal location to insert -- " << pos 
           << ".  List unchanged. ***\n";
      return;
   }
   


   // First shift array elements right to make room for item

   for (int i = mySize; i > pos; i--) {
	   myArray[i].number = myArray[i - 1].number;
	   myArray[i].stock = myArray[i - 1].stock;
	   myArray[i].price = myArray[i - 1].price;
	   myArray[i].min = myArray[i - 1].min;
	   myArray[i].name = myArray[i - 1].name;
   }
   // Now insert item at position pos and increase list size  
   myArray[pos].number = item.number;
   myArray[pos].stock = item.stock;
   myArray[pos].price = item.price;
   myArray[pos].min = item.min;
   myArray[pos].name = item.name;
   mySize++;
}

//--- Definition of erase()
void List::erase(int pos)
{
   if (mySize == 0)
   {
      cerr << "*** List is empty ***\n";
      return;
   }
   if (pos < 0 || pos >= mySize)
   {
      cerr << "Illegal location to delete -- " << pos
           << ".  List unchanged. ***\n";
      return;
   }

   // Shift array elements left to close the gap
   for (int i = pos; i < mySize; i++) {
	   myArray[i].number = myArray[i + 1].number;
	   myArray[i].stock = myArray[i + 1].stock;
	   myArray[i].price = myArray[i + 1].price;
	   myArray[i].min = myArray[i + 1].min;
	   myArray[i].name = myArray[i + 1].name;
   }

   
   // Decrease list size
    mySize--;
}

void List::search1(string name)
{
	for (int i = 0; i < mySize; i++) {

		if (name == myArray[i].name) {
			cout << myArray[i].number;
			cout << myArray[i].stock;
			cout << myArray[i].price;
			cout << myArray[i].min;
			cout << myArray[i].name;
		}
	}
}

void List::search2(int number) 
{
	for (int i = 0; i < mySize; i++) {

		if (number == myArray[i].number) {
			cout << myArray[i].number;
			cout << myArray[i].stock;
			cout << myArray[i].price;
			cout << myArray[i].min;
			cout << myArray[i].name;
		}
	}
}


Alright so now when I run the code because I fixed it, its not letting me input any values, its just putting in a constant one and then its returning that one and then it wont let me work. What am I doing wrong
Last edited on
Your code doesn't even compile.

And you've managed to cram about five errors in to one little construct here:
1
2
3
4
5
6
        if (answer = 'Yes' || 'yes') {
            return true;
        }
        else {
            return false;
        }

1. answer is an int
2. You are using = instead of ==
3. You are putting a string literal in single quotes instead of double quotes.
4. You are using || incorrectly.
5. You are returning a bool from an int function.
closed account (E0p9LyTq)
5. You are returning a bool from an int function.

The return is in main(), so even when all the errors are fixed the program will end (return to OS) the first time through the while() loop.
Issues 1 and 2 apply to Line 34 as well:

while (answer = true) {

The fact that you are incorrectly using the assignment operator (=) here means that answer is being initialized (with true being interpreted as an int). If you had used the comparison operator (==) as I believe you intended, then answer would be uninitialized.
Topic archived. No new replies allowed.