r/cpp_questions•
jwbcoon

Unexpected logic error in my program, I'm pretty lost on what my mistake could be. Thank for any help! :)

SOLVED

Details: I'm compiling C++11 (I think) code on Code::Blocks

Program Intention: My homework assignment is to make a program that calculates expense overtime for the user's choice of an ISP subscription and time frame. I must use the "switch" object, but it seems necessary anyways.

Error: The first choice, Package A, (case 1 in the switch group thing) works exactly as intended, and it functions the same way as Package B (case 2). However, when I follow Package B's branch of the program, the math comes out wrong. Here is the code:

//declare variables
int choice,
  hours,hourpkA,hourpkB;

double packA=9.95,packB=14.95,packC=19.95,         //base prices
       ovrateA=11.95,ovrateB=15.95,overageA,overageB,   //overage charges
       subCost;                                   //subscription cost

//receive input
cout<<"\nSelect a subscription package:"<<endl;
cout<<"****************"<<endl;
cout<<"* 1. Package A *"<<endl;
cout<<"* 2. Package B *"<<endl;
cout<<"* 3. Package C *"<<endl;
cout<<"* 4. Quit      *"<<endl;
cout<<"****************"<<endl;
cin>>choice;
cout<<"\nHow many hours were used? ";
cin>>hours;
cout<<fixed<<setprecision(2);

//determine hourly charges
hourpkA=hours;
hourpkB=hours;
overageA=0;
overageB=0;

if (hourpkA>10)
    packA=ovrateA,
    overageA=99.50,
    hours=(hours-10);
if (hourpkB>20)
    packB=ovrateB,
    overageB=149.50,
    hours=(hours-20);

//switch statement and output

switch (choice)
{
case(1):subCost=hours*packA+overageA;
    cout<<"\nThe total amount due would be $"<<subCost<<endl;
    break;
case(2):subCost=hours*packB+overageB;
    cout<<"\nThe total amount due would be $"<<subCost<<endl;
    break;
case(3):subCost=hours*packC;
    cout<<"\nThe total amount due would be $"<<subCost<<endl;
    break;
default:
    cout<<"\nHave a nice day :)"<<endl;
}

return 0;

}

I'm sorry to upload so much, but I'm not sure which parts are important and which aren't. To clear up some possible ambiguities, the subscription rates increase after 10 hours for Package A and after 20 for Package B. The calculations for Package B are correct for the inputs of 1-10 hours, then inputs 10-20 seem to repeat the calculations for inputs 1-10. This baffles me, any help is appreciated!

Extra Note: I'm also supposed to limit the "hours" input between 0-744 hours. I haven't tried my hand at that yet, but if, upon looking at my code, you see that I've already prevented myself from being able to do so, please let me know. Otherwise I'll come back here if I really need help :P

Posted Feb 17, 2017, 3:45 AM UTC

03
Loading comments…