help with writing a class

Hello,

I have updated my question now I am writing class to do a transaction
it does what I want but it fails when it comes to error checking process

my error checking statement are commented out
I don't know it's complaining although there's no major errors or warnings

Thanks in advance

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
70
71
72
73
74
75
76
77
78
79
80
81
class Transaction{
public:
	Transaction(unsigned doughnuts, unsigned carburetors = 0, unsigned racquets = 0);
	unsigned getDoughnuts() const;		
	unsigned getCarburetors() const;	
	unsigned getRacquets() const;		
	double getSubtotal() const;			
	double getTotal() const;			
private:
	unsigned myDoughnuts;			
	unsigned myCarburetors;		
	unsigned myRacquets;		
	double	 subtotal;
	double	 total;
};

void report(const Transaction transaction[], unsigned elements);
bool die(const string & msg);

int main()
{
	Transaction march[] = { Transaction(1, 2, 3), Transaction(10, 1,0), Transaction(12,0,0), Transaction(0, 2, 5) };
	report(march, 4);
}

Transaction::Transaction(unsigned doughnuts, unsigned carburetors , unsigned racquets )
	:myDoughnuts(doughnuts), myCarburetors(carburetors), myRacquets(racquets)
{
	/* if (!doughnuts) die("Invalid doug"); 
	if (!carburetors) die("Invalid carb");
	if (!racquets) die("Invalid rac"); */
	
}

unsigned Transaction::getDoughnuts() const
{return myDoughnuts;}

unsigned Transaction::getCarburetors() const
{return myCarburetors;}

unsigned Transaction::getRacquets() const
{return myRacquets;}

double Transaction::getSubtotal() const
{
	double subtotal=0;
	subtotal = (myDoughnuts*0.4) + (myCarburetors * 200) + (myRacquets * 75);
	return subtotal;
}

double Transaction::getTotal() const
{
	double total=0;
	total = (myDoughnuts*0.4*0.09+ myDoughnuts*0.4) + (myCarburetors * 200*0.09+ myCarburetors*200) + (myRacquets * 75);
	return total;
}

void report(const Transaction transaction[], unsigned elements)
{
	unsigned doughnuts=0, carburetors=0, racquets=0;
	double subtotal=0, total=0;
	for (unsigned i = 0; i < elements; i++)
	{
		doughnuts += transaction[i].getDoughnuts();
		carburetors += transaction[i].getCarburetors();
		racquets += transaction[i].getRacquets();
		subtotal += transaction[i].getSubtotal();
		total += transaction[i].getTotal();
	}
	cout << "doughnuts:         " << doughnuts << endl;
	cout << "carburetors:       " << carburetors << endl;
	cout << "racquets:          " << racquets << endl;
	cout << "roral without tax: " << subtotal << endl;
	cout << "total with tax:    " << total << endl;
}

bool die(const string & msg)
{
	cerr << endl << "FATAL ERROR: " << msg << endl;
	exit(EXIT_FAILURE);
}
Last edited on
the first thing I want to ask about is the variables X and Y represent individual points or just the values or x and y


Since you defined the class you should be able to answer this question better than anyone else.

If your class is named point, what is the purpose of all of the variables you defined in your class? Normally a "point" would only have two or three variables depending on if you're modelling two or three dimensional space.
Actually this is an assignment and I need help

I was trying to get the 4 values of 2 points to find the distance between them
I figured out the distance but now I need help to find the area
a,b, and c are three points
Generally when trying to figure out an algorithm, I take some imagined test cases and work out the solution manually. As I work out the calculations manually, I get a sense of what the procedure is.

For example, on paper work out the area for a triangle with coordinates (-2,3),(4,0), and (0,-3).
closed account (48T7M4Gy)
.
Last edited on
Please take look at my updated question
Its coz you used the logic NOT .

example.

1
2
3
4
Transaction(1,2,0); // Transaction(doughnuts, carburetors , racquets )
if(!racquets)
   die;  //true. coz racquets is 0.


NOT has 2 states. Non-zero and zero.
do you think if there's other than this way of checking

1
2
3
if (doughnuts<0) die("Invalid doug");
	if (carburetors<0) die("Invalid carb");
	if (racquets<0) die("Invalid rac");
Think that is the best way. Thumbs up!
Topic archived. No new replies allowed.