Computers are really, really dumb, and need you to hold their metaphorical hand every little step.
A good way to look at it is by reading through the assignment and modifying things one step at a time. For each step you will have some variation of:
• ask the user for input → store it in a variable
• perform some computation using that input
• output the results
Notice these are all separate steps, and they must occur
in order.
Your outputs the charge
before you compute it. Hence the output of zero. Remember, computers can’t look ahead and think! You must do it in order!
The next issue is one of syntax. A semicolon in C++
terminates a statement. Your
if
statements have no body. In general, you should make your
if
statements look like this:
1 2 3 4
|
if (some condition) // ← no semicolon, because the body is a compound
{ // statement (surrounded by curly braces)
do something; // ← semicolons terminate singular statements
}
| |
It helps also to keep questions simple, such as “Oak or Pine?”
Going through the sign charge rubric:
The charge for all signs is a minimum of $35.00. |
1 2 3 4 5 6 7 8 9 10 11 12
|
#include <iostream>
int main()
{
int charge = 35; // all signs cost at least this much
// no input necessary. All signs cost the same (so far)
// no computation necessary. (No input so far)
std::cout << "$" << charge << ".00" << std::endl;
}
| |
You can use
float
if you wish, but here all moneys are integer values, so you can make life a little easier by avoiding dinking with floats. If you wish, though:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <iomanip>
#include <iostream>
int main()
{
float charge = 35.00; // all signs cost at least this much
// no input necessary. All signs cost the same (so far)
// no computation necessary. (No input so far)
std::cout << "$" << std::fixed << std::setprecision(2) << charge << std::endl;
}
| |
Next rubric:
The first five letters or numbers are included in the minimum charge; there is a $4 charge for each additional character. |
So, we must now ask a question: what is to be printed on the sign?
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
|
#include <iostream>
#include <string>
int main()
{
int charge = 35; // all signs cost at least this much
std::cout << "What does the sign say? "; // prompt the user for input
std::string text;
getline( std::cin, text ); // get the input from the user
// computation: count all LETTERS and NUMBERS in the string
int count = 0;
for (char c : text) // look at each character in the input text
{
if (c is a letter or digit)
{
count += 1;
}
}
// if there are at least four letters and numbers then:
// the additional charge is $4 * (number of letters and numbers - 4)
if (count > 4)
{
charge += something goes here;
}
std::cout << "$" << charge << ".00" << std::endl;
}
| |
That was the hardest part, honestly. You can make your life easier by putting
#include <cctype>
in the includes and using the
isalnum(c)
function. Google “isalnum” for examples.
Next rubric:
If the sign is made of oak, add $20.00. No charge is added for pine. |
Your professor has listed all the available wood types for you — oak or pine. Ask the question:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
int main()
{
int charge = 35;
// ...all the stuff you already wrote goes here...
std::cout << "Oak or Pine? "; // prompt user for input
std::string wood_type;
getline( std::cin, wood_type ); // get user's input
if (wood_type == "oak")
{
charge += 20;
}
std::cout << "$" << charge << ".00" << std::endl;
}
| |
And so on.
Each time you make a modification, try compiling and running your program and giving it a variety of inputs.
Hope this helps.