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
``````

## 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, learning, and sharing knowledge.