943,515 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1034
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 21st, 2009
1

having problem with an if statement...

Expand Post »
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?
Similar Threads
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
cassie_sanford is offline Offline
57 posts
since Oct 2008
Feb 22nd, 2009
0

Re: having problem with an if statement...

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)

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
dvsConcept is offline Offline
39 posts
since Jan 2009
Feb 22nd, 2009
0

Re: having problem with an if statement...

can i do the same format for packages b-c?
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
cassie_sanford is offline Offline
57 posts
since Oct 2008
Feb 22nd, 2009
0

Re: having problem with an if statement...

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
dvsConcept is offline Offline
39 posts
since Jan 2009
Feb 22nd, 2009
0

Re: having problem with an if statement...

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
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
cassie_sanford is offline Offline
57 posts
since Oct 2008
Feb 22nd, 2009
0

Re: having problem with an if statement...

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
dvsConcept is offline Offline
39 posts
since Jan 2009
Feb 22nd, 2009
0

Re: having problem with an if statement...

this will end with the answer you expected from your example ($13.95)

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
dvsConcept is offline Offline
39 posts
since Jan 2009
Feb 22nd, 2009
0

Re: having problem with an if statement...

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?
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
cassie_sanford is offline Offline
57 posts
since Oct 2008
Feb 22nd, 2009
0

Re: having problem with an if statement...

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
dvsConcept is offline Offline
39 posts
since Jan 2009
Feb 22nd, 2009
0

Re: having problem with an if statement...

yes but it cant exceed 744 hours
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
cassie_sanford is offline Offline
57 posts
since Oct 2008

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: Character array
Next Thread in C++ Forum Timeline: program with padding a string.





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


Follow us on Twitter


© 2011 DaniWeb® LLC