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