jeudi 2 novembre 2017

If-Else nest redundant?

Currently, I am taking a C++ course at my local college, and was given a debugging assignment. In the instructions for this assignment, I was told that the only thing really wrong with this code, is that the conditions for the nested if-else-if statements on lines 82-89 are redundant, however, I cannot see another way to get the same results without having those conditions stay the same...any tips or such would be greatly appreciated!

#include <iostream>
#include <conio.h>
#include <iomanip>

using namespace std;

const int   BASE_COST = 10;
const int   LOW_LIMIT = 20;
const int   MID_LIMIT = 40;
const int   HIGH_LIMIT = 60;
const double    LOW_CHECKS = .10;
const double    MIDLOW_CHECKS = .08;
const double    MIDHIGH_CHECKS = .06;
const double    HIGH_CHECKS = .04;

int main()
{
int numOfChecks;
double multiplierValue;
double totalFee;

cout << fixed << showpoint;
cout << setprecision(2);
cout << "Please enter the number of checks you used this month: ";
cin >> numOfChecks;

if(numOfChecks < 0)
{
    cout << "Number of checks can't be negative. Program ends.\n";
    exit(1);    //terminate the program with error code 1
}

//the following line runs only if the program did not terminate, so start over if-else
if(numOfChecks < LOW_LIMIT)
    multiplierValue = LOW_CHECKS;
else if(numOfChecks < MID_LIMIT)
    multiplierValue = MIDLOW_CHECKS;
else if(numOfChecks >= MID_LIMIT && numOfChecks < HIGH_LIMIT)
    multiplierValue = MIDHIGH_CHECKS;
else if (numOfChecks >= HIGH_LIMIT)
    multiplierValue = HIGH_CHECKS;  

totalFee = BASE_COST + numOfChecks * multiplierValue;

cout << "Your total for this month is $" << totalFee;

_getch();
return 0;
}

Aucun commentaire:

Enregistrer un commentaire