C++ code doesn\'t add up the items properly. It exits before it adds up the tota
ID: 3829564 • Letter: C
Question
C++ code doesn't add up the items properly. It exits before it adds up the total_price after the first item is entered . Help please? It exits the program before I can call the "3. display list" to see my items.
#include <iostream>
#include <string>
using namespace std;
class Item {
private:
string name;
enum unit_types {can, box, pounds, ounces} unit;
int quantity;
float unit_price;
float total_price;
public:
Item() {
cout<<"Enter item name: ";
cin>>name;
cout<<"Enter unit type: "<<endl<<"1. Can 2. Box 3. Pounds 4. Ounces: ";
int temp;
cin>>temp;
unit = (unit_types)temp;
cout<<"Enter quantity: ";
cin>>quantity;
cout<<"Enter unit price: ";
cin>>unit_price;
total_price = unit_price * quantity;
}
void print(void) {
cout<<"Number of items: "<<quantity<<endl;
cout<<"Unit of sale: "<<unit<<endl;
cout<<"Quantity: "<<quantity;
cout<<"Unit price: ";
switch(unit) {
case 0:
cout<<"Can";
break;
case 1:
cout<<"Box";
break;
case 2:
cout<<"Pounds";
break;
case 3:
cout<<"Ounces";
break;
}
cout<<endl;
cout<<"Total price: "<<total_price<<endl;
}
float getTotalPrice() {
return total_price;
}
string getName() {
return name;
}
};
class List {
private:
Item * list;
int size;
int capacity;
public:
List() {
list = (Item *)calloc(4, sizeof(Item));
size = 0;
capacity = 4;
}
void addItem() {
Item item;
if(size == capacity) {
capacity *= 2;
Item * temp = (Item *)calloc(capacity, sizeof(Item));
for(int i=0; i<size; i++)
temp[i] = list[i];
list = temp;
}
list[size++] = item;
}
void removeItem(string name) {
Item * temp = (Item *)calloc(capacity, sizeof(Item));
for(int i=0; i<size; i++) {
if(name.compare(temp[i].getName()) != 0)
temp[i] = list[i];
}
list = temp;
size--;
}
void display() {
float total = 0;
cout<<"Size = "<<size<<endl;
for(int i=0; i<size; i++) {
cout<<"Item "<<(i+1)<<endl;
list[i].print();
cout<<endl;
total += list[i].getTotalPrice();
}
cout<<"Total price of all items: "<<total<<endl;
}
};
int main()
{
char ch = 'y';
while(ch == 'y') {
int choice;
cout<<"1. Add Item 2. Delete Item 3. Display list ";
cout<<"Enter choice: ";
cin>>choice;
List list;
string name;
switch (choice) {
case 1:
list.addItem();
break;
case 2:
cout<<"Enter item name to remove: ";
cin>>name;
list.removeItem(name);
break;
case 3:
cout<<"Displaying the list..."<<endl;
list.display();
break;
default:
cout<<"Invalid choice"<<endl;
break;
}
cout<<"Do you wish to continue (y/n)? ";
cin>>ch;
}
return 0;
}
Explanation / Answer
Hi, I have fixed the issue.
It is working fine now.
#include <iostream>
#include <string>
using namespace std;
class Item {
private:
string name;
enum unit_types {can, box, pounds, ounces} unit;
int quantity;
float unit_price;
float total_price;
public:
Item() {
cout<<"Enter item name: ";
cin>>name;
cout<<"Enter unit type: "<<endl<<"1. Can 2. Box 3. Pounds 4. Ounces: ";
int temp;
cin>>temp;
unit = (unit_types)temp;
cout<<"Enter quantity: ";
cin>>quantity;
cout<<"Enter unit price: ";
cin>>unit_price;
total_price = unit_price * quantity;
}
void print(void) {
cout<<"Number of items: "<<quantity<<endl;
cout<<"Unit of sale: "<<unit<<endl;
cout<<"Quantity: "<<quantity<<endl;
cout<<"Unit price: ";
switch(unit) {
case 0:
cout<<"Can";
break;
case 1:
cout<<"Box";
break;
case 2:
cout<<"Pounds";
break;
case 3:
cout<<"Ounces";
break;
}
cout<<endl;
cout<<"Total price: "<<total_price<<endl;
}
float getTotalPrice() {
return total_price;
}
string getName() {
return name;
}
};
class List {
private:
Item * list;
int size;
int capacity;
public:
List() {
list = (Item *)calloc(4, sizeof(Item));
size = 0;
capacity = 4;
}
void addItem() {
Item item;
if(size == capacity) {
capacity *= 2;
Item * temp = (Item *)calloc(capacity, sizeof(Item));
for(int i=0; i<size; i++)
temp[i] = list[i];
list = temp;
}
list[size++] = item;
}
void removeItem(string name) {
Item * temp = (Item *)calloc(capacity, sizeof(Item));
for(int i=0; i<size; i++) {
if(name.compare(temp[i].getName()) != 0)
temp[i] = list[i];
}
list = temp;
size--;
}
void display() {
float total = 0;
cout<<"Size = "<<size<<endl;
for(int i=0; i<size; i++) {
cout<<"Item "<<(i+1)<<endl;
list[i].print();
cout<<endl;
total += list[i].getTotalPrice();
}
cout<<"Total price of all items: "<<total<<endl;
}
};
int main()
{
List list;
char ch = 'y';
while(ch == 'y') {
int choice;
cout<<"1. Add Item 2. Delete Item 3. Display list ";
cout<<"Enter choice: ";
cin>>choice;
string name;
switch (choice) {
case 1:
list.addItem();
break;
case 2:
cout<<"Enter item name to remove: ";
cin>>name;
list.removeItem(name);
break;
case 3:
cout<<"Displaying the list..."<<endl;
list.display();
break;
default:
cout<<"Invalid choice"<<endl;
break;
}
cout<<"Do you wish to continue (y/n)? ";
cin>>ch;
}
return 0;
}
######## Sample run ######
fourthweek
fourthweek g++ displayCents.cpp
fourthweek ./a.out
1. Add Item
2. Delete Item
3. Display list
Enter choice: 1
Enter item name: Pk
Enter unit type:
1. Can
2. Box
3. Pounds
4. Ounces: 1
Enter quantity: 4
Enter unit price: 56.6
Do you wish to continue (y/n)? y
1. Add Item
2. Delete Item
3. Display list
Enter choice: 3
Displaying the list...
Size = 1
Item 1
Number of items: 4
Unit of sale: 1
Quantity: 4
Unit price: Box
Total price: 226.4
Total price of all items: 226.4
Do you wish to continue (y/n)? y
1. Add Item
2. Delete Item
3. Display list
Enter choice: 1
Enter item name: Jk
Enter unit type:
1. Can
2. Box
3. Pounds
4. Ounces: 2
Enter quantity: 3
Enter unit price: 76
Do you wish to continue (y/n)? y
1. Add Item
2. Delete Item
3. Display list
Enter choice: 3
Displaying the list...
Size = 2
Item 1
Number of items: 4
Unit of sale: 1
Quantity: 4
Unit price: Box
Total price: 226.4
Item 2
Number of items: 3
Unit of sale: 2
Quantity: 3
Unit price: Pounds
Total price: 228
Total price of all items: 454.4
Do you wish to continue (y/n)? n
fourthweek
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.