Switch Statement Help

,, easp
Last edited on
For your 2nd while statement, try changing || to &&
In addition to changing || to &&

You have an extra set of { }. There is an extra one before cout << "How many hours were used?: "; then delete one from the end.

Also, you may want to verify what the user enters is valid and not let them continue till they enter something valid.

Lastly, this will help you to reduce some of the length of the code. http://www.cplusplus.com/reference/cctype/toupper/
This is toupper. You can implement it by adding #include <ctype.h> and :[code]toupper(userPackage); after the user inputs userPackage. Then when testing the value of userPackage you only have to test the upper case so instead of
while (userPackage != 'A' && userPackage != 'a' && userPackage != 'b' && userPackage != 'B' && userPackage != 'c' && userPackage != 'C')
you can do
while (userPackage != 'A' && userPackage != 'B' && userPackage != 'C')
Problem solved. I used the function "toupper()" from <cctype> library.

#include <iostream>
#include <cctype>

using namespace std;

int main()
{
        char userPackage;
		int hoursUsed;			// Hours used by user
		int package_A_hours = 10;	// Max hours for Package A
		int package_B_hours = 20;	// Max hours for Package B
		int differenceHours;	// Difference of hours allowed and hours used
		double additionalHours;	// Cost of additional hours
		double amountDue_A;		// Monthly bill of user for Package A
		double amountDue_B;		// Monthly bill of user for Package B

	// Get package choice
		cout << "Please select one of the following Package choices:\n\n";
		cout << "Package A: For $9.95/month 10 hours of access are provided. Additional hours are $2.00/hour.\n";
		cout << "Package B: For $14.95/month 20 hours of access are provided. Additional hours are $1.00/hour.\n";
		cout << "Package C: For $19.95 per UNLIMITED access is provided.\n\n";

		cout << "Which package do you use? (A, B, C)\n";
		cout << "Package: ";
        cin >> userPackage;
        userPackage = toupper(userPackage);

        while (userPackage != 'A' && userPackage != 'a' && userPackage != 'b' && userPackage != 'B' && userPackage != 'c' && userPackage != 'C')
        {
        	cout << "Please enter a valid package choice: ";
        	cin >> userPackage;
        	userPackage = toupper(userPackage);
        }

        cout << "How many hours were used?: ";
        cin >> hoursUsed;

	// Validate hours used

        while (hoursUsed < 0 || hoursUsed > 744)
        {
        	cout << "Please enter a valid amount of hours: ";
        	cin >> hoursUsed;
        }


        switch( userPackage )
        {
            case 'A':
                if (hoursUsed <= 10)
                	cout << "Your monthly bill is $ 9.95\n";

                else if (hoursUsed > 10)
                {
                	differenceHours = hoursUsed - package_A_hours;
                	additionalHours = differenceHours * 2.00;
                	amountDue_A = additionalHours + 9.95;
                	cout << "Your monthly bill is $" << amountDue_A << endl;
                }
                break;

            default:
                cout << "VocĂȘ digitou uma userPackage invalida.";

        }
Since you used toupper then you don't need to test the lower case.

This:
while (userPackage != 'A' && userPackage != 'a' && userPackage != 'b' && userPackage != 'B' && userPackage != 'c' && userPackage != 'C')

can become:
while (userPackage != 'A' && userPackage != 'B' && userPackage != 'C')
Topic archived. No new replies allowed.