If statements problems

I'm having trouble running my code. I'm new to codding and to the language itself.

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.

Instructions
Ensure the file named HouseSign.cpp is open in the code editor.

You need to declare variables for the following, and initialize them where specified:

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

Write the rest of the program using assignment statements and if statements as appropriate. The output statements are written for you.

Execute the program by clicking the Run button. Your output should be: The charge for this sign is $82.

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>

using namespace std;
int main(){
  
  double minC = 35;
  int charge;
  int letterAmount;
  string woodType;
  string letterColor;
  string gold;
  string Oak;
  double extraC = 4;
  double oakC = 20;
  double goldleafL = 15;



cout << "State the text of color, White, Black, or Gold? " << endl;
cin >> letterColor;
cout << "Enter the number of characters on the sign:  "<<endl;
cin >> letterAmount;
cout << "What type of wood would you like, Pine or Oak? " << endl;
cin >> woodType;
 
if(letterColor=="Gold")
{
charge = minC + goldleafL;
} 
else
{
charge = minC;
}
if(letterAmount > 5 )
{
charge = minC + extraC * (letterAmount - 5);
} 
else 
{
charge = minC;
}
if(woodType == "Oak" )
{
charge = minC + oakC;
}
else
{
charge = minC;
}


cout << "The charge for this sign is $" << charge << endl;

return(0);


}
Last edited on
Your logic for calculating the charge is going wrong.

You should start by setting the charge to 35, and then at each step either add to that charge or leave it as it is.

Instead, you are resetting the charge at every step.

Hello, Repeater

Thanks for the advice but how would that look like?

charge = minC;

then add the if statements?

Does that mean I have to remove all the "charge =" after the if statements?
1
2
3
4
if (something that means charge should increase)
{
  charge = charge + the_increase;
}
Still confused by your instructions..
So, you have a variable. charge. We set it to some value.

int charge = 30;

See how its value is now 30?

Now, you decide that you want to INCREASE that value, by ten. How can you do that? Can you write the code that will increase the value by ten?
AJ24 where you able to get the program to work. I have the same problem? I’m stuck on trying to int color to gold and woodtype to oak?
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
  #include <iostream>
  #include <string>

using namespace std;
int main(){
  
  double cost = 0.0;
  int letterAmount = 8;
  string woodType = "oak";
  string letterColor = "gold";
 



cout << "State the color of text, White, Black, or Gold? " << endl;
cin >> letterColor;
cout << "Enter the number of characters on the sign:  "<<endl;
cin >> letterAmount;
cout << "What type of wood would you like, Pine or Oak? " << endl;
cin >> woodType;
 
cost += 35;

if(letterAmount > 5) {
    letterAmount -= 5;
    cost += (letterAmount * 4);
}

if(woodType == "oak") {
 cost += 20.0;   
}

if(letterColor == "gold") {
 cost += 15.0;   
}


cout << "The charge for this sign is $" << cost << endl;

return(0);


}
awesome thank you.
did you just go off the initial post?
didn't know if you had this homework in school also?
The initial post. I have never been to school beyond high school.
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
You make some good points, but your program example takes away a shoppers choice. What if I don't want oak wood or gold letters?
@Manga,

Not really.

My understanding of the instructions is that the last "cout" statement is given and it should print 82. The code to write is the variables, some initialized, then write the if statements that would produce the 82.

No where in the instructions do I see giving the user a choice, but after getting the program working correctly you can uncomment the prompts and "cin" statements and test the program with other combinations.

Since I was not there when the assignment was given out there could be something missing from the instructions or something that was added later that is not in the instructions.

The example I have shown can be used either way.

Hope that clears up what I did.

Andy
Topic archived. No new replies allowed.