While Loop with if statements
Sep 21, 2013 at 4:20pm UTC
I haven't compiled this yet but I'm wondering if I did this right from what you can tell me and if you see any glaring problems with it. The assignment said to
displayBetterPlans() function that calls calcPlanCharge (plan) for each of the plans, compares the charges and displays each of the plans that would have been a better choice for that month based on the customer usage and, in addition, it displays the charge under that plan and the savings the customer would have received. Use a while or for loop to do this task. Obviously, you will also have to use if statement(s).
This is just the section of code I'm worried about, will it produce an infinite loop??
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
double InternetCharge::calcPlanCharge(int plan)
{
switch (plan)
{
case 1:
if (minutes > 6000)
{
if (data > 5)
{
charge = 20 + (minutes - 6000)*1.00 + (data - 5)*10;
}
else
{
charge = 20 + (minutes - 6000)*1.00;
}
}
else
{
if (data > 5)
{
charge = 20 + (data - 5)*10;
}
else
{
charge = 20;
}
}
break ;
case 2:
if (data > 5)
{
charge = 50 + (data - 5)*10;
}
else
{
charge = 50;
}
break ;
case 3:
if (minutes > 9000)
{
charge = 80 + (minutes - 9000)*1;
}
else
{
charge = 80;
}
break ;
case 4:
charge = 120;
break ;
default :
cout << "Plan number not valid" ;
}
return charge;
}
//Output Functions
void InternetCharge::displayBetterPlans()
{
double alt = 0.0;
double savings = 0.0;
calcPlanCharge(1);
calcPlanCharge(2);
calcPlanCharge(3);
calcPlanCharge(4);
cout << "Plans that would have saved the customer money: " ;
while (plan == 1)
{
if (charge > calcPlanCharge(2))
{
alt = calcPlanCharge(2);
savings = charge - alt;
cout << "Plan 2: Charge: $" << alt << " Savings: $" << savings << endl;
}
else if (charge > calcPlanCharge(3))
{
alt = calcPlanCharge(3);
savings = charge - alt;
cout << "Plan 3: Charge: $" << alt << " Savings: $" << savings << endl;
}
else if (charge > calcPlanCharge(4))
{
alt = calcPlanCharge(4);
savings = charge - alt;
cout << "Plan 4: Charge: $" << alt << " Savings: $" << savings << endl;
}
}
while (plan == 2)
{
if (charge > calcPlanCharge(1))
{
alt = calcPlanCharge(1);
savings = charge - alt;
cout << "Plan 1: Charge: $" << alt << " Savings: $" << savings << endl;
}
else if (charge > calcPlanCharge(3))
{
alt = calcPlanCharge(3);
savings = charge - alt;
cout << "Plan 3: Charge: $" << alt << " Savings: $" << savings << endl;
}
else if (charge > calcPlanCharge(4))
{
alt = calcPlanCharge(4);
savings = charge - alt;
cout << "Plan 4: Charge: $" << alt << " Savings: $" << savings << endl;
}
}
while (plan == 3)
{
if (charge > calcPlanCharge(1))
{
alt = calcPlanCharge(1);
savings = charge - alt;
cout << "Plan 1: Charge: $" << alt << " Savings: $" << savings << endl;
}
else if (charge > calcPlanCharge(2))
{
alt = calcPlanCharge(2);
savings = charge - alt;
cout << "Plan 2: Charge: $" << alt << " Savings: $" << savings << endl;
}
else if (charge > calcPlanCharge(4))
{
alt = calcPlanCharge(4);
savings = charge - alt;
cout << "Plan 4: Charge: $" << alt << " Savings: $" << savings << endl;
}
}
while (plan == 4)
{
if (charge > calcPlanCharge(1))
{
alt = calcPlanCharge(1);
savings = charge - alt;
cout << "Plan 1: Charge: $" << alt << " Savings: $" << savings << endl;
}
else if (charge > calcPlanCharge(2))
{
alt = calcPlanCharge(2);
savings = charge - alt;
cout << "Plan 2: Charge: $" << alt << " Savings: $" << savings << endl;
}
else if (charge > calcPlanCharge(3))
{
alt = calcPlanCharge(3);
savings = charge - alt;
cout << "Plan 3: Charge: $" << alt << " Savings: $" << savings << endl;
}
}
}
Last edited on Sep 21, 2013 at 4:22pm UTC
Sep 21, 2013 at 4:42pm UTC
Alrighty I've compiled it, but it just displays
"Plans that would have saved the customer money: "
And nothing else...
Sep 21, 2013 at 5:11pm UTC
Ok so now it's showing things but in an infinite loop! How do I stop this?? Here is the code so far:
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
void InternetCharge::displayBetterPlans()
{
double alt = 0.0;
double savings = 0.0;
cout << "Plans that would have saved the customer money: " << endl;
while (plan == 1)
{
if (calcPlanCharge(plan) > calcPlanCharge(2))
{
alt = calcPlanCharge(2);
savings = calcPlanCharge(plan) - alt;
cout << "Plan 2: Charge: $" << alt << " Savings: $" << savings << endl;
}
else if (calcPlanCharge(plan) > calcPlanCharge(3))
{
alt = calcPlanCharge(3);
savings = calcPlanCharge(plan) - alt;
n = n + 1;
cout << "Plan 3: Charge: $" << alt << " Savings: $" << savings << endl;
}
else if (calcPlanCharge(plan) > calcPlanCharge(4))
{
alt = calcPlanCharge(4);
savings = calcPlanCharge(plan) - alt;
n = n + 1;
cout << "Plan 4: Charge: $" << alt << " Savings: $" << savings << endl;
}
}
while (plan == 2)
{
if (calcPlanCharge(plan) > calcPlanCharge(1))
{
alt = calcPlanCharge(1);
savings = calcPlanCharge(plan) - alt;
cout << "Plan 1: Charge: $" << alt << " Savings: $" << savings << endl;
}
else if (calcPlanCharge(plan) > calcPlanCharge(3))
{
alt = calcPlanCharge(3);
savings = calcPlanCharge(plan) - alt;
cout << "Plan 3: Charge: $" << alt << " Savings: $" << savings << endl;
}
else if (calcPlanCharge(plan) > calcPlanCharge(4))
{
alt = calcPlanCharge(4);
savings = calcPlanCharge(plan) - alt;
cout << "Plan 4: Charge: $" << alt << " Savings: $" << savings << endl;
}
}
while (plan == 3)
{
if (calcPlanCharge(plan) > calcPlanCharge(1))
{
alt = calcPlanCharge(1);
savings = calcPlanCharge(plan) - alt;
cout << "Plan 1: Charge: $" << alt << " Savings: $" << savings << endl;
}
else if (calcPlanCharge(plan) > calcPlanCharge(2))
{
alt = calcPlanCharge(2);
savings = calcPlanCharge(plan) - alt;
cout << "Plan 2: Charge: $" << alt << " Savings: $" << savings << endl;
}
else if (calcPlanCharge(plan) > calcPlanCharge(4))
{
alt = calcPlanCharge(4);
savings = calcPlanCharge(plan) - alt;
cout << "Plan 4: Charge: $" << alt << " Savings: $" << savings << endl;
}
}
while (plan == 4)
{
if (calcPlanCharge(plan) > calcPlanCharge(1))
{
alt = calcPlanCharge(1);
savings = calcPlanCharge(plan) - alt;
cout << "Plan 1: Charge: $" << alt << " Savings: $" << savings << endl;
}
else if (calcPlanCharge(plan) > calcPlanCharge(2))
{
alt = calcPlanCharge(2);
savings = calcPlanCharge(plan) - alt;
cout << "Plan 2: Charge: $" << alt << " Savings: $" << savings << endl;
}
else if (calcPlanCharge(plan) > calcPlanCharge(3))
{
alt = calcPlanCharge(3);
savings = calcPlanCharge(plan) - alt;
cout << "Plan 3: Charge: $" << alt << " Savings: $" << savings << endl;
}
}
}
Sep 21, 2013 at 5:38pm UTC
Figured it out! Now I just gotta figure out why I'm off by .50 every time and I'll be peachy keen.
Topic archived. No new replies allowed.