In: Computer Science
QUESTION 1a
A valid selection must be between 0 and 4. Enter the program
below and complete the while loop. Name the program
question1.cpp, compile and run it.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// Fill in the code to define an integer variable called
selection
cout << "Please enter the choice of ticket "
<< "(a number from 1 to 4 or 0 to quit) " <<
endl;
cout << "Ticket Menu " << endl << endl;
cout << "1: Exclusive VIP area A " << endl;
cout << "2: VIP area B " << endl;
cout << "3: Elevated area " << endl;
cout << "4: General area R600 " << endl;
cout << "0: QUIT " << endl <<endl <<
endl;
cin >> selection ;
while (…………………) //complete the condition
{
cout << “Invalid choice – Please re-enter “;
cin >> selection;
}
cout << “You have selected option number “ <<
selection;
return 0;
}
QUESTION 1b
Modify question1.cpp by adding the code to calculate the cost
per order. Make use of a switch statement.
cout << "You have selected option number "“ <<;
cout << "How many tickets would you like?" <<
endl;
// Fill in the code to read in number
// Fill in the code to begin a switch statement
// that is controlled by selection
{
case 1: cost = number * 3000;
cout << "The total cost is R " << cost <<
endl;
break;
// Fill in the code for the case VIP area B ( R2000.00 each)
// Fill in the code for the case Elevated area (R1200.00
each)
// Fill in the code for the case General area (R600.00 each)
case 0: cout << " Please come again" << endl;
break;
default:
cout << “Invalid selection”
cout << " Try again please" << endl;
}
Solution(1a):
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int selection;
cout << "Please enter the choice of ticket "
<< "(a number from 1 to 4 or 0 to quit) " << endl;
cout << "Ticket Menu " << endl << endl;
cout << "1: Exclusive VIP area A " << endl;
cout << "2: VIP area B " << endl;
cout << "3: Elevated area " << endl;
cout << "4: General area R600 " << endl;
cout << "0: QUIT " << endl <<endl << endl;
cin >> selection ;
while (selection<0 || selection>4) //complete the condition
{
cout << "Invalid choice - Please re-enter";
cin >> selection;
}
cout << "You have selected option number " << selection;
return 0;
}
Output:
Solution(1b):
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int selection,cost,number;
cout << "Please enter the choice of ticket "
<< "(a number from 1 to 4 or 0 to quit) " << endl;
cout << "Ticket Menu " << endl << endl;
cout << "1: Exclusive VIP area A " << endl;
cout << "2: VIP area B " << endl;
cout << "3: Elevated area " << endl;
cout << "4: General area R600 " << endl;
cout << "0: QUIT " << endl <<endl << endl;
cin >> selection ;
while (selection<0 || selection>4) //complete the condition
{
cout << "Invalid choice - Please re-enter";
cin >> selection;
}
cout << "You have selected option number " << selection;
cout << "\nHow many tickets would you like?" << endl;
cin>>number;
switch(selection)
{
case 1: cost = number * 3000;
cout << "The total cost is R " << cost << endl;
break;
case 2: cost = number * 2000;
cout << "The total cost is R " << cost << endl;
break;
case 3: cost = number * 1200;
cout << "The total cost is R " << cost << endl;
break;
case 4: cost = number * 600;
cout << "The total cost is R " << cost << endl;
break;
case 0: cout << " Please come again" << endl;
break;
default:
cout << "Invalid selection";
cout << " Try again please" << endl;
}
return 0;
}
Output: