I am attempting to complete the phone bill calculator for my C++ course. I have been provided the following:
Plan A:
Price: $39.99 per month.
Data allowance: Includes 4 gigabytes of data. Additional data costs $10 per gigabyte.
Plan B:
Price: $59.99 per month.
Data Allowance: Includes 8 gigabytes of data. Additional data costs $5 per gigabyte.
Plan C:
Price: $69.99 per month.
Data Allowance: Unlimited
I feel I am improperly implementing the switch cases as well as the if statements. On top of that, if the user inputs a non-numeric value for gigsUsed, the program displays the total based on the first else if statement. Example: user inputs “B” for gigsUsed. The output will show “The total amount due is $39.99. How can I make sure only valid numeric inputs will be taken? I apologize in advance for any silly errors being made both with my code and with posting this question, I am new to programming, and this is my first post to this site.
// Display menu & prompt user for plan type (1-3) and gigabytes of data used (not a negative value)
cout << "nSelect a subscription package:" << endl
<< "t1. Package A" << endl
<< "t2. Package B" << endl
<< "t3. Package C" << endl
<< "t4. Quit" << endl
<< "Package: ";
cin >> plan;
// Drive menu options
switch (plan)
{
case 1: // Plan A
// Prompt User for total amount of gigabytes used
cout << "nHow many gigabytes of data were used? ";
cin >> gigsUsed;
// Determine total amount due
if (gigsUsed > 4)
totalOwed = (gigsUsed - 4) * 10 + 39.99;
else if (gigsUsed <= 4)
totalOwed = 39.99;
else if (gigsUsed < 0)
cout << "nError ... Invalid gigabytes entered. Try Again." << endl;
// Display total amount due
cout << "nThe total amount due is $" << totalOwed << endl;
break;
case 2: // Plan B
// Prompt User for total amount of gigabytes used
cout << "nHow many gigabytes of data were used? ";
cin >> gigsUsed;
// Determine total amount due
if (gigsUsed > 8)
totalOwed = (gigsUsed - 8) * 5 + 59.99;
else if (gigsUsed <= 8)
totalOwed = 59.99;
else if (gigsUsed < 0)
cout << "nError ... Invalid gigabytes entered. Try Again." << endl;
// Display total amount due
cout << "nThe total amount due is $" << totalOwed << endl;
break;
case 3: // Plan C
// Display total amount due
totalOwed = 69.99;
cout << "nThe total amount due is $" << totalOwed << endl;
break;
case 4: // Exit option
// Let the user know they are exiting the program
cout << "nYou have chosen to exit the program ... Goodbye!" << endl;
break;
default: // Error
cout << "Error ... Invalid Package. Try again." << endl;
break;
}
return 0;
}
Carson Farinella is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.