Difficulty with "if statements" homework

I'm struggling hard with an exercise in Cengage for my Intro to Programming class. Professor hasn't gotten back to me for assistance (it's an online class) and hasn't graded my homework from last week to let me know what I was doing wrong at that point, so I'm not asking anyone to do my homework for me but I could really use a push in the right direction.

Here's the assignment:
"In this lab, you complete a prewritten C++ program for a carpenter who creates personalized house signs. The program is supposed to compute the price of any sign a customer orders, based on the following facts:

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."

Here's my work so far:

// HouseSign.cpp - This program calculates prices for custom made signs.

#include <iostream>
#include <string>
using namespace std;


int main()
{
// This is the work done in the housekeeping() function
// Declare and initialize variables here
// Charge for this sign
float charge = 0.00;
// Color of characters in sign
string color;
string gold;
// Number of characters in sign
int numChars = 8;
// Type of wood
string woodType;
string oak;
int colorCharge = 15;
int woodCharge = 20;
int addCharge = 12;
int signCharge = 35;

// This is the work done in the detailLoop() function

// Write assignment and if statements here

if (numChars > 5);
cout << "The additional charge for the characters is " << addCharge << endl;
addCharge = (numChars - 5) * 4;


if (woodType == oak);
cout << "The additional charge for the wood type is " << woodCharge << endl;
woodCharge = 20;

if (color == gold);
cout << "The additional charge for the lettering color is " << colorCharge << endl;
colorCharge = 15;

// This is the work done in the endOfJob() function
// Output charge for this sign
cout << "The total charge for this sign is " << charge << endl;
charge = signCharge + colorCharge + woodCharge + addCharge;
return 0;
}

Whenever I run it, I just keep getting 0 for the total charge at the end. Any help is appreciated!
First things first:
Please learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either

Your code formatted and code-tagged:
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
// HouseSign.cpp - This program calculates prices for custom made signs.

#include <iostream>
#include <string>
using namespace std;


int main()
{
   // This is the work done in the housekeeping() function
   // Declare and initialize variables here
   // Charge for this sign
   float charge = 0.00;
   // Color of characters in sign
   string color;
   string gold;
   // Number of characters in sign
   int numChars = 8;
   // Type of wood
   string woodType;
   string oak;
   int colorCharge = 15;
   int woodCharge = 20;
   int addCharge = 12;
   int signCharge = 35;

   // This is the work done in the detailLoop() function

   // Write assignment and if statements here

   if (numChars > 5);
   cout << "The additional charge for the characters is " << addCharge << endl;
   addCharge = (numChars - 5) * 4;


   if (woodType == oak);
   cout << "The additional charge for the wood type is " << woodCharge << endl;
   woodCharge = 20;

   if (color == gold);
   cout << "The additional charge for the lettering color is " << colorCharge << endl;
   colorCharge = 15;

   // This is the work done in the endOfJob() function
   // Output charge for this sign
   cout << "The total charge for this sign is " << charge << endl;
   charge = signCharge + colorCharge + woodCharge + addCharge;
   return 0;
}

Line 47, you are calculating the total charges AFTER you print out the charges at line 46. Ooops!

Swap the lines.

Plus, all your if statement have a semi-colon (;) after them. That terminates the ifs. Remove the semi-colons.

It looks as if you want two statements to be executed when an if is true. Without curly braces { } enclosing the statements only the first statement after the if is executed. The second statement is ALWAYS executed no matter what the if evaluates to.

Fixing the logic and syntax errors:
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
// HouseSign.cpp - This program calculates prices for custom made signs.

#include <iostream>
#include <string>
using namespace std;


int main()
{
   // This is the work done in the housekeeping() function
   // Declare and initialize variables here
   // Charge for this sign
   float charge = 0.00;
   // Color of characters in sign
   string color;
   string gold;
   // Number of characters in sign
   int numChars = 8;
   // Type of wood
   string woodType;
   string oak;
   int colorCharge = 15;
   int woodCharge = 20;
   int addCharge = 12;
   int signCharge = 35;

   // This is the work done in the detailLoop() function

   // Write assignment and if statements here

   if (numChars > 5)
   {
      cout << "The additional charge for the characters is " << addCharge << endl;
      addCharge = (numChars - 5) * 4;
   }

   if (woodType == oak)
   {
      cout << "The additional charge for the wood type is " << woodCharge << endl;
      woodCharge = 20;
   }

   if (color == gold)
   {
      cout << "The additional charge for the lettering color is " << colorCharge << endl;
      colorCharge = 15;
   }

   // This is the work done in the endOfJob() function
   // Output charge for this sign
   charge = signCharge + colorCharge + woodCharge + addCharge;
   cout << "The total charge for this sign is " << charge << endl;
   return 0;
}
The additional charge for the characters is 12
The additional charge for the wood type is 20
The additional charge for the lettering color is 15
The total charge for this sign is 82

There is a lot of input logic that is missing, such as getting the actual letters for the sign, whether the customer wants regular or gold lettering, what type of wood is wanted.

At least now you have a working bit of code, even if it isn't complete "complete."
Last edited on
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.
@Duthomas, I suspect all the "get input from the customer" code in some form is in a missing function, the comment at line 27.
// This is the work done in the detailLoop() function

The instructor provided prewritten incomplete code the students have to complete.
Thank you so much! I rewrote a lot of it with more "hand-holding" and it works now.
Post your completed code and we could give some "better ways to code" advise. :)
Topic archived. No new replies allowed.