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

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2007
Posts: 5
Reputation: Amanda21 is an unknown quantity at this point 
Solved Threads: 0
Amanda21 Amanda21 is offline Offline
Newbie Poster

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

 
0
  #1
Mar 15th, 2007
  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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

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

 
0
  #2
Mar 15th, 2007
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:
  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:
  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 5
Reputation: Amanda21 is an unknown quantity at this point 
Solved Threads: 0
Amanda21 Amanda21 is offline Offline
Newbie Poster

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

 
0
  #3
Mar 20th, 2007
Thank you so much i really appreciate your help and will try to do better on my future posts!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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