Retail Item (OOP)

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
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
Perhaps as a starter:

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
69
#include <string>
#include <iostream>

class RetailItem {
	std::string description;
	int unitsOnHand {};
	int select {};
	double price {};

public:
	RetailItem(const std::string&, int, double);
	RetailItem(const std::string&);
	RetailItem();
	void setDesc(const std::string&);
	void setUnits(int);
	void setPrice(double);
	std::string getDesc() const;
	int getUnits() const;
	double getPrice() const;
};

RetailItem::RetailItem(const std::string& d, int u, double p) : description(d), unitsOnHand(u), price(p) {}
RetailItem::RetailItem(const std::string& d) : description(d) {}
RetailItem::RetailItem() {}

void RetailItem::setDesc(const std::string& d) { description = d; }
void RetailItem::setUnits(int u) { unitsOnHand = u; }
void RetailItem::setPrice(double p) { price = p; }
std::string RetailItem::getDesc() const { return description; }
int RetailItem::getUnits() const { return unitsOnHand; }
double RetailItem::getPrice() const { return price; }

int main()
{
	int cho {};
	RetailItem choice3;

	std::cout << "Choose a Retail Item(1-3)\n1. Name: Jacket\n2. Name: Designer Jeans\n3. Name: Shirt\nChoice: ";
	std::cin >> cho;

	switch (cho) {
		case 1:
			choice3.setDesc("Jacket");
			choice3.setUnits(12);
			choice3.setPrice(59.95);
			break;

		case 2:
			choice3.setDesc("Designer Jeans");
			choice3.setUnits(40);
			choice3.setPrice(34.95);
			break;

		case 3:
			choice3.setDesc("Shirt");
			choice3.setUnits(20);
			choice3.setPrice(24.95);
			break;

		default:
			std::cout << "Invalid option\n";
			break;
	}

	if (cho >= 1 && cho <= 3)
		std::cout << "Name: " << choice3.getDesc() << '\n' <<
			"Units: " << choice3.getUnits() << '\n' <<
			"Price: " << choice3.getPrice() << '\n';
}

See http://www.cplusplus.com/forum/beginner/279521/

Apparently OP did not learn anything from that thread.
Topic archived. No new replies allowed.