@KORNFREDY
How do I do keep this program running until the users presses E to exit. |
Here is one way to do that. I had tho add in a couple do/while loops and such. Of course, the program would look a lot better, and be easier to follow, if you used the switch command.
I changed the dollar amounts you had hard coded in the program. This way, if the amounts changed in the plans, you only have to change the three listed in your constants, otherwise, you would have to search each line and change the dollar values listed or you will receive incorrect results.
I also made the input to always be uppercase, so you don't have to check both upper and lowers.
One last thing, use the code format button on the right side. ( They look like <> )
After pasting in your code, highlight it, and click on the format button. As you can see with the code below, it's easier to read, and comment on, since you see line numbers as well.
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
|
// Packages.cpp : main project file.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
char package;
const double planA = 9.95;
const double planB = 14.95;
const double planC = 19.95;
double hours;
double currentBill;
double currentBillb;
do
{
package = ' ';
cout << "Select a package: \n" << endl;
cout << "Package\t\tCost\t\tHours Provided\t\tExtra Hours" << endl;
cout << "A\t\t$" << planA <<"\t\t10\t\t\t$2.00 per hour" << endl;
cout << "B\t\t$" << planB <<"\t\t20\t\t\t$1.00 per hour" << endl;
cout << "C\t\t$" << planC <<"\t\tUnlimited\t\tUnlimited" << endl << endl;
cout << "E\t\t EXIT THE PROGRAM" << endl;
while((package< 'A' || package > 'C') && package != 'E')
{
cout << "\nEnter the package purchased: ";
cin >> package;
package = toupper(package);
if((package< 'A' || package > 'C') && package != 'E' )
cout <<"\nError! You must select package A, B, or C. ";
}
if(package != 'E')
{
cout <<"\nEnter the number of hours used: ";
cin >>hours;
//cin.get();
while(hours < 0 || hours > 744)
{
cout <<"\nError! Hours cannot be negative or exceed 744. \n\nYou must enter appropriate hours.";
cout <<"\n\nEnter the number of hours used. ";
cin >> hours;
}
if(package == 'A')
{
if (hours <= 10)
cout<<"\nYour monthly fee is: $" << planA <<endl;
else
cout<<"\nYour monthly fee is: $"<<planA+(hours-10)*2<<endl;}
currentBill = (planA+(hours-10)*2);
if(package == 'A')
{
if (hours >= 13 && hours <=20)
{
cout<<"\nAccording to your current bill: $"<<currentBill<<" if you were to switch to Plan B,\nyou would save: $"<<currentBill-planB<<endl;
}
else if (hours > 20)
{
cout<<"\nAccording to your current bill: $"<<currentBill<<" if you were to switch to Plan C,\nyou would save: $"<<currentBill-planC<<endl;
}
}
if(package == 'B')
{
if (hours <= 20)
cout<<"\nYour monthly fee is: $" << planB <<endl;
else
cout<<"\nYour monthly fee is: $"<< planB + (hours - 20) <<endl;
}
currentBillb = (planB + (hours - 20));
if(package == 'B')
{
if (hours > 25)
{
cout<<"\nAccording to your current bill: $"<<currentBillb<<" if you were to switch to Plan C, You\n\ncould save: $"<<(currentBillb-planC)<<endl;
}
else if (hours <= 10)
{
cout<<"\nAccording to the total of hours you used this month : "<<hours<<" hours. If you were to\n\nswitch to Plan A, You\n\ncould save: $"<<abs(currentBillb-planA)<<endl;
}
}
if(package == 'C')
{
cout<<"\nYour monthly fee is: $" << planC <<endl;
if ((hours <= 20 && hours>10))
cout<<"\nAccording to the total of hours you used this month : "<<hours<<" hours. If you were to\n\nswitch to Plan B, You\n\ncould save: $"<<abs(planC-planB)<<endl;
else if (hours <= 10)
cout<<"\nAccording to the total of hours you used this month : "<<hours<<" hours. If you were to\n\nswitch to Plan A, You\n\ncould save: $"<<abs(planC-planA)<<endl;
}
}
}while(package !='E' );
cout << endl << endl << "Thanks for shipping with us.." << endl;
cin.get();
return 0;
}
|