Thread: if... else statement errors?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    7

    if... else statement errors?

    I'm having a bit of trouble with the if... else statements. I thought I had it right, but when I start the program... I don't get any results. I'll post the problem and my code and maybe someone could help me out.

    Problem:
    CSC110
    IF…ELSE Program


    Description: The new Telephone Company has the following rate structure for long-distance calls:

    1. The regular rate for a call is $0.10 per minute
    2. Any call started at or after 6:00pm (1800 hours) but before 8:00 am is discounted 50%
    3. Any call longer than 60 minutes receives a 15% discount on its cost (after any other discount is subtracted).
    4. All calls are subject to a 4% federal tax on their final cost.

    Write a program that reads the start time for a call based on a 24-hour clock and the length of the call. Your report should show the gross cost (before any discounts or tax) , the disount (even if it is 0), the tax, and finally the net cost (after discounts are deducted and tax is added).

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    
    {
        // local variable
        int Start_Time;
        int Length_First;
        int Length_Second;
        int Length_Third;
        int End_Time;
        float Total;
        float Gross_Cost; //for gross cost
        float Length_Discount; //for length discount
        float Time_Discount; //for time discount
        float Total_Discounts; //for total discounts
        float Tax; //for tax
        float Reg_Rate = .10;
       
        // local constants
        const int FULL_MIN = 60;
        const int SIX_PM = 1800;
        const int EIGHT_AM = 800;
        const int ADD_HUNDRED = 100;
        const int OVER_HOUR = 0060;
       
    
        // Input Time Of Call
        cout << "What was the time of your phone call?";
        cin  >> Start_Time;
       
        // Input Length Of Minutes
        cout << "What was the length of your phone call in minutes?";
        cin  >> Length_First;
       
        //Clear the screen
        system("cls");
       
        //Calculate Discount  
            Total_Discounts = Length_Discount + Time_Discount;
           
        //Calculate Total
            Total = Gross_Cost - Total_Discounts;
           
        //Calculate Tax
            Tax = Total * 1.1;
       
        //If Statement
    
        if(Length_First >= FULL_MIN) //changes length if over 60 min.
            Length_Second = Length_First % FULL_MIN; //modulus by 60
    {        
           if(Length_Third = Length_Second + ADD_HUNDRED) //adds 100
               End_Time = Length_Third + Start_Time; //calculates total
    
        else if(Length_Third > OVER_HOUR) //starts if length is over 0060
            Length_Discount = Gross_Cost * 0.2; //calculates length discounts
       
        else if(SIX_PM >= Length_Third || Length_Third >= EIGHT_AM)
            Time_Discount = Gross_Cost * 0.5; //calculates time discounts
    }
        // End If
       
                 
       //Display the percent of each model sold
        cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(2);
        cout << "\n\n\n\n\n\n\n\n";
        cout << setw(56) << "Telephone Bill             "  << "\n";
        cout << setw(43) << "Category        " << setw(9)  << "Total"            << "\n\n";
        cout << setw(43) << "Original Gross  " << setw(9)    << Gross_Cost        << endl
             << setw(43) << "Discount        " << setw(9)    << Total_Discounts     << endl
             << setw(43) << "Tax             " << setw(9)  <<  Tax               << endl
             << setw(43) << "Final Gross     " << setw(9)  <<  Total  
             << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                 
       
        //Pause The Output Screen For Viewing
        system("pause");
       
        return 0;
       
    } // End Of Program

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    After the beginning of the first if-statement(Changes length if over 60 minutes), shouldn't the assignment be inside the brackets? Also, in the next if-statement(Add 100) you have an assignment instead of a comparison, consider if that's really what you want. Additionally, use the compiler warnings, you are using some uninitialized variables in your calculations, namely:

    Length_Discount
    Time_Discount
    Gross_Cost

    Not sure if that's everything. Maybe state what the actual problem is?
    Last edited by JacobN; 03-13-2009 at 08:11 PM.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147
    Code:
    if(Length_Third = Length_Second + ADD_HUNDRED) //adds 100
               End_Time = Length_Third + Start_Time; //calculates total

    That's your first problem. You used an assignment operator so no matter what tests you do your Length_Third variable will always equal (Length_Second + ADD_HUNDRED). Use the '==' operator instead.


    Code:
        if(Length_First >= FULL_MIN) //changes length if over 60 min.
            Length_Second = Length_First % FULL_MIN; //modulus by 60
    {        
           if(Length_Third = Length_Second + ADD_HUNDRED) //adds 100
               End_Time = Length_Third + Start_Time; //calculates total
    
        else if(Length_Third > OVER_HOUR) //starts if length is over 0060
            Length_Discount = Gross_Cost * 0.2; //calculates length discounts
       
        else if(SIX_PM >= Length_Third || Length_Third >= EIGHT_AM)
            Time_Discount = Gross_Cost * 0.5; //calculates time discounts
    }
    You've also got a block in your statements here so if your first test ( if(Length_First >= FULL_MIN)) fails nothing below it will be executed (your formatting doesn't make sense so this looks like you may not have had the intention for this behavior).
    Last edited by leeor_net; 03-14-2009 at 12:46 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  2. Header File Errors...
    By Junior89 in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2007, 12:28 AM
  3. Switch statement
    By beene in forum C++ Programming
    Replies: 21
    Last Post: 07-01-2007, 08:13 AM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM