Percentage calculated
I wrote this code:
std::cout << "When did you graduate? "; int graduation_year = 0; std::cin >> graduation_year; std::cout << "\n"; std::cout << "How much is your student debt? "; double debt = 0; std::cin >> debt; std::cout << "You graduated in " << graduation_year << " and have student debt worth " << debt << ".\n"; double discount = 0; switch (graduation_year) { case 2010: { if (debt >= 5000 && debt < 10000) double discount = .99; double payment = debt * discount; std::cout << "Your student debt is between 5000 and 10000 which means your payment would be " << payment << "\n"; } break; This is not for the school assignment, I'm just trying to learn C ++ and trying to get percentages and switch / case.
Annoying when I change this part
double discount = .99; double payment = debt * discount; to
double payment = debt * 0.99; works great. Therefore, I feel that something may go wrong, because the double being is <1, but I cannot understand what it is for my life. The code continues with 2011 code, etc., but it gives the same problems for this part of the code, so I decided that I would leave it.
You re-declare the discount as an internal variable in the next if-statement block
if (debt >= 5000 && debt < 10000) double discount = .99; You should write it like this:
if (debt >= 5000 && debt < 10000) discount = .99; ETA: a little explanation.
The discount declaration in the if block temporarily hides the global discount declaration. Although the value is correctly assigned to the internal discount declaration when you exit the if-block, this variable is beyond the scope , and any further reference to the discount is resolved using the global discount declaration. Since you have not changed the global variable, you will not get the correct result.