//Driver.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
usingnamespace 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') {
returntrue;
}
else {
returnfalse;
}
}
}
//List.h
#include <iostream>
#ifndef LIST
#define LIST
usingnamespace std;
struct lens
{
int number;
int stock;
float price;
int min;
string name;
};
constint 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>
usingnamespace 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
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.
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.