943,786 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1034
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Feb 22nd, 2009
0

Re: having problem with an if statement...

also...quick question will every if statement be like this:

if(hours == 10)

else if(hours>10) ???
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...

in order for you to determine which price to go with A,B,or C you are going to have to include some sort of comparison within your if(HERE) ill give you an example one sec.........
Last edited by dvsConcept; Feb 22nd, 2009 at 1:20 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...

here's what i have but its not working

 if(hours ==10)    
         {            
                   packageACharge = PACKAGE_A_RATE; 
                   cout <<packageACharge<<endl;   
         }   
          else if(hours > 10)    
          {         
                    packageACharge = (packageACharge +((hours - 10) * 2)) + 
                    PACKAGE_A_RATE; 
                    cout << packageACharge<<endl;     
          } 
          
          else if(hours ==20)    
         {            
                   packageBCharge = PACKAGE_B_RATE; 
                   cout <<packageBCharge<<endl;   
         }   
          else if(hours > 20)    
          {         
                    packageBCharge = (packageBCharge +((hours - 20) * 1)) + 
                    PACKAGE_B_RATE; 
                    cout << packageBCharge<<endl;     
          } 
          
          else if(hours <= 744)    
         {            
                   packageCCharge = PACKAGE_C_RATE; 
                   cout <<packageCCharge<<endl;   
         }   
          else if(hours <= 744)    
          {         
                    packageCCharge = (packageCCharge +((hours - 744) * 2)) + 
                    PACKAGE_C_RATE; 
                    cout << packageACharge<<endl;     
          }
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...

here this should work to your liking hopefully you understand why i implied the switch statement...if not ill explain it to you

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. char packageName = ' ';
  19. double hours = 0.0;
  20. double totalDue = 0.0;
  21. string customerName = "";
  22. double packageCharge = 0.0;//replaced all of your packageA,B,CCharge variables for output purposes
  23.  
  24.  
  25.  
  26. cout << "Enter your name: ";
  27. cin >> customerName;
  28.  
  29. cout << "Enter the package you purchased(a,b,or c): ";
  30. cin >> packageName;
  31.  
  32. cout << "Enter the number of hours used: ";
  33. cin >> hours;
  34.  
  35. switch(packageName)
  36. //not sure if you went over switch/case yet but this is a good example of when to use one
  37. //(also use them for menus)
  38. {
  39. case 'a'://package a chosen by user
  40. if(hours == 10)
  41. {
  42. packageCharge = PACKAGE_A_RATE;
  43.  
  44. }
  45. else if(hours > 10)
  46. {
  47. packageCharge = ((hours - 10) * 2) + PACKAGE_A_RATE;
  48.  
  49. }
  50. break;
  51. case 'b'://package b chosen by user
  52. if(hours == 10)
  53. {
  54. packageCharge = PACKAGE_B_RATE;
  55.  
  56. }
  57. else if(hours > 10)
  58. {
  59. packageCharge = ((hours - 10) * 2) + PACKAGE_B_RATE;
  60.  
  61. }
  62. break;
  63. case 'c'://package c chosen by user
  64. if(hours == 10 && hours <= 744)
  65. {
  66. packageCharge = PACKAGE_C_RATE;
  67.  
  68. }
  69. else if(hours > 744)
  70. {
  71. packageCharge = ((hours - 744) * 2) + PACKAGE_C_RATE;
  72.  
  73. }
  74. break;
  75. }
  76. cout<<packageCharge;
  77. system("pause");
  78. return 0;
  79. }

i used the switch/case to make the program determain which if statement to use depending on what package the user chose.
Last edited by dvsConcept; Feb 22nd, 2009 at 1:33 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...

i see where you're going with it.....

package A--9.95 for 10 hours per month additonal hrs 2.00
package B--14.95 for 20 hours per month additonal hrs 1.00
package C was 19.95 with unlimited access but can't go over 744 hours...


so after the break i can display the user information right?
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...

you could also add a do/while loop to lock the user out from picking any choice other than a,b,c for packageName like this:

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. char packageName = ' ';
  19. double hours = 0.0;
  20. double totalDue = 0.0;
  21. string customerName = "";
  22. double packageCharge = 0.0;//replaced all of your packageA,B,CCharge variables for output purposes
  23.  
  24.  
  25. system("cls");//clear screen
  26. cout << "Enter your name: ";
  27. cin >> customerName;
  28. do
  29. {
  30. cout << "Enter the package you purchased(a,b,or c): ";
  31. cin >> packageName;
  32. }
  33. while(packageName != 'a' && packageName != 'b' && packageName != 'c');//while packageName is not equal(!=) to a,b,or c
  34.  
  35. cout << "Enter the number of hours used: ";
  36. cin >> hours;
  37.  
  38. switch(packageName)
  39. {
  40. case 'a':
  41. if(hours == 10)
  42. {
  43. packageCharge = PACKAGE_A_RATE;
  44.  
  45. }
  46. else if(hours > 10)
  47. {
  48. packageCharge = ((hours - 10) * 2) + PACKAGE_A_RATE;
  49.  
  50. }
  51. break;
  52. case 'b':
  53. if(hours == 20)
  54. {
  55. packageCharge = PACKAGE_B_RATE;
  56.  
  57. }
  58. else if(hours > 20)
  59. {
  60. packageCharge = ((hours - 20) * 2) + PACKAGE_B_RATE;
  61.  
  62. }
  63. break;
  64. case 'c':
  65. if(hours == 10 && hours <= 744)
  66. {
  67. packageCharge = PACKAGE_C_RATE;
  68.  
  69. }
  70. else if(hours > 744)
  71. {
  72. packageCharge = ((hours - 744) * 2) + PACKAGE_C_RATE;
  73.  
  74. }
  75. break;
  76. default:
  77. cout<<"INVALID CHOICE"<<endl;
  78. system("pause");
  79. }
  80.  
  81. cout<<name<<' '<<packageName<<' '<<packageCharge;// display user information for bill
  82. system("pause");
  83. return 0;
  84. }
Last edited by dvsConcept; Feb 22nd, 2009 at 1:45 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...

i'll stick with the switch statement. so would the same formula for package A apply to packages b and C? because im getting squirly answers
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...

Correct the break will take you completely out of the switch/case so than you can display user information for the bill such as:

c++ Syntax (Toggle Plain Text)
  1. cout<<name<<' '<<packageName<<' '<<packageCharge;// display user information for bill
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...

no...the whole point of using the switch was to break out of getting all if statements working at the same time

if you use switch than it will only use the appropriate if statement for the order
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...

OK THIS works perfectly

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. char packageName = ' ';
  19. double hours = 0.0;
  20. double totalDue = 0.0;
  21. string customerName = "";
  22. double packageCharge = 0.0;//replaced all of your packageA,B,CCharge variables for output purposes
  23.  
  24.  
  25. system("cls");//clear screen
  26. cout << "Enter your name: ";
  27. cin >> customerName;
  28. do
  29. {
  30. cout << "Enter the package you purchased(a,b,or c): ";
  31. cin >> packageName;
  32. }
  33. while(packageName != 'a' && packageName != 'b' && packageName != 'c');//while packageName is not equal(!=) to a,b,or c
  34.  
  35. cout << "Enter the number of hours used: ";
  36. cin >> hours;
  37.  
  38. switch(packageName)
  39. {
  40. case 'a':
  41. if(hours == 10)
  42. {
  43. packageCharge = PACKAGE_A_RATE;
  44.  
  45. }
  46. else if(hours > 10)
  47. {
  48. packageCharge = ((hours - 10) * 2) + PACKAGE_A_RATE;
  49.  
  50. }
  51. break;
  52. case 'b':
  53. if(hours == 20)
  54. {
  55. packageCharge = PACKAGE_B_RATE;
  56.  
  57. }
  58. else if(hours > 20)
  59. {
  60. packageCharge = ((hours - 20) * 2) + PACKAGE_B_RATE;
  61.  
  62. }
  63. break;
  64. case 'c':
  65. if(hours <= 744)
  66. {
  67. packageCharge = PACKAGE_C_RATE;
  68.  
  69. }
  70. else if(hours > 744)
  71. {
  72. packageCharge = ((hours - 744) * 2) + PACKAGE_C_RATE;
  73.  
  74. }
  75. break;
  76. default:
  77. cout<<"INVALID CHOICE"<<endl;
  78. system("pause");
  79. }
  80.  
  81. cout<<customerName<<' '<<packageName<<' '<<packageCharge;// display user information for bill
  82. system("pause");
  83. return 0;
  84. }
Reputation Points: 10
Solved Threads: 0
Light Poster
dvsConcept is offline Offline
39 posts
since Jan 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: 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