An online shopper program Problem

Hello, I have a problem here with this question. There are 2 parts to this question and I have solved the first part. Here is the question:

Part A)
An online shopper searches web to find an item and purchase is from the store with the lowest total price. The total price includes: the price of the item plus the shipping and
handling cost. Write a simple C++ program to do the following: Ask the user for the unit price of the item, the number of items the shopper wants to purchase, the name of the first store, the shipping cost of the first store, the name and the shipping cost of the second store. Then, calculate the total cost for each store and decide from which store the shopper should buy the item.

Part B)
Display the following to shopper (as a table): The unit item cost, the number of items purchased, the name and shipping cost of the first store, the name and shipping cost of the second store, and your recommendation on from which store the shopper should make the purchase and total cost difference. See the example below:

UNIT COST # STORE NAME SHIPPING TOTAL
------------------------------------------------------------
12.98 2 Toys r us 2.50 28.46
13.54 2 Amazon.com 0.00 27.08
------------------------------------------------------------
| Lowest price from Amazon.com, you may save $1.38 |
------------------------------------------------------------
(Sorry if this looks weird but I can't seem to fix this on the forum, it's actually aligned)


I answered Part A and it works fine, but I don't know how get part B to look like the example, I try to do it, but every time I run it I get some of the words closer to each or farther away from each other. This is what I have so far. Please help.


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
82
83
84

#include <iostream>;
#include <string>;

using namespace std;

int main()
{

	// Set all the variables
	double price1, price2, sCost1, sCost2,
		difference, total1, total2, items;

	// Set the strings
	string store1, store2;


	// Store 1
	cout << "What is the price of the item from the 1st store? " << endl;
	cin >> price1;

	cout << "How many items did you buy? " << endl;
	cin >> items;
	cin.ignore();

	cout << "What was the store you bought it from? " << endl;
	getline(cin, store1);

	cout << "What's the shipping cost? " << endl;
	cin >> sCost1;

	// Store 2
	cout << "What is the price of the item from the 2nd store? " << endl;
	cin >> price2;
	cin.ignore();

	cout << "What was the store you bought it from? " << endl;
	getline(cin, store2);

	cout << "What's the shipping cost? " << endl;
	cin >> sCost2;

	// Calculating totals from Store 1 and Store 1
	total1 = (price1 * items) + sCost1;
	total2 = (price2 * items) + sCost2;

	// Calculating difference
	// Determine which total is greater so they can subtract without a negative
	if (total1 > total2)
	{
		difference = total1 - total2;
	}
	else if (total1 = total2)
	{
		difference = total1 - total2;
	}
	else
	{
		difference = total2 - total1;
	}

// This is where I have the problem
	cout << "UNIT COST # STORE NAME	SHIPPING TOTAL " << endl;
	cout << "-------------------------------------------------------- " << endl;
	cout << price1 << "		" << items << "		" << store1 << "	 " << sCost1 << "	 " << total1 << endl;
	cout << price2 << "		" << items << "		" << store2 << "	 " << sCost2 << "	 " << total1 << endl;
	cout << "-------------------------------------------------------- " << endl;

	// Determining lowest price
	if (total1 > total2)
	{
		cout << "Lowest price from " << store2 << ", you may save " << difference << endl;
	}
	else
	{
		cout << "Lowest price from " << store1 << ", you may save " << difference << endl;
	}



	return 0;
}

Last edited on
One part of the solution is to be able to format numbers consistently.
For example the shipping cost 2.50 or 0.00 may display as 2.5 and 0 respectively.

So you should look at that first.

#include <iomanip>

1
2
std::cout << std::fixed;
std::cout << std::setprecision(2);

see reference page:
http://www.cplusplus.com/reference/iomanip/setprecision/
http://www.cplusplus.com/reference/ios/fixed/

That should help you to make things a bit more consistent.

For the other parts of the layout, rather than printing some fixed number of spaces between each value, what you need is to ensure that the printed value always occupies a specific number of characters - padded with spaces if necessary. Use std::setw() for that.
http://www.cplusplus.com/reference/iomanip/setw/

There may be one more piece of this which will help. You may want to specify whether a column is aligned to the left or right-hand side. Use std::left and std:: right for that, as required.
http://www.cplusplus.com/reference/ios/left/
http://www.cplusplus.com/reference/ios/right/

I would hope that you have already used some of these capabilities, if not, now's the time to learn about them.


Is the equality on line 53 correct?
Is the equality on line 53 correct?

You mean the assignment ... no, it looks wrong.
You mean the assignment [...]


you're right. Thanks.
Topic archived. No new replies allowed.