#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const double PRICEA = 9.95;
const double PRICEB = 14.95;
const double PRICEC = 19.95;
const int ONE = 10;
const int TWO = 20;
const double EXTRA1 = 2.00;
const double EXTRA2 = 1.00;

int main()
{
 int hours;
    char pkgType;
 double cost;
 
 ifstream infile;
 
 infile.open("pa5.input");
 if(infile.fail())
 {
           cout<<"Error Opening File." <<endl;
           exit(1);
    }
 infile >> pkgType >> hours;
 
 
 while (infile)
 {
          switch (pkgType)
          {
          case 'A': 
                     
             if (hours > ONE )
                        
                   cout << "Package A     " << PRICEA    << hours      
                   <<          cost << endl;
                   cost = PRICEA + ((hours - ONE) * EXTRA1);
             
                   break;
                                                                                                                                
              else if (hours <= ONE)
              {           
                   cout << "Package A    " << PRICEA    << hours
                   <<  cost  << endl;    
                   cost = PRICEA;
              }
                   break;
                                            
           case 'B': 
                     
                 if (hours > TWO )
                 {        
                     cout << "Package B     "<< PRICEB     << hours
                     << cost  << endl;
                     cost = PRICEB + ((hours - TWO) * EXTRA2);
                 }                                                                                                                  
                      break;
                                        
                 else if
                 {        
                      cout << "Package B     "<< PRICEB     << hours
                      << cost  << endl;
                      cost = PRICEB;
                 }
                      break;
                         
           case 'C': 
                     
                 cout <<"Package C     "<< PRICEC     << hours   
                 << cost  << endl;
                 cost = PRICEC;
                 
                 break;
                     
           default: cout << endl << "Invalid Package Type! " << endl;
           }               
    }                     
           
    
    
system("pause");
return 0;
}

Recommended Answers

All 2 Replies

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:

switch (pkgType)
    {
      case 'A':
        if (hours > ONE )
          cout << "Package A " << PRICEA << hours
            << cost << endl;
        cost = PRICEA + ((hours - ONE) * EXTRA1);  // this will always get run
        break; // this will always get run
        // everything below this is unreachable code
        else if (hours <= ONE)
        {
          cout << "Package A " << PRICEA << hours
            << cost << endl;
          cost = PRICEA;
        }
        break;

That happens in all the case statements. I think you want something like this:

switch (pkgType)
    {
      case 'A':
        if (hours > ONE )
        {
          cout << "Package A " << PRICEA << hours
            << cost << endl;
          cost = PRICEA + ((hours - ONE) * EXTRA1);
        }
        else if (hours <= ONE)
        {
          cout << "Package A " << PRICEA << hours
            << cost << endl;
          cost = PRICEA;
        }
        break;

which could be further reduced to this:

switch (pkgType)
    {
      case 'A':
        cout << "Package A " << PRICEA << hours
             << cost << endl; // this line was the same in both the if and else
        if (hours > ONE )
        {
          cost = PRICEA + ((hours - ONE) * EXTRA1);
        }
        else if (hours <= ONE)
        {
          cost = PRICEA;
        }
        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 ;)

Thank you so much i really appreciate your help and will try to do better on my future posts! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.