944,113 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2679
  • C++ RSS
Mar 15th, 2007
0

Must read file from while loop with nested switch and if/else, why won't it compile??

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. const double PRICEA = 9.95;
  6. const double PRICEB = 14.95;
  7. const double PRICEC = 19.95;
  8. const int ONE = 10;
  9. const int TWO = 20;
  10. const double EXTRA1 = 2.00;
  11. const double EXTRA2 = 1.00;
  12.  
  13. int main()
  14. {
  15. int hours;
  16. char pkgType;
  17. double cost;
  18.  
  19. ifstream infile;
  20.  
  21. infile.open("pa5.input");
  22. if(infile.fail())
  23. {
  24. cout<<"Error Opening File." <<endl;
  25. exit(1);
  26. }
  27. infile >> pkgType >> hours;
  28.  
  29.  
  30. while (infile)
  31. {
  32. switch (pkgType)
  33. {
  34. case 'A':
  35.  
  36. if (hours > ONE )
  37.  
  38. cout << "Package A " << PRICEA << hours
  39. << cost << endl;
  40. cost = PRICEA + ((hours - ONE) * EXTRA1);
  41.  
  42. break;
  43.  
  44. else if (hours <= ONE)
  45. {
  46. cout << "Package A " << PRICEA << hours
  47. << cost << endl;
  48. cost = PRICEA;
  49. }
  50. break;
  51.  
  52. case 'B':
  53.  
  54. if (hours > TWO )
  55. {
  56. cout << "Package B "<< PRICEB << hours
  57. << cost << endl;
  58. cost = PRICEB + ((hours - TWO) * EXTRA2);
  59. }
  60. break;
  61.  
  62. else if
  63. {
  64. cout << "Package B "<< PRICEB << hours
  65. << cost << endl;
  66. cost = PRICEB;
  67. }
  68. break;
  69.  
  70. case 'C':
  71.  
  72. cout <<"Package C "<< PRICEC << hours
  73. << cost << endl;
  74. cost = PRICEC;
  75.  
  76. break;
  77.  
  78. default: cout << endl << "Invalid Package Type! " << endl;
  79. }
  80. }
  81.  
  82.  
  83.  
  84. system("pause");
  85. return 0;
  86. }
Last edited by ~s.o.s~; Mar 15th, 2007 at 11:03 pm. Reason: Added code tags. Learn to use them.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Amanda21 is offline Offline
5 posts
since Mar 2007
Mar 15th, 2007
0

Re: Must read file from while loop with nested switch and if/else, why won't it compi

You seem to be leaving out braces on your if statements. It was really easy to find with the code auto-indented. Here's a code excerpt:
C++ Syntax (Toggle Plain Text)
  1. switch (pkgType)
  2. {
  3. case 'A':
  4. if (hours > ONE )
  5. cout << "Package A " << PRICEA << hours
  6. << cost << endl;
  7. cost = PRICEA + ((hours - ONE) * EXTRA1); // this will always get run
  8. break; // this will always get run
  9. // everything below this is unreachable code
  10. else if (hours <= ONE)
  11. {
  12. cout << "Package A " << PRICEA << hours
  13. << cost << endl;
  14. cost = PRICEA;
  15. }
  16. break;
That happens in all the case statements. I think you want something like this:
C++ Syntax (Toggle Plain Text)
  1. switch (pkgType)
  2. {
  3. case 'A':
  4. if (hours > ONE )
  5. {
  6. cout << "Package A " << PRICEA << hours
  7. << cost << endl;
  8. cost = PRICEA + ((hours - ONE) * EXTRA1);
  9. }
  10. else if (hours <= ONE)
  11. {
  12. cout << "Package A " << PRICEA << hours
  13. << cost << endl;
  14. cost = PRICEA;
  15. }
  16. break;
which could be further reduced to this:
C++ Syntax (Toggle Plain Text)
  1. switch (pkgType)
  2. {
  3. case 'A':
  4. cout << "Package A " << PRICEA << hours
  5. << cost << endl; // this line was the same in both the if and else
  6. if (hours > ONE )
  7. {
  8. cost = PRICEA + ((hours - ONE) * EXTRA1);
  9. }
  10. else if (hours <= ONE)
  11. {
  12. cost = PRICEA;
  13. }
  14. break;
btw, for future posts, please put your code between [code] and [/code] tags to preserve formatting for the rest of us. Also, posting the error message is usually quite helpful
Last edited by Infarction; Mar 15th, 2007 at 4:58 pm.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Mar 20th, 2007
0

Re: Must read file from while loop with nested switch and if/else, why won't it compile??

Thank you so much i really appreciate your help and will try to do better on my future posts!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Amanda21 is offline Offline
5 posts
since Mar 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: C++
Next Thread in C++ Forum Timeline: help with c++ vectors





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


Follow us on Twitter


© 2011 DaniWeb® LLC