nested if-else statements

I am new to c++ programming, and I wrote this program. It runs perfectly but the problem is I want the program to stop If I entered letters other than A,B or C
The code as follows:

#include <cstdlib>
#include <iostream>
#include <cstring>
#include <iomanip>

using namespace std ;

int main()
{
// Decleration Statements

const float PACKAGE_ACOST = 9.95, PACKAGE_BCOST = 14.95, PACKAGE_CCOST = 19.95 ;
const float ADD_AHOURS_COST = 2.00 , ADD_BHOURS_COST = 1.00 ;
float total_amount = 0 ,add_cost = 0.0 , package_pay ;
int package_Ahours = 10 , package_Bhours = 20 , package_Chours ;
int used_hours , add_hours=0 ;
string customer_name ;
char packageType ;

// C++ Statements

cout << "Enter Customer's Name : " ;
getline(cin,customer_name);

cout << endl << "Enter the package type: " ;
cin >> packageType ;
packageType = toupper(packageType) ;

// Selection Statement to evaluate the package type that was entered
if ( packageType != 'A')
{
if ( packageType != 'B')
{
if ( packageType != 'C')

{
cout << endl << "Invalid Package Type" ;
}

else
{
// Prompting the user to enter how many hours did the customer use the internet
cout << endl << "Enter the hours that " << customer_name << " use : " ;
cin >> used_hours ;



total_amount = PACKAGE_CCOST ;
package_pay = PACKAGE_CCOST ;
}
}
else
{
// Prompting the user to enter how many hours did the customer use the internet
cout << endl << "Enter the hours that " << customer_name << " use : " ;
cin >> used_hours ;

if (used_hours <= 20)
{
total_amount = PACKAGE_BCOST ;
package_pay = PACKAGE_BCOST ;
}
else
{
// Assignment Statements
add_hours = used_hours - 20; // Calculate how many extra hours did the customer use if his package is B
add_cost = add_hours * ADD_BHOURS_COST ; // Calculate how much does the extra hours cost
total_amount = PACKAGE_BCOST + add_cost ; /* Calculate the total amount that the customer should
pay in the end of the month */
package_pay = PACKAGE_BCOST ;
}
}
}
else
{
// Prompting the user to enter how many hours did the customer use the internet
cout << endl << "Enter the hours that " << customer_name << " use : " ;
cin >> used_hours ;


if (used_hours <= 10)
{
total_amount = PACKAGE_ACOST ;
package_pay = PACKAGE_ACOST ;
}
else
{
add_hours = used_hours - 10 ; // Calculate how many extra hours did the customer use if his package is A
add_cost = add_hours * ADD_AHOURS_COST ; // Calculate how much does the extra hours cost
total_amount = PACKAGE_ACOST + add_cost ; /* Calculate the total amount that the customer should
pay in the end of the month */
package_pay = PACKAGE_ACOST ;
}
}


cout << endl << endl << endl ;
// Formatting the output

// Designing The Bill
cout << setw(51) << "*****************************\n";
cout << setw(50) << "*The Siegel Internet Company*" << endl ;
cout << setw(51) << "*****************************\n";
cout << setw(72) << "1 Pine Street\n" ;
cout << setw(78) << "Paterson , NJ 07505\n" ;
cout << setw(81) << "Phone#: (201)-777-777\n\n" ;

// The customer information
cout << setw(15) << "Dear Mr/Mrs " << customer_name << endl << endl ;
cout << setw(54) << "Your monthly internet bill is due on May 15, 2014\n\n";
cout << setw(33) << "The bill summary is included\n\n";

cout << setw(19) << "Customer Name : " << customer_name << endl << endl;
cout << setw(18) << "Package Type : " << packageType << endl << endl;

cout << setw(17) << "Package/Month " << setw(16) << "Hours Used" << setw(25) << "Additional Used Hours"
<< setw(20) << "Additional Cost" << endl ;
cout << setw(16) << "-------------" << setw(17) << "----------" << setw(25) << "---------------------"
<< setw(20) << "---------------" << endl ;

// Displaying the calculation results
cout << setw(7) << "$" << package_pay << setw(19) << used_hours << setw(19) << add_hours << setw(20) << "$" << add_cost << endl << endl ;
cout << setw(19) << "Total Amount = " << setw(2) << "$" << package_pay + add_cost << endl << endl;


system ("pause");

return 0 ;

}
Easiest way is to put a return 0; after the cout << endl << "Invalid Package Type" ; line.
why not just do
while(packageType != 'A' || packageType != 'B' || packageType != 'C'){
cerr << "Invalid package type! " << endl;
cout << endl << "Enter the package type: " ;
cin >> packageType ;
}

not sure if you want this error to exit or to keep prompting.. just my thoughts

return 0 or exit(0) will exit on error if that's your goal, but keep it prompting will work fine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch(packageType)
{
  case 'A':
    ...
    break;
  case 'B':
    ...
    break;
  case 'C':
    ...
    break;
  default:
    std::cout << std::endl << "Invalid Package Type" ;
    exit(0);
};
Simply put the following condition

1
2
3
4
5
6
7
8
do
{}
 while(packageType == 'A' || packageType == 'B' || packageType == 'C')
{}
 if(!(packageType == 'A' || packageType == 'B' || packageType == 'C'))
{
exit(0);
}
Thank you All That was helpful
Topic archived. No new replies allowed.