944,171 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1015
  • C++ RSS
Nov 4th, 2009
0

Internet Provider Switch Program Help

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vinochick is offline Offline
17 posts
since Nov 2009
Nov 4th, 2009
0
Re: Internet Provider Switch Program Help
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)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vinochick is offline Offline
17 posts
since Nov 2009
Nov 4th, 2009
0
Re: Internet Provider Switch Program Help
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6.  
  7. int main()
  8. {
  9. //declare constants and variables
  10. double packageA = $9.95;
  11. double packageB = $14.95;
  12. double packageC = $19.95;
  13. char pkg = ' ';
  14. int hrs = 0;
  15.  
  16. //get input items
  17. 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;
  18. 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;
  19. cout << "C. For $19.95/mo. unlimited access is provided." << endl;
  20. cout << "Enter A, B, or C: "'
  21. cin >> pkg;
  22. cout << "Enter the number of hours used: ";
  23. cin >> hrs;
  24.  
  25.  
  26.  
  27. //enter input data
  28. cout<<"Enter pkg: ";
  29. cin>>pkg;
  30. pkg = toupper(pkg);
  31.  
  32. if( pkg==A || pkg==B || pkg==C)
  33.  
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vinochick is offline Offline
17 posts
since Nov 2009
Nov 4th, 2009
0
Re: Internet Provider Switch Program Help
should be if(pkg == 'A' || pkg=='B' || pkg == 'C') otherwise they won't match character for character.

Your switch statement should look like:
C++ Syntax (Toggle Plain Text)
  1. switch(pkg)
  2. {
  3. case 'A': //case A goes here
  4. break;
  5. case 'B' : // case B goes here
  6. break;
  7. etc.
  8. }
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Nov 4th, 2009
0
Re: Internet Provider Switch Program Help
I'm getting there, but still getting syntax errors.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. //declare constants and variables
  8. const double packageA = 9.95;
  9. const double packageB = 14.95;
  10. const double packageC = 19.95;
  11. char pkg = ' ';
  12. int hrs = 0;
  13. double chg = 0.0;
  14.  
  15.  
  16. //get input items
  17. 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;
  18. 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;
  19. cout << "C. For $19.95/mo. unlimited access is provided." << endl;
  20. cout << "Enter A, B, or C: "'
  21. cin >> pkg;
  22. pkg = toupper(pkg);
  23.  
  24. if (pkg == 'A'|| pkg =='B' || pkg =='C')
  25.  
  26. //perform calculations
  27. cout<<"Enter hours: ";
  28. cin>>hrs;
  29.  
  30. if(hrs>=0 && hrs<=744)
  31. {
  32. {
  33. switch (pkg)
  34.  
  35. case 'A':
  36. chg = packageA + ( (hrs>10) ? (hrs-10)*2 : 0 );
  37.  
  38.  
  39. if (hrs<=10)
  40. chg= packageA;
  41. else if (hrs>11 && hrs <=50)
  42. chg= packageA+( (hrs>20 && hrs<=50) ? (hrs-20)*3 : 0 );
  43. else if (hrs>51)
  44. chg= packageA+((hrs-51)*4 : 0);
  45. case 'B':
  46. chg = packageB + ( (hrs >20)? (hrs-20)*1 : 0);
  47.  
  48. if (hrs <=20)
  49. chg = packageB;
  50. else if (hrs>21 && hrs <=75)
  51. chg - packageB + ( (hrs > 21) ? (hrs - 21) * 3 : 0 );
  52. else if (hrs > 75)
  53. chg = packageB + ((hrs - 75) * 4 : 0);
  54. //end ifs
  55. case 'C' :
  56. chg = packageC;
  57. }//end switch
  58.  
  59. //display number
  60. cout << "Your charges are: $" <<chg<<endl;
  61. return 0;
  62. } //end of main function
  63.  
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vinochick is offline Offline
17 posts
since Nov 2009
Nov 4th, 2009
0
Re: Internet Provider Switch Program Help
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}
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Nov 10th, 2009
0

Please help :)

I am still having problems with this program. I don't understand what I'm doing wrong.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vinochick is offline Offline
17 posts
since Nov 2009
Nov 10th, 2009
0
Re: Internet Provider Switch Program Help
Post the portion that you are working on or refer us back to the old program as to where you are having problems
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Nov 10th, 2009
0
Re: Internet Provider Switch Program Help
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. //declare constants and variables
  8. const double packageA = 9.95;
  9. const double packageB = 14.95;
  10. const double packageC = 19.95;
  11. char pkg = ' ';
  12. int hrs = 0;
  13. double chg = 0.0;
  14.  
  15.  
  16. //get input items
  17. 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;
  18. 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;
  19. cout << "C. For $19.95/mo. unlimited access is provided." << endl;
  20. cout << "Enter A, B, or C: ";
  21. cin >> pkg;
  22. pkg = toupper(pkg);
  23.  
  24. if (pkg == 'A'|| pkg =='B' || pkg =='C')
  25.  
  26. //perform calculations
  27. cout<<"Enter hours: ";
  28. cin>>hrs;
  29.  
  30. if(hrs>=0 && hrs<=744)
  31. {
  32. {
  33. switch (pkg)
  34.  
  35. case 'A':
  36. chg = packageA + ( (hrs>10) ? (hrs-10)*2 : 0 );
  37.  
  38.  
  39. if (hrs<=10)
  40. chg= packageA;
  41. {
  42. else if (hrs>11 && hrs <=50)
  43. {chg= packageA+( (hrs>20 && hrs<=50) ? (hrs-20)*3 : 0 );}
  44. else if (hrs>51)
  45. {chg= packageA+((hrs > 51) ? (hrs-51)*4 : 0);
  46. }
  47. //end ifs
  48.  
  49. case 'B':
  50. chg = packageB + ( (hrs >20)? (hrs-20)*1 : 0);
  51.  
  52. if (hrs <=20)
  53. {chg = packageB;}
  54. else if (hrs>21 && hrs <=75)
  55. {chg - packageB + ( (hrs > 21) ? (hrs - 21) * 3 : 0 );}
  56. else if (hrs > 75)
  57. {chg = packageB + ((hrs - 75) * 4 : 0);}
  58. //end ifs
  59. case 'C' :
  60. chg = packageC;
  61. }//end switch
  62.  
  63. //display number
  64. cout << "Your charges are: $" <<chg<<endl;
  65. return 0;
  66. } //end of main function
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vinochick is offline Offline
17 posts
since Nov 2009
Nov 10th, 2009
0
Re: Internet Provider Switch Program Help
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.
Last edited by jonsca; Nov 10th, 2009 at 1:39 am.
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: to calculate square and cube form given input using arrays..
Next Thread in C++ Forum Timeline: Matrix multiplication





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC