### Array Problem

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304`` `````` //Driver.cpp #include #include #include #include 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 #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 #include 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

And you've managed to cram about five errors in to one little construct here:
 ``123456`` `````` if (answer = 'Yes' || 'yes') { return true; } else { return false; }``````

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.