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