I am very new to C++ and have the slightest idea where to start. Here are the directions for the project I'm doing. Below is a copy of the program that I have so far.

An internet service provider has three different subscription packages for its customers:

Package A:  For $9.95 per month 10 hours of access are provided. Additional hours are $2.00 per hour for 11-19 hours, $3.00 per hour for 20-50 hours and $4.00 per hour 51 or greater hours with maximum of 744 hours
Package B.  For $14.95 per month 20 hours of access are provided. Additional hours are $1.00 per hour for 11-30 hours, $3.00 per hour for 31-75 hours and $4.00 per hour 76 or greater hours with maximum of 744 hours
Package C.  For $19.95 per month unlimited access is provided.

Write a program that displays the menu above and use a switch case to calculate a customer’s monthly bill. The program will ask which package the customer has purchased and how many hours were used. It should then display the total amount due.
Input Validation: If user enters negative hours or enters a value other than A, B or C the program should display an error message and exit the program. If number of hours used in a month is greater than 744, display a message that the number hours used in a month cannot exceed 744.

#include <iostream>

using namespace std;



int main()
{
    //declare constants and variables
    double packageA = $9.95;
    double packageB = $14.95;
    double packageC = $19.95;
    char pkg = ' ';


    //enter input data
    cout<<"Enter pkg: ";
    cin>>pkg;
    pkg = toupper(pkg);

    if( pkg==A || pkg==B || pkg==C)
    {
        int hrs;
        cout<<"Enter hours: ";
        cin>>hrs;

        if(hrs>=0 && hrs<=744)
        {
            int chg;
            <strong class="highlight">switch</strong>(pkg)
            {
    case A:
         chg = 9.95 + ( (hrs>10) ? (hrs-10)*2 : 0 );
                    break;

                case:
         {
        if (hrs<=20)
             chg= 14.95;
        else if (hrs>20 && hrs <=50)
             chg= 18+( (hrs>20 && hrs<=51) ? (hrs-20)*3 : 0 );

        else if (hrs>51)
             chg= 19.95;

                                break;                              }
                case:
                    <strong class="highlight">3</strong>:
                      {
           if (hrs<=200)
        {
        chg=25;
        }
           else if (hrs>200 && hrs<=450)
        {
        chg=35;
        }
           else if (hrs>450)
        {
        chg=50;
        }
           break;
           }

                default: 
                    break; 
            }

            cout<<"charges: "<<chg<<endl;
        }
        else

            cout<<"Invalid hrs"<<endl;


    }
    else
    {
        cout<<"Invalid pkg"<<endl;
    }





}//end main

Dani AI

Generated

Quick summary and a clear path forward. As already pointed out, the immediate syntax problems are coming from character vs symbol comparisons and from stray/mismatched punctuation and braces in the edit process; has the right overall structure but several small errors are keeping the compiler from getting to the logic. Below are focused, concrete fixes and a short troubleshooting checklist to get the program compiling and producing correct bills.

Key syntax and structure fixes to make first (fix these in order):

  • Remove any literal currency symbols from number literals and make the monthly rates typed as numeric constants (use a floating type for money).
  • Fix every cout/cin statement so it ends with a semicolon and remove duplicated prompt lines; do not prompt twice for the same value.
  • Use single-quote character literals when testing the package letter and put the validation if-block in braces so the input/processing code is grouped correctly.
  • Enclose the entire switch body in braces, put a break at the end of each case to avoid fall-through, and ensure every opening brace has a matching closing brace.
  • Correct misuse of the ternary operator (it always needs both ? and : parts) — if the logic becomes hard to read, prefer simple if/else blocks.

Validation and billing logic advice:

  • Validate package first; if invalid print an error and exit. Then read hours and immediately check for negative or >744 and print a clear error if invalid.
  • For the charge calculation: compute the base fee, then add “additional” charges according to whichever billing interpretation your instructor expects (either a single extra-rate chosen by which bracket total hours fall into, or a cumulative per-tier charging). Pick one interpretation and write tests that demonstrate it so you know your code matches the spec.

Quick debugging checklist:

  • Fix the first compiler error, recompile, repeat.
  • Test with small set of cases: base hours, one hour into the first extra bracket, a mid-range hour, boundary values, >744, and invalid package.
  • Print intermediate values while testing (base fee, extra hours, extra charge) to verify arithmetic.

These focused fixes will stop the syntax noise so you can verify actual billing logic.

Recommended Answers

All 9 Replies

revised code so far...

#include <iostream>

using namespace std;



int main()
{
    //declare constants and variables
    double packageA = $9.95;
    double packageB = $14.95;
    double packageC = $19.95;
    char pkg = ' ';
    int hrs = 0;

    //get input items
    cout << "A.  For $9.95/mo. 10 access hours are provided.  Additional hours are $2.00/hr. for 11-19 hours, $3.00/hr for 20-50 hrs. and $4.00/hr. for 51 hrs. or greater up to a maximum of 744 hours."  << endl;
    cout << "B.  For $14.95/mo. 20 access hours are provided.  Additional hours are $1.00/hr. for 21-30 hours, $3.00/hr for 31-75 hrs. and $4.00/hr. for 76 hrs. or greater up to a maximum of 744 hours."  << endl;
    cout << "C.  For $19.95/mo. unlimited access is provided."  << endl;
    cout <<  "Enter A, B, or C:  "'
        cin  >> pkg;
        cout <<  "Enter the number of hours used:  ";
        cin >> hrs;



    //enter input data
    cout<<"Enter pkg: ";
    cin>>pkg;
    pkg = toupper(pkg);

    if( pkg==A || pkg==B || pkg==C)
#include <iostream>

using namespace std;



int main()
{
	//declare constants and variables
	double packageA = $9.95;
	double packageB = $14.95;
	double packageC = $19.95;
	char pkg = ' ';
	int hrs = 0;

	//get input items
	cout << "A.  For $9.95/mo. 10 access hours are provided.  Additional hours are $2.00/hr. for 11-19 hours, $3.00/hr for 20-50 hrs. and $4.00/hr. for 51 hrs. or greater up to a maximum of 744 hours."  << endl;
	cout << "B.  For $14.95/mo. 20 access hours are provided.  Additional hours are $1.00/hr. for 21-30 hours, $3.00/hr for 31-75 hrs. and $4.00/hr. for 76 hrs. or greater up to a maximum of 744 hours."  << endl;
	cout << "C.  For $19.95/mo. unlimited access is provided."  << endl;
	cout <<  "Enter A, B, or C:  "'
		cin  >> pkg;
		cout <<  "Enter the number of hours used:  ";
		cin >> hrs;



	//enter input data
    cout<<"Enter pkg: ";
    cin>>pkg;
	pkg = toupper(pkg);

    if( pkg==A || pkg==B || pkg==C)

should be if(pkg == 'A' || pkg=='B' || pkg == 'C') otherwise they won't match character for character.

Your switch statement should look like:

switch(pkg)
{
      case 'A':  //case A goes here
               break;
      case 'B' :  // case B goes here
               break;
       etc.
}

I'm getting there, but still getting syntax errors.

#include <iostream>

using namespace std;

int main()
{
	//declare constants and variables
	const double packageA = 9.95;
	const double packageB = 14.95;
	const double packageC = 19.95;
	char pkg = ' ';
	int hrs = 0;
	double chg = 0.0;


	//get input items
	cout << "A.  For $9.95/mo. 10 access hours are provided.  Additional hours are $2.00/hr. for 11-19 hours, $3.00/hr for 20-50 hrs. and $4.00/hr. for 51 hrs. or greater up to a maximum of 744 hours."  << endl;
	cout << "B.  For $14.95/mo. 20 access hours are provided.  Additional hours are $1.00/hr. for 21-30 hours, $3.00/hr for 31-75 hrs. and $4.00/hr. for 76 hrs. or greater up to a maximum of 744 hours."  << endl;
	cout << "C.  For $19.95/mo. unlimited access is provided."  << endl;
	cout <<  "Enter A, B, or C:  "'
		cin  >> pkg;
		pkg = toupper(pkg);

		if (pkg == 'A'|| pkg =='B' || pkg =='C')

		//perform calculations
		cout<<"Enter hours: ";
        cin>>hrs;

        if(hrs>=0 && hrs<=744)
        {           
            {
				switch (pkg)
				
	case 'A':
	     chg = packageA + ( (hrs>10) ? (hrs-10)*2 : 0 );
                    
                
		if (hrs<=10)
		     chg= packageA;
		else if (hrs>11 && hrs <=50)
		     chg= packageA+( (hrs>20 && hrs<=50) ? (hrs-20)*3 : 0 );				
		else if (hrs>51)
			chg= packageA+((hrs-51)*4 : 0);
	case 'B':
		chg = packageB + ( (hrs >20)? (hrs-20)*1 : 0);
		
		if (hrs <=20)
			chg = packageB;
		else if (hrs>21 && hrs <=75)
			chg - packageB + ( (hrs > 21) ? (hrs - 21) * 3 : 0 );
		else if (hrs > 75)
			chg = packageB + ((hrs - 75) * 4 : 0);
		//end ifs
	case 'C' :
		chg = packageC;
			}//end switch

			//display number
			cout << "Your charges are:  $"  <<chg<<endl;
			return 0;
		}  //end of main function

Start with the first error and go through them one by one. It's just a bunch of stray stuff that you left in during editing. Just go to the lines the complier is complaining about, you'll catch em.
And, you need braces {} around your entire switch block, encompassing all the cases. switch(variable) { //all the cases in here}

I am still having problems with this program. I don't understand what I'm doing wrong.

Post the portion that you are working on or refer us back to the old program as to where you are having problems

#include <iostream>

using namespace std;

int main()
{
	//declare constants and variables
	const double packageA = 9.95;
	const double packageB = 14.95;
	const double packageC = 19.95;
	char pkg = ' ';
	int hrs = 0;
	double chg = 0.0;


	//get input items
	cout << "A.  For $9.95/mo. 10 access hours are provided.  Additional hours are $2.00/hr. for 11-19 hours, $3.00/hr for 20-50 hrs. and $4.00/hr. for 51 hrs. or greater up to a maximum of 744 hours."  << endl;
	cout << "B.  For $14.95/mo. 20 access hours are provided.  Additional hours are $1.00/hr. for 21-30 hours, $3.00/hr for 31-75 hrs. and $4.00/hr. for 76 hrs. or greater up to a maximum of 744 hours."  << endl;
	cout << "C.  For $19.95/mo. unlimited access is provided."  << endl;
	cout <<  "Enter A, B, or C:  ";
		cin  >> pkg;
		pkg = toupper(pkg);

		if (pkg == 'A'|| pkg =='B' || pkg =='C')

		//perform calculations
		cout<<"Enter hours: ";
        cin>>hrs;

        if(hrs>=0 && hrs<=744)
        {           
            {
				switch (pkg)
				
	case 'A':
	     chg = packageA + ( (hrs>10) ? (hrs-10)*2 : 0 );
                 
		          
		if (hrs<=10)
		     chg= packageA;
		{
		else if (hrs>11 && hrs <=50)
		{chg= packageA+( (hrs>20 && hrs<=50) ? (hrs-20)*3 : 0 );}				
		else if (hrs>51)
		{chg= packageA+((hrs > 51) ? (hrs-51)*4 : 0);
		}
		//end ifs

	case 'B':
		chg = packageB + ( (hrs >20)? (hrs-20)*1 : 0);
		
		if (hrs <=20)
		{chg = packageB;}
		else if (hrs>21 && hrs <=75)
		{chg - packageB + ( (hrs > 21) ? (hrs - 21) * 3 : 0 );}
		else if (hrs > 75)
		{chg = packageB + ((hrs - 75) * 4 : 0);}
		//end ifs
	case 'C' :
		chg = packageC;
			}//end switch

			//display number
			cout << "Your charges are:  $"  <<chg<<endl;
			return 0;
		}  //end of main function

Your statements in line 57 doesn't make any sense. The ternary operator for if statements needs 3 things. So delete the :0 there . Unless you have been explicitly taught the way you have it, it makes the code difficult to read. I haven't broken down all of your if statements to see if they make sense in light of your spec but one thing I noticed is you have no condition for the user specifying > 744 minutes, the program just exits silently.

Also, if (pkg == 'A'|| pkg =='B' || pkg =='C') is just hanging off in the air. You need to put in braces if you want to encompass everything underneath it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.