d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Ugh Logic. > Need Help With Logical Error.
Add Reply New Topic New Poll
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Nov 24 2014 12:07am
This section of the program is supposed to compare the customer's bill to another plan. If another plan would save five dollars or greater; suggest plan

Code
float calc3(int customerMinutes, float customerBill)
{
float planA = 39.99, planB = 59.99, planC = 69.00;
if (customerMinutes >= 451)
{
planA = (((customerMinutes - 450) * 0.45) + 39.99);
}
if (customerMinutes >= 901)
{
planB = (((customerMinutes - 900) * 0.40) + 59.99);
}
if (customerMinutes <= 461)
{
if (customerBill <= planA + 5)
//if (planA + 5 <= customerBill)
{
std::cout << "\n\nPlan A is a best value.\n";
// std::cout << "Plan A price = " << planA << std::endl;
}
else
{
std::cout << "\n\nPlan B is a best value.\n";
//std::cout << "Plan B price = " << planB << std::endl;
}
}
else if (customerMinutes >= 462 && customerMinutes <= 912)
{
if (customerBill <= planB + 5)
//if (planB + 5 <= customerBill)
{
std::cout << "\n\nPlan B is a best value.\n";
//std::cout << "Customer Bill = " << customerBill << std::endl;
//std::cout << "Plan B price = " << planB << std::endl;
}
else
{
std::cout << "\n\nPlan C is the best deal.\n";
//std::cout << "Customer Bill = " << customerBill << std::endl;
//std::cout << "Plan C price = " << planC << std::endl;
//std::cout << "Plan B price = " << planB << std::endl;
}
}
else if (customerMinutes >= 913)
{
std::cout << "\n\nPlan C is the best value.\n";
//std::cout << "Plan C price = " << planC <<std::endl;
}
}


I've tried different approaches to find out where the error is but one way works one way, another - another. I can't get it to work correctly in all scenarios.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 24 2014 12:14am
make it more generic. imagine if you had 4 or 5 plans instead of just 3. your comparisons would be a mess to maintain.

1) create one function* that calculates cost of a specific plan
2) add each plan and its corresponding cost to an array.**
3) remove current plan from array. store in variable
4) find the lowest cost plan in the array
5) use your threshold to compare results from #3 and #4 to make a recommendation

i'm assuming, of course, that your array holds <plan, cost> as opposed to just <cost>

* you can use a more OOP design instead of a function, but i dont wanna overwhelm you. just farm out the functions (eg: calculatePlanA, calculatePlanB, etc) and wrap with a single function calculatePlan to make it easier
** technically you don't need an array, but it makes it conceptually simpler.

This post was edited by carteblanche on Nov 24 2014 12:25am
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 24 2014 12:25am
Each time you post a code snippet...that's all you do. You post a code snippet, and leave the rest to our interpretation. How do you expect us to help if you can't even tell us what the problem is?

The only reason carteblanche was even able to offer you advice is because he for some reason took the type to decipher your code. It's admirable.

Why does your function return a float when you are just printing to console? In fact, you don't even have return statements meaning this code won't even compile. So, I am going to say that is your problem. You have syntax errors that need to be fixed.

Once you get your code compiling, if you would like to discuss a specific issue, I am all ears.

But just a little advice: of all the code you have posted there is one thing that remains constant; your lack of design. It seems as if you just start typing into your IDE and hope that a solution will emerge the longer you go at it. And it shows in the jumble you produce.
You need to take a step back and think about your problem, and then tackle it.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 24 2014 12:47am
Quote (Minkomonster @ Nov 24 2014 01:25am)

The only reason carteblanche was even able to offer you advice is because he for some reason took the type to decipher your code. It's admirable.


actually, i saw the topic title and assumed it was the same problem he pm'd me about yesterday. didnt bother reading his code. i ended up pm'ing myself a bunch of times to fill up my box just so he'd post instead of pm me.
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Nov 24 2014 01:01am
fake edit: I've been trying to post it. Either this One Plus One or Windows 8.1 networking is fucking up. I had to log onto free wifi to post (out eating food). :( Also there is a possessed old lady here and it's creeping me out. :ph34r:

The problem I have is the program doesn't fully encompass all parameters.

Run this program using parameters: [ A for plan A. Choose any month. 800 for the number of minutes used. ] Since the minutes are less that 900, the program should suggest Plan B. I need to take a step back I think and rethink things.




Code
// 17-NOV-2014

#include <iostream>
#include <iomanip>
#include <string>

void text1(), text2(char, int, float), calc3(int, float);
int calc1(int), switch1(), switch2();
float calc2(char, int);



int main()
{
std::cout << std::fixed << std::setw(3) << std::setprecision(2);
int planMonth, totalMinutes;
float bill;
char plan;

text1();
plan = switch1(); // Selected plan.
planMonth = switch2(); // The month of the bill cycle.
totalMinutes = calc1(planMonth); // Minutes used in bill cycle.
bill = calc2(plan, totalMinutes); // Total cost of bill.
text2(plan, totalMinutes, bill);
calc3(totalMinutes, bill);

return 0;
}

void text1()
{
std::cout << "You can use this program to determine if there is a more cost effective cell\nphone plan available. ";
std::cout << "The current plans available are as follow.\n\n";
std::cout << "\tPlan A - $39.99 per month; 450 minutes provided.\n\tAdditional minutes are $0.45 per minute.\n\n";
std::cout << "\tPlan B - $59.99 per month; 900 minutes provided.\n\tAdditional minutes are $0.40 per minute.\n\n";
std::cout << "\tPlan C - $69.00 per month; unlimited minutes provided.\n\n";
std::cout << "Now we will collect data from you regarding your usage.\n";
std::cout << "Please select the current plan you have by typing: A for plan A, B for plan B,\nand C for plan C, ";
std::cout << "followed by the 'enter/return' key.\n\n";
}

int switch1()
{
bool valid = true;
char planChoice;
do
{
char customerPlan;
std::cin >> customerPlan;
std::cout << "\nYou entered plan: " << customerPlan << std::endl << std::endl;
switch(customerPlan)
{
case 'a':
case 'A':
std::cout << "\tPlan A - $39.99 per month; 450 minutes provided.\n\tAdditional minutes are $0.45 per minute.\n\n";
planChoice = 'A';
valid = true;
break;
case 'b':
case 'B':
std::cout << "\tPlan B - $59.99 per month; 900 minutes provided.\n\tAdditional minutes are $0.40 per minute.\n\n";
planChoice = 'B';
valid = true;
break;
case 'c':
case 'C':
std::cout << "\tPlan C - $69.00 per month; unlimited minutes provided.\n\n";
planChoice = 'C';
valid = true;
break;
default:
std::cout << "INVALID INPUT!! Please select a correct plan: A, B, or C.\n";
valid = false;
break;
}
}
while (!valid);
return planChoice;
}

int switch2()
{
bool valid = true;
int month;
do
{
std::cout << "Please select the month of this bill cycle by entering the numerical month\n";
std::cout << "and pressing the return key.\n\n";
std::cout << "\t1 for January\n";
std::cout << "\t2 for February\n";
std::cout << "\t3 for March\n";
std::cout << "\t4 for April\n";
std::cout << "\t5 for May\n";
std::cout << "\t6 for June\n";
std::cout << "\t7 for July\n";
std::cout << "\t8 for August\n";
std::cout << "\t9 for September\n";
std::cout << "\t10 for October\n";
std::cout << "\t11 for November\n";
std::cout << "\t12 for December\n";

int mon;
std::cin >> mon;
switch(mon)
{
case 1:
std::cout << "\nJanuary\n";
month = 31;
valid = true;
break;
case 2:
std::cout << "\nFebruary\n";
month = 28;
valid = true;
break;
case 3:
std::cout << "\nMarch\n";
month = 31;
valid = true;
break;
case 4:
std::cout << "\nApril\n";
month = 30;
valid = true;
break;
case 5:
std::cout << "\nMay\n";
month = 31;
valid = true;
break;
case 6:
std::cout << "\nJune\n";
month = 30;
valid = true;
break;
case 7:
std::cout << "\nJuly\n";
month = 31;
valid = true;
break;
case 8:
std::cout << "\nAugust\n";
month = 31;
valid = true;
break;
case 9:
std::cout << "\nSeptember\n";
month = 30;
valid = true;
break;
case 10:
std::cout << "\nOctober\n";
month = 31;
valid = true;
break;
case 11:
std::cout << "\nNovember\n";
month = 30;
valid = true;
break;
case 12:
std::cout << "\nDecember\n";
month = 31;
valid = true;
break;
default:
std::cout << "\nINVALID INPUT!! Please select 1 - 12.\n";
valid = false;
break;
}
}
while(!valid);
return month;
}

int calc1(int month)
{
bool valid = true;
int billMinutes, monthMinutes, dayMinutes = 1440; // Number of minutes in a 24 hour day.
monthMinutes = (month * dayMinutes);
do
{
std::cout << "\nNow enter the total amount of minutes used during your previous months' bill.\n";
std::cin >> billMinutes;
if (billMinutes > monthMinutes || billMinutes < 0)
{
std::cout << "\nINVALID INPUT!! The maximum minutes for the month select is: " << monthMinutes << std::endl;
std::cout << "Please enter a value within the range of 0 through " << monthMinutes << "." <<std::endl;
valid = false;
}
else
{
valid = true;
}
}
while (!valid);
return billMinutes;
}

float calc2(char planLetter, int minutes)
{
int overageMinutes;
float totalFee, overageFee = 0;
if (planLetter == 'A')
{
if (minutes >= 451)
{
overageMinutes = (minutes - 450);
overageFee = (overageMinutes * 0.45);
}
totalFee = (overageFee + 39.99);
}
else if (planLetter == 'B')
{
if (minutes >= 901)
{
overageMinutes = (minutes - 900);
overageFee = (overageMinutes * 0.40);
}
totalFee = (overageFee + 59.99);
}
else if (planLetter == 'C')
{
totalFee = 69.00;
}
return totalFee;
}

void text2(char plan1, int totalMinutes1, float bill1)
{
std::string planString;
if (plan1 == 'A')
{
planString = "\nPlan A: \t$39.99 per month; 450 minutes provided. \n\t\tAdditional minutes are $0.45 per minute.\n";
}
else if (plan1 == 'B')
{
planString = "\nPlan B - $59.99 per month; 900 minutes provided.\nAdditional minutes are $0.40 per minute.\n";
}
else if (plan1 == 'C')
{
planString = "\nPlan C - $69.00 per month; unlimited minutes provided.\n";
}
std::cout << planString << "Minutes:\t" << totalMinutes1 << "\nBill:\t\t$" << bill1;
}

void calc3(int customerMinutes, float customerBill)
{
float planA = 39.99, planB = 59.99, planC = 69.00;
if (customerMinutes >= 451)
{
planA = (((customerMinutes - 450) * 0.45) + 39.99);
}
if (customerMinutes >= 901)
{
planB = (((customerMinutes - 900) * 0.40) + 59.99);
}
if (customerMinutes <= 461)
{
if (customerBill <= planA + 5)
//if (planA + 5 <= customerBill)
{
std::cout << "\n\nPlan A is a best value.\n";
std::cout << "Plan A price = " << planA << std::endl;
}
else
{
std::cout << "\n\nPlan B is a best value.\n";
std::cout << "Plan B price = " << planB << std::endl;
}
}
else if (customerMinutes >= 462 && customerMinutes <= 912)
{
if (customerBill <= planB + 5)
//if (planB + 5 <= customerBill)
{
std::cout << "\n\nPlan B is a best value.\n";
//std::cout << "Customer Bill = " << customerBill << std::endl;
//std::cout << "Plan B price = " << planB << std::endl;
}
else
{
std::cout << "\n\nPlan C is the best deal.\n";
//std::cout << "Customer Bill = " << customerBill << std::endl;
//std::cout << "Plan C price = " << planC << std::endl;
//std::cout << "Plan B price = " << planB << std::endl;
}
}
else if (customerMinutes >= 913)
{
std::cout << "\n\nPlan C is the best value.\n";
//std::cout << "Plan C price = " << planC <<std::endl;
}
}


This post was edited by NinjaSushi2 on Nov 24 2014 01:10am
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Nov 24 2014 01:13am
I haven't fully studied arrays but I agree it would be better. The program doesn't even call for functions yet because this is only chapter 4 in my book. I did also plan out my pseudo code but I am stuck on this one spot. After that I am done with this program.
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Nov 24 2014 08:55am
I think I was getting a little too hooah. :lol:

I rewrote the code in five minutes. It's simple, logical, and preforms the task requires.

Code
void calc3 (int customerMinutes, float customerBill)
{
float customerSavings, planA = 39.99, planB = 59.99, planC = 69;
if (customerMinutes >= 0 && customerMinutes <= 461)
{
std::cout << "\n\nPlan A is the best plan. $39.99 per month; 450 minutes provided.\n";
customerSavings = (customerBill - planA);
std::cout << "You would save $" << customerSavings << std::endl;
}
else if (customerMinutes >= 462 && customerMinutes <= 912)
{
std::cout << "\n\nPlan B is the best plan. $59.99 per month; 900 minutes provided.\n";
customerSavings = (customerBill - planB);
std::cout << "You would save $" << customerSavings << std::endl;
}
else
{
std::cout << "\n\nPlan C is the best plan. $69.00 per month; unlimited minutes provided.\n";
customerSavings = (customerBill - planC);
std::cout << "You would save $" << customerSavings << std::endl;
}
}


I was totally over thinking things. Thank God for McDonald's coffee.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll