having problem with an if statement...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 57
Reputation: cassie_sanford is an unknown quantity at this point 
Solved Threads: 0
cassie_sanford cassie_sanford is offline Offline
Junior Poster in Training

having problem with an if statement...

 
1
  #1
Feb 21st, 2009
The problem that i am working asks to calculate a customer's monthly bill. The customer has to input their name, which package they purchased, and how many hours were used. For example package a is for 9.95 a month 10 hours of access provided. Additional hours are 2.00 per hour. Im wondering if my syntax is correct...here's what i have so far...
#include <iostream>
#include <string>
#include <iomanip>


using namespace std;

int main()
{
    //Declare variables
    const double PACKAGE_A_RATE = 9.95;
    const double PACKAGE_B_RATE = 14.95;
    const double PACKAGE_C_RATE = 19.95;
    
    char packageA;
    char packageB;
    char packageC;
    char packageName;
    double packageACharge;
    double packageBCharge;
    double packageCCharge;
    double hours;
    double totalDue;
    string customerName;
    
    
 
    
    
    
    cout << "Enter your name: ";
    cin >> customerName;
    
    cout << "Enter the package you purchased: ";
    cin >> packageName;
    
    cout << "Enter the number of hours used: ";
    cin >> hours;
    
    if(hours ==10)
    {
             packageACharge = PACKAGE_A_RATE;
    }
    else if(hours > 10)
    {
         packageACharge = (packageACharge + hours) % 2;
         
         cout << packageACharge<<endl;
    
    }
    
    
    system("pause");
    return 0;
}

can anyone help?
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 39
Reputation: dvsConcept is an unknown quantity at this point 
Solved Threads: 0
dvsConcept dvsConcept is offline Offline
Light Poster

Re: having problem with an if statement...

 
0
  #2
Feb 22nd, 2009
if i did your math right then the problem was with your math (you used a % not a *) not with the if statement and i switched your package name to a string but you can switch it back if you like...hope this helps (it does compile now)

  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. //Declare variables
  11. const double PACKAGE_A_RATE = 9.95;
  12. const double PACKAGE_B_RATE = 14.95;
  13. const double PACKAGE_C_RATE = 19.95;
  14.  
  15. char packageA = ' ';
  16. char packageB = ' ';
  17. char packageC = ' ';
  18. string packageName = "";
  19. double packageACharge = 0.0;
  20. double packageBCharge = 0.0;
  21. double packageCCharge = 0.0;
  22. double hours = 0.0;
  23. double totalDue = 0.0;
  24. string customerName = "";
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31. cout << "Enter your name: ";
  32. cin >> customerName;
  33.  
  34. cout << "Enter the package you purchased: ";
  35. cin >> packageName;
  36.  
  37. cout << "Enter the number of hours used: ";
  38. cin >> hours;
  39.  
  40. if(hours ==10)
  41. {
  42. packageACharge = PACKAGE_A_RATE;
  43. cout << packageACharge<<endl;
  44. }
  45. else if(hours > 10)
  46. {
  47. packageACharge = (packageACharge +((hours - 10) * 2))+10;
  48.  
  49. cout << packageACharge<<endl;
  50.  
  51. }
  52.  
  53.  
  54. system("pause");
  55. return 0;
  56. }


by the way use code tags to display your code properly here...the other people might get mad if you dont!
[ code = c++ ] <---beginning tag
[ / code ] <--ending tag ((both with no spaces))
Last edited by dvsConcept; Feb 22nd, 2009 at 12:40 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 57
Reputation: cassie_sanford is an unknown quantity at this point 
Solved Threads: 0
cassie_sanford cassie_sanford is offline Offline
Junior Poster in Training

Re: having problem with an if statement...

 
0
  #3
Feb 22nd, 2009
can i do the same format for packages b-c?
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 39
Reputation: dvsConcept is an unknown quantity at this point 
Solved Threads: 0
dvsConcept dvsConcept is offline Offline
Light Poster

Re: having problem with an if statement...

 
0
  #4
Feb 22nd, 2009
Originally Posted by cassie_sanford View Post
can i do the same format for packages b-c?
yes it will work just fine for b-c
Last edited by dvsConcept; Feb 22nd, 2009 at 12:42 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 57
Reputation: cassie_sanford is an unknown quantity at this point 
Solved Threads: 0
cassie_sanford cassie_sanford is offline Offline
Junior Poster in Training

Re: having problem with an if statement...

 
0
  #5
Feb 22nd, 2009
For example, if the user uses 12 hours and the package calls for 9.95 per month 10 hrs of access, my answer on the calculator came out to be 13.95
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 39
Reputation: dvsConcept is an unknown quantity at this point 
Solved Threads: 0
dvsConcept dvsConcept is offline Offline
Light Poster

Re: having problem with an if statement...

 
0
  #6
Feb 22nd, 2009
Originally Posted by cassie_sanford View Post
For example, if the user uses 12 hours and the package calls for 9.95 per month 10 hrs of access, my answer on the calculator came out to be 13.95
ok let me change the way we are calculating because this is the answer you need right? one sec while i adjust the calculation
Last edited by dvsConcept; Feb 22nd, 2009 at 12:47 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 39
Reputation: dvsConcept is an unknown quantity at this point 
Solved Threads: 0
dvsConcept dvsConcept is offline Offline
Light Poster

Re: having problem with an if statement...

 
0
  #7
Feb 22nd, 2009
this will end with the answer you expected from your example ($13.95)

  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. //Declare variables
  11. const double PACKAGE_A_RATE = 9.95;
  12. const double PACKAGE_B_RATE = 14.95;
  13. const double PACKAGE_C_RATE = 19.95;
  14.  
  15. char packageA = ' ';
  16. char packageB = ' ';
  17. char packageC = ' ';
  18. string packageName = "";
  19. double packageACharge = 0.0;
  20. double packageBCharge = 0.0;
  21. double packageCCharge = 0.0;
  22. double hours = 0.0;
  23. double totalDue = 0.0;
  24. string customerName = "";
  25.  
  26.  
  27. cout << "Enter your name: ";
  28. cin >> customerName;
  29.  
  30. cout << "Enter the package you purchased: ";
  31. cin >> packageName;
  32.  
  33. cout << "Enter the number of hours used: ";
  34. cin >> hours;
  35.  
  36.  
  37.  
  38. if(hours ==10)
  39. {
  40. packageACharge = PACKAGE_A_RATE;
  41. cout << packageACharge<<endl;
  42. }
  43. else if(hours > 10)
  44. {
  45. //this turned out to be cleaner than the last
  46. packageACharge = ((hours - 10) * 2) + PACKAGE_A_RATE;
  47.  
  48. cout << packageACharge<<endl;
  49.  
  50. }
  51.  
  52. if(hours ==10)
  53. {
  54. packageBCharge = PACKAGE_B_RATE;
  55. cout << packageBCharge<<endl;
  56. }
  57. else if(hours > 10)
  58. {
  59.  
  60. packageBCharge = ((hours - 10) * 2) + PACKAGE_B_RATE;
  61.  
  62. cout << packageBCharge<<endl;
  63.  
  64. }
  65. system("pause");
  66. return 0;
  67. }
Last edited by dvsConcept; Feb 22nd, 2009 at 1:05 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 57
Reputation: cassie_sanford is an unknown quantity at this point 
Solved Threads: 0
cassie_sanford cassie_sanford is offline Offline
Junior Poster in Training

Re: having problem with an if statement...

 
0
  #8
Feb 22nd, 2009
Two questions---
1) if i have a package that is 19.95 per month for unlimited access, how do figure that into the equation?

2) when im ready to output the bill, do i need to output it in every if statement?
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 39
Reputation: dvsConcept is an unknown quantity at this point 
Solved Threads: 0
dvsConcept dvsConcept is offline Offline
Light Poster

Re: having problem with an if statement...

 
0
  #9
Feb 22nd, 2009
Originally Posted by cassie_sanford View Post
Two questions---
1) if i have a package that is 19.95 per month for unlimited access, how do figure that into the equation?

2) when im ready to output the bill, do i need to output it in every if statement?

package C is the unlimited access price?

and you dont have to output in every if statement ill show you once you answer my question about package C
Last edited by dvsConcept; Feb 22nd, 2009 at 1:13 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 57
Reputation: cassie_sanford is an unknown quantity at this point 
Solved Threads: 0
cassie_sanford cassie_sanford is offline Offline
Junior Poster in Training

Re: having problem with an if statement...

 
0
  #10
Feb 22nd, 2009
yes but it cant exceed 744 hours
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC