C++ how to make the time look nice? like 60min -> 1hr

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • clairelee0322
    New Member
    • Jan 2008
    • 13

    C++ how to make the time look nice? like 60min -> 1hr

    I am a C++ beginner and I am working on a C++ project that calucates the phone bill.
    I got stuck on how to make every 60 minutes into one hour.. Like if the user enter 1000 (10:00) and it asks for the length of your phone call, if the user enters 70, the program has to print out the result time (end_time) 1110





    here's my program, hope you guys can help me out!!
    Thanks alot!

    [CODE=cpp]#include<iostre am.h>

    main()
    {
    int start_time;
    int length;
    int end_time;
    double gross_cost ;
    double length_discount ;
    double total_discounts ;
    double tax;
    double net_cost;
    double time_discount;
    int length_min, length_hour;
    int end_min, end_hour;
    int call_hour, call_min;

    cout << "What was the start time of your phone call? Write as 1900 instead of 7:00 PM\n";
    cin >> start_time;


    //Any call started at or after 7:00 P.M. (1900 hours) but before 8:00 A.M. (0800 hours) is discounted 60 percent.


    if (start_time >= 1900 || start_time <= 800)
    {
    time_discount = (length * 0.25) * 0.6;
    }

    //Asking for the length.
    cout << "What was the length of your phone call in minutes?\n";
    cin >> length;

    //make time look nice
    length_min = length % 100;
    length_hour = length / 100;

    call_min = start_time % 100;
    call_hour = start_time / 100;

    end_hour = call_hour + length_hour;

    end_min = call_min + length_min;


    if (end_min >= 60)
    {
    end_hour++;
    }

    end_time = (call_hour + length_hour) * (100) + end_min;


    //Any call at least 20 minutes long receives a 30 percent discount on its cost (after any other discount is subtracted).

    gross_cost = length * 0.25;

    if(length >=20)
    {
    length_discount = 0.3 * (gross_cost - time_discount);
    }

    // if time doesn't apply
    else(length <=21);
    {
    length_discount = 0.3 * gross_cost;
    }


    //calculations
    end_time = start_time + length;
    total_discounts = time_discount + length_discount ;
    gross_cost = length * 0.25;
    time_discount = gross_cost * 0.6;
    gross_cost = total_discounts + (tax + net_cost);
    tax = (gross_cost - total_discounts ) * 0.08;
    net_cost = gross_cost + tax - total_discounts ;

    // Print the result.
    cout << "Start Time: " << start_time << '\n';
    cout << "End Time: " << end_time << '\n';
    cout << "Length: " << length << '\n';

    cout.unsetf(ios ::scientific);

    cout << "Gross Cost: " << length * 0.25 << '\n';
    cout << "Time Discount: " << (length * 0.25) * 0.6 << '\n';
    cout << "Length Discount: " << length_discount << '\n';
    cout << "Total Discounts: " << time_discount + length_discount << '\n';

    cout << "Tax: " << (length * 0.25) - (time_discount + length_discount ) * 0.08 << '\n';
    cout << "Net Cost: " << (length * 0.25) + tax - total_discounts << '\n';
    return 0;
    }[/CODE]
    Last edited by Ganon11; Jan 14 '08, 08:26 PM. Reason: Please use the [CODE] tags provided.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    It looks like lines 34-51 are where you've tried to 'make the time look nice'. Can you explain to us what's not working with that section, what is working, what you were trying to do, etc.

    By the way, two notes. First, <iostream.h> is a deprecated header file that has been replaced by <iostream>, so you should be using #include <iostream>. Second, when you write main(), you're implicitly writing void main(), which is non standard and almost 95% of the time will give you errors. Instead use int main(), and return 0; at the end of your program. Not much of a difference to you, but your code will now universally work.

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      Hi,
      One change in the code....
      [code=cpp]
      if (start_time >= 1900 || start_time <= 800)
      {
      time_discount = (length * 0.25) * 0.6;
      }
      cout << "What was the length of your phone call in minutes?\n";
      cin >> length;
      [/code]

      This should be after the code where you ask the user to enter the length like this

      [code=cpp]
      cout << "What was the length of your phone call in minutes?\n";
      cin >> length;

      if (start_time >= 1900 || start_time <= 800)
      {
      time_discount = (length * 0.25) * 0.6;
      }
      [/code]


      I wil spend some more time in the logic and mail u back if i find something

      Thanks
      Raghuram

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        I have made some code changes and i am posting the same.

        [code=cpp]
        #include<iostre am>
        using namespace std;

        int main()
        {
        int start_time;
        int length;
        int end_time;
        double gross_cost ;
        double length_discount ;
        double total_discounts ;
        double tax;
        double net_cost;
        double time_discount;
        int length_min, length_hour;
        int end_min, end_hour;
        int call_hour, call_min;

        cout << "What was the start time of your phone call? Write as 1900 instead of 7:00 PM\n";
        cin >> start_time;


        //Any call started at or after 7:00 P.M. (1900 hours) but before 8:00 A.M. (0800 hours) is discounted 60 percent.
        /*
        if (start_time >= 1900 || start_time <= 800)
        {
        time_discount = (length * 0.25) * 0.6;
        }*/

        //Asking for the length.
        cout << "What was the length of your phone call in minutes?\n";
        cin >> length;

        if (start_time >= 1900 || start_time <= 800)
        {
        time_discount = (length * 0.25) * 0.6;
        }

        //make time look nice
        /*length_min = length % 100;
        length_hour = length / 100; Commented by ME
        */

        length_min = length % 60;
        length_hour = length / 60;

        call_min = start_time % 100;
        call_hour = start_time / 100;

        end_hour = call_hour + length_hour;
        end_min = call_min + length_min;

        if (end_min >= 60)
        {
        end_hour++;
        }

        //end_time = (call_hour + length_hour) * (100) + end_min;//commented by ME

        end_time = (end_hour) * (100) + end_min;


        //Any call at least 20 minutes long receives a 30 percent discount on its cost (after any other discount is subtracted).

        gross_cost = length * 0.25;

        if(length >=20)
        {
        length_discount = 0.3 * (gross_cost - time_discount);
        }

        // if time doesn't apply
        else(length <=21);
        {
        length_discount = 0.3 * gross_cost;
        }


        //calculations
        //end_time = start_time + length;//Commented by ME
        total_discounts = time_discount + length_discount ;
        gross_cost = length * 0.25;
        time_discount = gross_cost * 0.6;
        gross_cost = total_discounts + (tax + net_cost);
        tax = (gross_cost - total_discounts ) * 0.08;
        net_cost = gross_cost + tax - total_discounts ;

        // Print the result.
        cout << "Start Time: " << start_time << '\n';
        cout << "End Time: " << end_time << '\n';
        cout << "Length: " << length/60 << ":" << length%60 <<endl; //changed by ME

        cout.unsetf(ios ::scientific);

        cout << "Gross Cost: " << length * 0.25 << '\n';
        cout << "Time Discount: " << (length * 0.25) * 0.6 << '\n';
        cout << "Length Discount: " << length_discount << '\n';
        cout << "Total Discounts: " << time_discount + length_discount << '\n';

        cout << "Tax: " << (length * 0.25) - (time_discount + length_discount ) * 0.08 << '\n';
        cout << "Net Cost: " << (length * 0.25) + tax - total_discounts << '\n';
        return 0;
        }

        [/code]
        Thanks
        Raghuram

        Comment

        • clairelee0322
          New Member
          • Jan 2008
          • 13

          #5
          -----------------
          Thanks for telling me those things.....So do you mean that I should always use <iostream> instead of <iostream.h> when writing a program? I know that void main () returns no value whereas main() and int main() returns the value zero... Because some of the sample programs in my textbook use main() so I got it mixed with int main() and void main().. Anyways, thank you!! I learned a lot today!

          Comment

          • clairelee0322
            New Member
            • Jan 2008
            • 13

            #6
            Hi Raghuram!
            Thank you for correcting my program..
            I got it!!
            This program must be easy for you but hard for me!!

            thanks,
            Claire

            Comment

            Working...