Below is a copy of my code for a homework assignment. I need to validate the input of hours. The validation appears to be working, but then it will skip my next if statment that trys to preform a calculation. It will however display the data from my else statment. I'm confused...Any help would be appreciated!
Thanks
# include <iostream>
# include <iomanip>
# include <string>
# include <fstream>
cout << setw(67) << "Please Select a Subscription Package from the list below!\n \n \n";
cout << "Package " << setw(10)<< right << "Rate"
<< setw(20) << right << "Number of Hours"
<< setw(30) << right << "Pice Per Additional Hour\n\n\n";
cout << "Package A:" << setw(8)<< right << aRate
<< setw(18) << right << "10/Month "
<< setw(23) << right << "$2.00/Hour " << endl;
cout << "Package B:" << setw(8)<< right << bRate
<< setw(18) << right << "20/Month "
<< setw(23) << right << "$1.00/Hour " << endl;
cout << "Package C:" << setw(8)<< right << cRate
<< setw(21) << right << "Unlimited/Month "
<< setw(17) << right << "N/A " << endl << endl << endl;
Much of what you did was repeated. For instance, inside each of the case statements you asked identical questions which really only need asking once as shown in example below.
I ignored your if(!errFlag) because its unclear what you are intending to do.
Added a do-while loop to replace your errflag attempt. You should also put a 'default' in a switch statement. Google it and find out about it. I always read that you must ALWAYS put in a default no matter how simple the switch statement...
include <iostream>
# include <iomanip>
# include <string>
usingnamespace std;
int main( )
{
constdouble aRate = 9.95, bRate = 14.95, cRate = 19.95;
string name;
double hours;
double aBaseHr = 10.0;
double bBaseHr = 20.0;
double aAdd = 2.0;
double bAdd = 1.0;
double total;
char ch;
do
{
cout << setw(67) << "Please Select a Subscription Package from the list below!\n \n \n";
cout << "Package " << setw(10)<< right << "Rate"
<< setw(20) << right << "Number of Hours"
<< setw(30) << right << "Price Per Additional Hour\n\n\n";
cout << "Package A:" << setw(8)<< right << aRate
<< setw(18) << right << "10/Month "
<< setw(23) << right << "$2.00/Hour " << endl;
cout << "Package B:" << setw(8)<< right << bRate
<< setw(18) << right << "20/Month "
<< setw(23) << right << "$1.00/Hour " << endl;
cout << "Package C:" << setw(8)<< right << cRate
<< setw(21) << right << "Unlimited/Month "
<< setw(17) << right << "N/A " << endl << endl << endl;
cout << "Please Select Your Subscription A, B, or C." << endl;
cin >> ch;
if ((ch == 'a') || (ch == 'A') || (ch == 'b') || (ch == 'B') || (ch == 'c') || (ch == 'C'))
{
break;
}
cout << "\n\n\n\n\n\n\nYou entered an invalid option" << endl;
}while ((ch != 'a') || (ch != 'A') || (ch != 'b') || (ch != 'B') || (ch != 'c') || (ch != 'C'));
cout << "You Have Selected Package " << ch << endl;
cout << "Please Enter your First and Last Name \n";
cin.ignore();
getline(cin, name);
cout << "Please Enter the number of hours your used this month. \n" ;
cin >> hours;
switch(ch)
{
case'a':
case'A': if(hours >= 745)
{
cout << "You have exceeded the maximum number of hours in a month\n" ;
}
if(hours > aBaseHr)
{
total = ((hours - aBaseHr)* aAdd) + aRate;
cout << name << " your internet bill this month is $" << total << endl;
}
else
{
total = aRate;
cout << name << " your internet bill this month is $" << total << endl;
}
break;
case'b':
case'B': if(hours > bBaseHr)
{
total = ((hours - bBaseHr)* bAdd) + bRate;
cout << name << " your internet bill this month is $" << total << endl;
}
else { total = bRate;
cout << name << " your internet bill this month is $" << total << endl;
}
break;
case'c':
case'C': total = cRate;
cout << name << " your internet bill this month is $" << total << ", you used " << hours << " hours this month." << endl;
break;
default:
cout<<"Selection invalid.\n\n\n";
break;
}
cin.ignore().get();
return 0;
}