C++ need help editing my code so it can run successfully its giving me a error o
ID: 3721080 • Letter: C
Question
C++ need help editing my code so it can run successfully its giving me a error on line 184 someone help please:
SimpleVector.h
--------------
#ifndef SimpleVector_h
#define SimpleVector_h
#include
#include
using namespace std;
template
class SimpleVector
{
private:
T *elements;
int numElements;
int capacity;
public:
SimpleVector(int capacity = 10);
SimpleVector(const SimpleVector &v);
SimpleVector& operator = (const SimpleVector& v);
void add(T elem);
void set(int index, T elem);
int search(T elem);
void sort();
void listAll();
int getNumElements();
T get(int index);
~SimpleVector();
};
template
SimpleVector::SimpleVector(int cap)
{
capacity = cap;
elements = new T[cap];
numElements = 0;
}
template
SimpleVector::SimpleVector(const SimpleVector &v)
{
capacity = v.capacity;
elements = new T[capacity];
numElements = v.numElements;
for(int i = 0; i < numElements; i++)
elements[i] = v.elements[i];
}
template
SimpleVector& SimpleVector::operator = (const SimpleVector& v)
{
delete []elements;
capacity = v.capacity;
elements = new T[capacity];
numElements = v.numElements;
for(int i = 0; i < numElements; i++)
elements[i] = v.elements[i];
}
template
void SimpleVector::add(T elem)
{
if(numElements == capacity) //is it full, expadn to double capacity
{
capacity *= 2;
T* temp = new T[capacity];
for(int i = 0; i < numElements; i++)
temp[i] = elements[i];
delete []elements;
elements = temp;
}
elements[numElements] = elem;
numElements++;
}
template
void SimpleVector::set(int index, T elem)
{
if(index >= 0 && index < numElements)
elements[index] = elem;
}
template
int SimpleVector::search(T elem)
{
for(int i = 0 ; i < numElements; i++)
{
if(elements[i] == elem)
return i;
}
return -1;
}
template
void SimpleVector::sort()
{
for(int i = 0; i < numElements; i++)
{
int minIdx = i;
for(int j = i+1; j < numElements; j++)
{
if(elements[j] < elements[minIdx])
minIdx = j;
}
if(i != minIdx)
{
T temp = elements[i];
elements[i] = elements[minIdx];
elements[minIdx] = temp;
}
}
}
template
void SimpleVector::listAll()
{
for(int i = 0; i < numElements; i++)
cout << elements[i] << endl;
cout << endl;
}
template
int SimpleVector::getNumElements()
{
return numElements;
}
template
T SimpleVector::get(int index)
{
return elements[index];
}
template
SimpleVector::~SimpleVector()
{
delete []elements;
}
#endif /* SimpleVector_h */
Item.h
--------------
#ifndef Item_h
#define Item_h
#include
using namespace std;
class Item
{
private:
int SKU;
string description;
int quantity;
public:
Item();
Item(int sku, string desc, int qty);
int getSKU();
string getDescription();
int getQuantity();
void setSKU(int sku);
void setDescription(string desc);
void setQuantity(int qty);
friend ostream& operator << (ostream& out, const Item& item);
bool operator <(const Item &i);
bool operator >(const Item &i);
bool operator ==(const Item &i);
};
#endif /* Item_h */
Item.cpp
--------------
#include "Item.h"
Item::Item()
{
SKU = 0;
description = "";
quantity = 0;
}
Item::Item(int sku, string desc, int qty)
{
SKU = sku;
description = desc;
quantity = qty;
}
int Item::getSKU()
{
return SKU;
}
string Item::getDescription()
{
return description;
}
int Item::getQuantity()
{
return quantity;
}
void Item::setSKU(int sku)
{
SKU = sku;
}
void Item::setDescription(string desc)
{
description = desc;
}
void Item::setQuantity(int qty)
{
quantity = qty;
}
ostream& operator << (ostream& out, const Item& item)
{
out << "SKU: " << item.SKU << ", " << item.description << "[ stock = " << item.quantity << "]" ;
return out;
}
bool Item::operator <(const Item &i)
{
return SKU < i.SKU;
}
bool Item::operator >(const Item &i)
{
return SKU > i.SKU;
}
bool Item::operator ==(const Item &i)
{
return SKU == i.SKU;
}
main.cpp
--------------
#include
#include
#include "SimpleVector.h"
#include "Item.h"
using namespace std;
int main()
{
string choice = " ";
string desc;
int sku, qty;
SimpleVector items;
while(choice != "q" && choice != "Q")
{
cout << "Inventory Menu" << endl;
cout << "A - add an item" << endl;
cout << "S - search for an Item" << endl;
cout << "L - list all items" << endl;
cout << "Q - Quit" << endl;
cout << "Your choice: ";
cin >> choice;
cout << endl;
if(choice == "a" || choice == "A")
{
cout << "Enter SKU: ";
cin >> sku;
cout << "Enter description: ";
cin >> desc;
cout << "Enter quantity: ";
cin >> qty;
Item it(sku, desc, qty);
items.add(it);
}
else if(choice == "s" || choice == "S")
{
cout << "Enter SKU to find: ";
cin >> sku;
Item toFind(sku, "", 0);
int index = items.search(toFind);
if( index == -1)
cout << sku << " is not a valid SKU";
else
{
cout << items.get(index) << endl;
Item it = items.get(index);
cout << "Update description (y/n)? ";
string ans;
cin >> ans;
if(ans == "y" || ans == "Y")
{
cout << "Enter new description: ";
cin >> desc;
it.setDescription(desc);
cout << "Description updated" << endl;
}
cout << "Update quantity (y/n)? ";
cin >> ans;
if(ans == "y" || ans == "Y")
{
cout << "Enter new quantity: ";
cin >> qty;
it.setQuantity(qty);
cout << "Quantity updated" << endl;
}
cout << it << endl;
items.set(index, it);
}
}
else if(choice == "l" || choice == "L")
{
if(items.getNumElements() == 0)
cout << "No items" << endl;
else
{
items.sort();
items.listAll();
}
}
cout << endl << endl;
}
cout << "Good bye!" << endl;
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
output should be:
------
Inventory Menu
A - add an item
S - search for an Item
L - list all items
Q - Quit
Your choice: s
Enter SKU to find: 10
10 is not a valid SKU
Inventory Menu
A - add an item
S - search for an Item
L - list all items
Q - Quit
Your choice: a
Enter SKU: 100
Enter description: keyboard
Enter quantity: 10
Inventory Menu
A - add an item
S - search for an Item
L - list all items
Q - Quit
Your choice: l
SKU: 100, keyboard[ stock = 10]
Inventory Menu
A - add an item
S - search for an Item
L - list all items
Q - Quit
Your choice: a
Enter SKU: 200
Enter description: mouse
Enter quantity: 20
Inventory Menu
A - add an item
S - search for an Item
L - list all items
Q - Quit
Your choice: a
Enter SKU: 110
Enter description: USBdrive
Enter quantity: 2
Inventory Menu
A - add an item
S - search for an Item
L - list all items
Q - Quit
Your choice: L
SKU: 100, keyboard[ stock = 10]
SKU: 110, USBdrive[ stock = 2]
SKU: 200, mouse[ stock = 20]
Inventory Menu
A - add an item
S - search for an Item
L - list all items
Q - Quit
Your choice: s
Enter SKU to find: 110
SKU: 110, USBdrive[ stock = 2]Update description (y/n)? n
Update quantity (y/n)? y
Enter new quantity: 7
Quantity updated
SKU: 110, USBdrive[ stock = 7]
Inventory Menu
A - add an item
S - search for an Item
L - list all items
Q - Quit
Your choice: l
SKU: 100, keyboard[ stock = 10]
SKU: 110, USBdrive[ stock = 7]
SKU: 200, mouse[ stock = 20]
Inventory Menu
A - add an item
S - search for an Item
L - list all items
Q - Quit
Your choice: q
Explanation / Answer
SimpleVector.h
#ifndef SimpleVector_h
#define SimpleVector_h
// #include<>
// #include
using namespace std;
template<class T>
class SimpleVector
{
private:
T *elements;
int numElements;
int capacity;
public:
SimpleVector(int capacity = 10);
SimpleVector(const SimpleVector &v);
SimpleVector<T> &operator=(const SimpleVector<T> &v);
void add(T elem);
void set(int index, T elem);
int search(T elem);
void sort();
void listAll();
int getNumElements();
T get(int index);
~SimpleVector<T>();
};
template<class T>
SimpleVector<T>::SimpleVector(int cap)
{
capacity = cap;
elements = new T[cap];
numElements = 0;
}
template<class T>
SimpleVector<T>::SimpleVector(const SimpleVector &v)
{
capacity = v.capacity;
elements = new T[capacity];
numElements = v.numElements;
for (int i = 0; i < numElements; i++)
elements[i] = v.elements[i];
}
template<class T>
SimpleVector<T> &SimpleVector<T>::operator=(const SimpleVector<T> &v)
{
delete[] elements;
capacity = v.capacity;
elements = new T[capacity];
numElements = v.numElements;
for (int i = 0; i < numElements; i++)
elements[i] = v.elements[i];
}
template<class T>
void SimpleVector<T>::add(T elem)
{
if (numElements == capacity) //is it full, expadn to double capacity
{
capacity *= 2;
T *temp = new T[capacity];
for (int i = 0; i < numElements; i++)
temp[i] = elements[i];
delete[] elements;
elements = temp;
}
elements[numElements] = elem;
numElements++;
}
template<class T>
void SimpleVector<T>::set(int index, T elem)
{
if (index >= 0 && index < numElements)
elements[index] = elem;
}
template<class T>
int SimpleVector<T>::search(T elem)
{
for (int i = 0; i < numElements; i++)
{
if (elements[i] == elem)
return i;
}
return -1;
}
template<class T>
void SimpleVector<T>::sort()
{
for (int i = 0; i < numElements; i++)
{
int minIdx = i;
for (int j = i + 1; j < numElements; j++)
{
if (elements[j] < elements[minIdx])
minIdx = j;
}
if (i != minIdx)
{
T temp = elements[i];
elements[i] = elements[minIdx];
elements[minIdx] = temp;
}
}
}
template<class T>
void SimpleVector<T>::listAll()
{
for (int i = 0; i < numElements; i++)
cout << elements[i] << endl;
cout << endl;
}
template<class T>
int SimpleVector<T>::getNumElements()
{
return numElements;
}
template<class T>
T SimpleVector<T>::get(int index)
{
return elements[index];
}
template<class T>
SimpleVector<T>::~SimpleVector<T>()
{
delete[] elements;
}
#endif /* SimpleVector_h */
Item.h
#ifndef Item_h
#define Item_h
#include<string>
using namespace std;
class Item
{
private:
int SKU;
string description;
int quantity;
public:
Item();
Item(int sku, string desc, int qty);
int getSKU();
string getDescription();
int getQuantity();
void setSKU(int sku);
void setDescription(string desc);
void setQuantity(int qty);
friend ostream &operator<<(ostream &out, const Item &item);
bool operator<(const Item &i);
bool operator>(const Item &i);
bool operator==(const Item &i);
};
#endif /* Item_h */
Item.cpp
#include<iostream>
#include "Item.h"
using namespace std;
Item::Item()
{
SKU = 0;
description = "";
quantity = 0;
}
Item::Item(int sku, string desc, int qty)
{
SKU = sku;
description = desc;
quantity = qty;
}
int Item::getSKU()
{
return SKU;
}
string Item::getDescription()
{
return description;
}
int Item::getQuantity()
{
return quantity;
}
void Item::setSKU(int sku)
{
SKU = sku;
}
void Item::setDescription(string desc)
{
description = desc;
}
void Item::setQuantity(int qty)
{
quantity = qty;
}
ostream &operator<<(ostream &out, const Item &item)
{
out << "SKU: " << item.SKU << ", " << item.description << "[ stock = " << item.quantity << "]";
return out;
}
bool Item::operator<(const Item &i)
{
return SKU < i.SKU;
}
bool Item::operator>(const Item &i)
{
return SKU > i.SKU;
}
bool Item::operator==(const Item &i)
{
return SKU == i.SKU;
}
main.cpp
#include<iostream>
// #include
#include "SimpleVector.h"
#include "Item.h"
using namespace std;
int main()
{
string choice = " ";
string desc;
int sku, qty;
SimpleVector<Item> items;
while (choice != "q" && choice != "Q")
{
cout << "Inventory Menu" << endl;
cout << "A - add an item" << endl;
cout << "S - search for an Item" << endl;
cout << "L - list all items" << endl;
cout << "Q - Quit" << endl;
cout << "Your choice: ";
cin >> choice;
cout << endl;
if (choice == "a" || choice == "A")
{
cout << "Enter SKU: ";
cin >> sku;
cout << "Enter description: ";
cin >> desc;
cout << "Enter quantity: ";
cin >> qty;
Item it(sku, desc, qty);
items.add(it);
}
else if (choice == "s" || choice == "S")
{
cout << "Enter SKU to find: ";
cin >> sku;
Item toFind(sku, "", 0);
int index = items.search(toFind);
if (index == -1)
cout << sku << " is not a valid SKU";
else
{
cout << items.get(index) << endl;
Item it = items.get(index);
cout << "Update description (y/n)? ";
string ans;
cin >> ans;
if (ans == "y" || ans == "Y")
{
cout << "Enter new description: ";
cin >> desc;
it.setDescription(desc);
cout << "Description updated" << endl;
}
cout << "Update quantity (y/n)? ";
cin >> ans;
if (ans == "y" || ans == "Y")
{
cout << "Enter new quantity: ";
cin >> qty;
it.setQuantity(qty);
cout << "Quantity updated" << endl;
}
cout << it << endl;
items.set(index, it);
}
}
else if (choice == "l" || choice == "L")
{
if (items.getNumElements() == 0)
cout << "No items" << endl;
else
{
items.sort();
items.listAll();
}
}
cout << endl
<< endl;
}
cout << "Good bye!" << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.