help - named cosntants



Ok I am having trouble with a program I am writing for class. It has to do with constants.

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
#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;

int main ()
{
int salePrice,
comAmt;
const double COM_RATE;?
bool validCode = true; b=
char propCode; 

cout << "Enter the property's selling price:\t$ ";
cin >> salePrice;

cout << endl << "Enter the property code (R,M,C):\t";
cin >> propCode;

propCode = toupper(propCode);

if (propCode == 'R')
{
const double COM_RATE = .061; 
}
else if (propCode == 'M')
{
const double COM_RATE = .052;
}
else if (propCode == 'C')
{
const double COM_RATE = .045;
}
else
{
validCode = false;
}
if (validCode)
{
cout << fixed << setprecision(2);
comAmt = salePrice * COM_RATE;
cout << endl << "$ " <<comAmt << endl;
}
else
{
cout << endl << "Error, your code is invalid" << endl << endl;
}


Here is my problem with the code. It wont build. I have been taught to use named constants like this:

(EXAMPLE)

const double COM_RATE = 0.2;

cout << "What is the rate";
cin >> COM_RATE;

I Have been taught to intitalize it first at the top and then use it later on. However, my professor wants us to use a const double where I have highlighted under the if statements, but it just wont work because I dont know what to put at the top for my COM_RATE where I have declared my other variables. With a named constant you cannot declare and then initialize, you have intitalize at once. The problem, however, is that I have already intialized it under the if statement but its still telling me that I need to decalre it in the block of code where I have decalred my other variables at the top. Help please??

Sorry If I am not explaining this in the best way. I know how to use a const double, it's just that my professor wants me to use it in a way that I have never used it before and it is giving me errors.
Last edited on
Please put your code between code tags. They are under format and look liket his <>.

http://www.cplusplus.com/articles/jEywvCM9/
Is this ok? I dont know why it divided my code into two sides.
You must have "---" at the start of a line for some reason. That causes what follows to be displayed on the right. That is normally used for displaying program output.

The problem here is that COM_RATE at line 10L and lines 14R, 18R, 22R are in different scopes. COM_RATE at lines 14R, 18R, and 22R immediately go out of scope when the } of the if block is encountered.

Frankly, I think this is a stupid way to do this, but if it's what your professor wants, then you need to move line 31R inside each of the if blocks so the calculation with COM_RATE is done inside the if block, then COM_RATE can go out of scope. There is no need for line 10L

Last edited on
My professor actually only wants calculations and couts done in line 31/32. However, I dont know if I am doing the const doubles correctly. All I know is that my professor wants me to use named constants for my COM_RATE. I dont know, however, if I am using them correctly. Basically what I am trying to say is that everything is correct, according to my professor, except the way I am using const double COM_RATE.
I can't read you professors mind, so i don't know what point he is trying to make here, or if you're accurately conveying what he said. In any case, you can't have COM_RATE in a local scope and then later use COM_RATE which is in a global scope and expect it to have any bearing on a local variable which has gone out of scope.

The traditional way to code this type of problem is to do the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const double COM_RATE_R = .061;
const double COM_RATE_M = .052;
const double COM_RATE_C = .045;
double rate;
...
if (propCode == 'R')
{  rate = COM_RATE_R;
}
else if (propCode == 'M')
{  rate = COM_RATE_M;
}
else if (propCode == 'C')
{  rate = COM_RATE_C;
}
...
comAmt = salePrice * rate;




Last edited on
Thank you. This helped me immensely. What I failed to do was declare a rate variable and then use that to set the value of COM_RATE_R and so on. I never thought of that, surprisingly.
Topic archived. No new replies allowed.