having problem with an if statement...

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

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
  #11
Feb 22nd, 2009
also...quick question will every if statement be like this:

if(hours == 10)

else if(hours>10) ???
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
  #12
Feb 22nd, 2009
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.
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
  #13
Feb 22nd, 2009
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;     
          }
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
  #14
Feb 22nd, 2009
here this should work to your liking hopefully you understand why i implied the switch statement...if not ill explain it to you

  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.
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
  #15
Feb 22nd, 2009
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?
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
  #16
Feb 22nd, 2009
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:

  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.
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
  #17
Feb 22nd, 2009
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
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
  #18
Feb 22nd, 2009
Correct the break will take you completely out of the switch/case so than you can display user information for the bill such as:

  1. cout<<name<<' '<<packageName<<' '<<packageCharge;// display user information for bill
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
  #19
Feb 22nd, 2009
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
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
  #20
Feb 22nd, 2009
OK THIS works perfectly

  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. }
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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC