if statement being skipped after validation

Hi

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>

using namespace std;

int main( )
{

const double 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;
bool errFlag = false;
char ch;

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;


{
if(!errFlag)
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'))
cout << "You entered an invalid option" << endl;
errFlag = true;
}

}

switch(ch)
{
case 'a':
case 'A':
cout << "You Have Selected Package A!" << endl;
cout << "Please Enter your First and Last Name ";

cin.ignore();
getline(cin, name);



cout << "Please Enter the number of hours your used this month " ;
cin >> hours;

if(hours >= 745)
{
cout << "You have exceeded the maximum number of hours in a month\n" ;
errFlag = true;
}

if(!errFlag)

// cannot exceed 744 hours in a month



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':
cout << "You Have Selected Package B!" << endl;
cout << "Please Enter your First and Last Name ";

cin.ignore();
getline(cin, name);

cout << "Please Enter the number of hours you used this month " ;
cin >> hours;



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':
cout << "You Have Selected Package C!" << endl;
cout << "Please Enter your First and Last Name ";

cin.ignore();
getline(cin, name);

cout << "Please Enter the number of hours your used this month " ;
cin >> hours;

// cannot exceed 744 hours in a month

total = cRate;
cout << name << " your internet bill this month is $" << total << ", you used " << hours << " hours this month." << endl;



break;

}
return 0;
}
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...

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

 include <iostream>
# include <iomanip>
# include <string>
using namespace std;

int main( )
{

const double 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;
}


Last edited on
Topic archived. No new replies allowed.