Hello AJ24,
I also say this for
Manga's benefit.
Start by rereading your directions.
This part:
-The charge for all signs is a minimum of $35.00.
-The first five letters or numbers are included in the minimum charge; there is a $4 charge for each additional character.
-If the sign is made of oak, add $20.00. No charge is added for pine.
-Black or white characters are included in the minimum charge; there is an additional $15 charge for gold-leaf lettering.
|
This tells me that there will be constant variables in the program with these values.
This:
-A variable for the cost of the sign initialized to 0.00 (charge).
-A variable for the number of characters initialized to 8 (numChars).
-A variable for the color of the characters initialized to "gold" (color).
-A variable for the wood type initialized to "oak" (woodType).
|
Tells me the other variable names and and that they are to be initialized to the values given.
Although for "color" I do like "letterColor" better.
Given the directions and the input from
Repeater I came up with this. I also added a few things that will help now and in the future.
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
|
#include <iostream>
#include <iomanip> // <--- Added for "setprecision".
//#include <limits>
#include <string> // <--- Added.
//using namespace std; // <--- Best not to use.
// The most recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
int main()
{
constexpr double MIN_CHARGE{ 35.0 };
constexpr double MIN_CHARS{ 5.0 };
constexpr double EXTRA_LTR{ 4.0 };
constexpr double OAK_CHARGE{ 20.0 };
constexpr double GOLD_LEAF_CHARGE{ 15.0 };
const std::string GOLD_LTRING{ "Gold" };
double charge{};
double numChars{ 8.0 };
std::string woodType{ "Oak" };
std::string letterColor{ "Gold" };
//string Oak; // <--- Not used.
std::cout << std::fixed << std::showpoint << std::setprecision(2); // <--- Only needs done once.
//std::cout << "State the text of color, White, Black, or Gold? ";
//std::cin >> letterColor;
//std::cout << "Enter the number of characters on the sign: ";
//std::cin >> letterAmount;
//std::cout << "What type of wood would you like, Pine or Oak? ";
//std::cin >> woodType;
charge = MIN_CHARGE; // <--- Sets "charge" to minimum amount to start with before the if statements.
//if (letterColor == GOLD_LTRING) // <--- Alternative using a constant variable.
if (letterColor == "Gold") // <--- Adds extra charge if necessary.
{
charge += GOLD_LEAF_CHARGE;
}
//else // <--- Replaced with line 35.
//{
// charge = minC;
//}
if (numChars > MIN_CHARS)
{
charge += EXTRA_LTR * (numChars - MIN_CHARS);
}
if (woodType == "Oak")
{
charge += OAK_CHARGE;
}
std::cout << "\n The charge for this sign is $" << charge << std::endl; // <--- Displays 82.00.
// A fair C++ replacement for "system("pause")". Or a way to pause the program.
// The next line may not be needed. 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; // <--- The () are not needed and the return is not required, but makes a good break point.
}
|
Both of you try to avoid using magic numbers in your code, something like
cost += 20.0;
. This may seem trivial now, but the larger your programs become it will be harder to find and change many lines of code and easy to miss one that may become hard to track down. This way there is only one place to make changes and this will affect every variable used in the program.
Since the program is dealing with money and most of your variables are "double"s all should be "doubles". This makes the calculations easier to work with. When giving a "double" a value "35" may work, but "35.0" is the more proper way to write this.
Lines 2 and 24 I added since you are working with a monetary value for your "double"s it makes sure that you use two decimal points in your output. Although these lines could be omitted and the program will work fine. IMHO I think it looks better with these lines in the program.
The next part is the prompts and the "cin" statements. for now they are not needed until you have the program working correctly the you can use them to try different combinations.
Line 35 is where the program starts by setting charge to $35.00 before your if statements add to "charge" if needed.
I left the first "else" statement to show why none of them are needed.
The comments in the code should explain what it is doing.
In the prompts, line 26 for example, if you leave off the "endl" this will put the "cin" on the same line. The space after the "?" is there to make what is displayed look better. See what you think.
Andy