Need Help: Wholesaler Problem

My professor doesn't really go over major concepts in class and I'm very confused on how to write this C++ program without if-else statements...

The problem is:
Wholesalers often provide reduced prices on goods if the customer purchases more than one item. Your company wants to create a program that customers can use to determine the cost of their purchase.

The price breakdown that your company uses is

QUANTITY | Price
0 <= q < 10 | $15.00 per unit
10 <= q < 50 | $12.50 per unit + $25.00
50 <= q < 100 | $9.75 per unit + $162.50
100 <= q < 250 | $8.25 per unit + $312.50
250 <= q | $5.25 per unit + $1,062.50

Your program should prompt the user for the number of items purchased then calculate the cost of the purchase. You may not use if-else structures but must instead limit yourself to the relational operators. The output must include printing the number of items ordered as well as the cost of the purchased. The output should be properly formatted.


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
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

int main(void) {

	// Splash Screen
	cout << endl;
	cout << "Your Name" << endl;
	cout << "CMPSC 101" << endl;
	cout << "Date" << endl;
	cout << "Program Description" << endl;
	cout << endl;

	//	Declare a variable for cost. Call it q. This should be a double
	//From student: in the problem, q represents quantity not price.
	//I will use p to represent price not q.
	double p;
	int q;
	
	//	Declare five bool variables, s, t, u, v, and w
	bool s;
	bool t;
	bool u;
	bool v;
	bool w;
	
	//	Use cout and cin to enter the quantity into the program
	//	This will be the number of items to be purchased
	cout << "What is the number of items you wish to purchase?" << endl;
	cin >> q;
	//	Calculate the true and false values for the breakdowns. Each of these
	//	is a single (greater than or equal) relational operator.- The
	//	first is done for you
	s = (q >= 0) & (q < 10);
	t = (q >= 10) & ( q < 50);
	u = (q >= 50) & (q < 100);
	v = (q >= 100) & (q < 250);
	w = (q >= 250);
	//	Calculate the cost - Do NOT use an If-Else block
	
	
	
	//	Print the results as a formatted table. The first line is the
	//	quantity and the second line is the cost.

  std::cout << std::left << std::setw(12) << "Quantity"
            << std::right << std::setw(3) << "Cost"
            << '\n';
  std::cout << std::left << std::setw(12) << q
            << std::right << std::setw(3) << p
            << '\n';
	

	return 0;
}
Last edited on
Can you do a switch statement?
You may not use if-else structures but must instead limit yourself to the relational operators.

Fascinating.

The "if-else structure" has conditional expressions that determine which branch of the structure is executed. Relational operator is usually in conditional expression.

If you are truly given: bool s = (q >= 0) & (q < 10);
then there are two questions:
1. Why bitwise AND, rather than boolean AND? (Although it looks like a red herring.)
2. How to convert group of booleans into a pair of values?

There is always the ternary operator. Effectively it is no different from if-else, but all do not believe that a rose by any other name will smell as sweet ...
Hello geminilie,

I changes some things in the program. First try to avoid single letter variable names. This may seem easy, but hard to follow. I may not have come up with the best names for the "bool" variables, but it gives you an idea and you can see how much easier it is to understand.

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
#include<iostream>
#include<iomanip>
#include <limits>  // <--- Added.
#include<string>

//using namespace std;  // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/

int main()  // <--- "void" is no longer needed here.
{  // <--- Moved.

	// Splash Screen
	//std::cout << std::endl;
	//std::cout << "Your Name" << std::endl;
	//std::cout << "CMPSC 101" << std::endl;
	//std::cout << "Date" << std::endl;
	//std::cout << "Program Description" << std::endl;
	//std::cout << std::endl;

	std::cout << "Your Name\n"
		<< "CMPSC 101\n"
		<< "Date\n"
		<< "\nProgram Description"
		<< std::endl;


	//	Declare a variable for cost. Call it q. This should be a double
	//From student: in the problem, q represents quantity not price.
	//I will use p to represent price not q.
	double price{10.25};
	int quantity{};

	//	Declare five bool variables, s, t, u, v, and w
	bool base{};
	bool tier1{};
	bool tier2{};
	bool tier3{};
	bool tier4{};

	//	Use cout and cin to enter the quantity into the program
	//	This will be the number of items to be purchased
	std::cout << "What is the number of items you wish to purchase? ";
	std::cin >> quantity;

	//	Calculate the true and false values for the breakdowns. Each of these
	//	is a single (greater than or equal) relational operator.- The
	//	first is done for you
	base = (quantity >= 0) & (quantity < 10);
	tier1 = (quantity >= 10) & (quantity < 50);
	tier2 = (quantity >= 50) & (quantity < 100);
	tier3 = (quantity >= 100) & (quantity < 250);
	tier4 = (quantity >= 250);
	//	Calculate the cost - Do NOT use an If-Else block

	base ? price *= 1.0 : tier1 ? price = price - (price * 0.02) : tier2 ? price = price - (price * 0.05) : tier3 ? price = price - (price * 0.1) : tier4 ? price = price - (price * 0.15) : price = price - (price * 0.15);

	//	Print the results as a formatted table. The first line is the
	//	quantity and the second line is the cost.

	std::cout << std::fixed << std::showpoint << std::setprecision(2);  // <--- Added.

	std::cout << std::left << std::setw(12) << "Quantity"
		<< std::right << std::setw(3) << "Cost"
		<< '\n';
	std::cout << std::left << std::setw(12) << quantity
		<< std::right << std::setw(3) << price
		<< '\n';


	// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue :";
	std::cin.get();

	return 0;
}

Lines 20 - 24 are an alternative and a suggestion to what you have. Try to use "std::endl" on the last line and use the new line (\n) in the middle. Usually the "\n" will print everything, i.e., flush the output buffer. To many "std::endl"s in a program can slow it down with the overhead to preform flushing the buffer and the new line and carriage return. You may not notice much difference in a small program though.

On line 42 I put a space after the "?" and removed the "std::endl". This puts the "std::cin" on the same line as the prompt. IMHO I think it looks better this way.

Line 55 is using ternary operator as keskiverto mentioned. True this is a form of an if/else if statement without those key words. Also it is able to be done on one line.

Line 60 needs to be done only once before you print a floating point number. You could put this near the top of the program if you want and it will affect every "std::cout" statement until it is changed.

The bit at the bottom above the "return" is something I use to create a pause or stopping point in the program. You may not need it now, but keep it for the future.

Hope that helps,

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <iomanip>
using namespace std;

void getRates( int q, int &centsPerUnit, int &centsExtra )
{
   centsPerUnit = 1500 - 250 * ( q >= 10 ) -   275 * ( q >= 50 ) -   150 * ( q >= 100 ) -   300 * ( q >= 250 );
   centsExtra   =       2500 * ( q >= 10 ) + 13750 * ( q >= 50 ) + 15000 * ( q >= 100 ) + 75000 * ( q >= 250 );
}

int main()
{
   int q, centsPerUnit, centsExtra;
   cout << "Enter q: ";   cin >> q;
   getRates( q, centsPerUnit, centsExtra );
   int cents = q * centsPerUnit + centsExtra;
   cout << "Price: " << '$' << cents / 100 << '.' << setw( 2 ) << setfill( '0' ) << cents % 100 << '\n';
}


Enter q: 75
Price: $893.75
Registered users can post here. Sign in or register to post.