Trouble with constants

0 Bawer Sultan · February 25, 2015
Ok I am having trouble with a program I am writing for class. It has to do with constants. 

#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;

int main ()
{
int salePrice,
comAmt;
const double COM_RATE;?
bool validCode = true;
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 statement, 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??

Post a Reply

Replies

Oldest  Newest  Rating
0 c student · February 25, 2015
perhaps you could try to design a function to test propCode and then return the result into your const variable?
0 Bawer Sultan · February 25, 2015
 Sadly, I have to follow by the rules.  I have to set the const variable to the numbers 0.61,0.52, ect. My professor gave me this assigment.  I have to use if else and all the other good stuff. Basically, everything that you see (codes) are correct and cant be changed except for the fact that I dont know what I am doing when in regrards to the const double. Everything is correct except the const doubles. They are giving me errors. 
0 c student · February 26, 2015
i'm not familiar with c++, but i think the problem with your main code is that when you declare a variable within an if statement, it is restricted within that scope and when you try to use it outside, it won't exist.  i'm guessing the problem in your example code is that you can't use cin with a const variable.  maybe???
0 Bawer Sultan · February 26, 2015
Thank you. I noticed that I also forget to add in another non constant double variable.
  • 1

C++

106,942 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator