Aug 16, 2021 at 11:10pm UTC
Keep getting logical errors. Printing empty output
Main File
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
#include "RetailItem.h"
#include <iostream>
using namespace std;
int main()
{
int cho;
int unit;
double cost;
RetailItem choice3;
printf("Choose a Retail Item(1-3)\n1. Name: Jacket\n2. Name: Designer Jeans\n3. Name: Shirt\nChoice: " );
cin >> cho;
switch (cho)
{
case '1' :
choice3.setDesc("Jacket" );
choice3.setUnits(12);
choice3.setPrice(59.95);
cout << "Name: " << choice3.getDesc() << endl;
cout << "Units: " << choice3.getUnits() << endl;
cout << "Price: " << choice3.getPrice() << endl;
break ;
case '2' :
choice3.setDesc("Designer Jeans" );
choice3.setUnits(40);
choice3.setPrice(34.95);
cout << "Name: " << choice3.getDesc() << endl;
cout << "Units: " << choice3.getUnits() << endl;
cout << "Price: " << choice3.getPrice() << endl;
break ;
case '3' :
choice3.setDesc("Shirt" );
choice3.setUnits(20);
choice3.setPrice(24.95);
cout << "Name: " << choice3.getDesc() << endl;
cout << "Units: " << choice3.getUnits() << endl;
cout << "Price: " << choice3.getPrice() << endl;
break ;
default :
choice3.setDesc("Jacket" );
choice3.setUnits(12);
choice3.setPrice(59.95);
cout << "Name: " << choice3.getDesc() << endl;
cout << "Units: " << choice3.getUnits() << endl;
cout << "Price: " << choice3.getPrice() << endl;
break ;
}
return 0;
}
Header File
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
#ifndef RETAILITEM_H
#define RETAILITEM_H
#include <iostream>
using namespace std;
class RetailItem
{
private :
string description;
int unitsOnHand;
int select;
double price;
public :
RetailItem(string, int , double );
RetailItem(string);
RetailItem();
void setDesc(string);
void setUnits(int );
void setPrice(double );
string getDesc();
int getUnits();
double getPrice();
};
RetailItem::RetailItem(string d, int u, double p)
{
d = description;
u = unitsOnHand;
p = price;
}
RetailItem::RetailItem(string d)
{
d = description;
}
RetailItem::RetailItem()
{
description = "" ;
unitsOnHand = 0;
price = 0.0;
}
void RetailItem::setDesc(string d)
{
d = description;
}
void RetailItem::setUnits(int u)
{
u = unitsOnHand;
}
void RetailItem::setPrice(double p)
{
p = price;
}
string RetailItem::getDesc()
{
return description;
}
int RetailItem::getUnits()
{
return unitsOnHand;
}
double RetailItem::getPrice()
{
return price;
}
#endif
Last edited on Aug 16, 2021 at 11:11pm UTC
Aug 16, 2021 at 11:13pm UTC
we got code, but ... can you elaborate? perhaps say what you typed into the program?
where do you LOAD up the retail items? why do you only have 1 of them? it seems like you should have a container of them, one with jacket info, one with shirts, etc.
it sort of works because you set then get right back out. but it does not seem like its going to work long term as anything but a unit test of the class?
why the printf? it will work, but cout is the c++ way
and... the real problem.
the setters' assignments are all backwards.
void RetailItem::setDesc(string d)
{
d = description; //modify the parameter
}
should be
description = d;
Last edited on Aug 16, 2021 at 11:21pm UTC