473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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!

Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2.  
  3. main()
  4. {
  5.     int start_time;
  6.     int length;
  7.     int end_time;
  8.     double gross_cost ;
  9.     double length_discount ;
  10.     double total_discounts ;
  11.     double tax;
  12.     double net_cost;
  13.     double time_discount;
  14.         int length_min, length_hour;
  15.     int end_min, end_hour;
  16.     int call_hour, call_min;
  17.  
  18.     cout << "What was the start time of your phone call? Write as 1900 instead of 7:00 PM\n";
  19.     cin >> start_time;
  20.  
  21.  
  22. //Any call started at or after 7:00 P.M. (1900 hours) but before 8:00 A.M. (0800 hours) is discounted 60 percent. 
  23.  
  24.  
  25.     if (start_time >= 1900 || start_time <= 800)
  26.     {
  27.         time_discount = (length * 0.25) * 0.6;
  28.     }
  29.  
  30. //Asking for the length.
  31.     cout << "What was the length of your phone call in minutes?\n";
  32.     cin >> length;
  33.  
  34. //make time look nice
  35.     length_min = length % 100;
  36.     length_hour = length / 100;
  37.  
  38.     call_min = start_time % 100;
  39.     call_hour = start_time / 100;
  40.  
  41.     end_hour = call_hour + length_hour;
  42.  
  43.     end_min = call_min + length_min;
  44.  
  45.  
  46.     if (end_min >= 60)
  47.     {
  48.         end_hour++;
  49.     }
  50.  
  51.     end_time = (call_hour + length_hour) * (100) + end_min;
  52.  
  53.  
  54. //Any call at least 20 minutes long receives a 30 percent discount on its cost (after any other discount is subtracted).
  55.  
  56.     gross_cost = length * 0.25;
  57.  
  58.     if(length >=20) 
  59.     {    
  60.         length_discount = 0.3 * (gross_cost - time_discount);
  61.     }
  62.  
  63. // if time doesn't apply
  64.     else(length <=21);
  65.     {
  66.         length_discount = 0.3 * gross_cost;
  67.     }
  68.  
  69.  
  70. //calculations
  71.     end_time = start_time + length;
  72.     total_discounts = time_discount + length_discount;
  73.     gross_cost = length * 0.25;
  74.     time_discount = gross_cost * 0.6;
  75.     gross_cost = total_discounts + (tax + net_cost);
  76.     tax = (gross_cost - total_discounts) * 0.08;
  77.     net_cost = gross_cost + tax - total_discounts;
  78.  
  79. // Print the result.
  80.     cout << "Start Time: " << start_time << '\n';
  81.     cout << "End Time: " << end_time << '\n';
  82.     cout << "Length: " << length << '\n';
  83.  
  84.     cout.unsetf(ios::scientific);
  85.  
  86.     cout << "Gross Cost: " << length * 0.25 << '\n';
  87.     cout << "Time Discount: " << (length * 0.25) * 0.6 << '\n';
  88.     cout << "Length Discount: " << length_discount << '\n';
  89.     cout << "Total Discounts: " << time_discount + length_discount << '\n';
  90.  
  91.     cout << "Tax: " << (length * 0.25) - (time_discount + length_discount) * 0.08 << '\n';
  92.     cout << "Net Cost: " << (length * 0.25) + tax - total_discounts << '\n';
  93.     return 0;
  94. }
Jan 14 '08 #1
5 1876
Ganon11
3,652 Expert 2GB
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.
Jan 14 '08 #2
gpraghuram
1,275 Expert 1GB
Hi,
One change in the code....
Expand|Select|Wrap|Line Numbers
  1. if (start_time >= 1900 || start_time <= 800)
  2.     {
  3.         time_discount = (length * 0.25) * 0.6;
  4.     }
  5.     cout << "What was the length of your phone call in minutes?\n";
  6.     cin >> length;
  7.  
This should be after the code where you ask the user to enter the length like this

Expand|Select|Wrap|Line Numbers
  1.     cout << "What was the length of your phone call in minutes?\n";
  2.     cin >> length;
  3.  
  4.     if (start_time >= 1900 || start_time <= 800)
  5.     {
  6.         time_discount = (length * 0.25) * 0.6;
  7.     }
  8.  

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

Thanks
Raghuram
Jan 15 '08 #3
gpraghuram
1,275 Expert 1GB
I have made some code changes and i am posting the same.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     int start_time;
  7.     int length;
  8.     int end_time;
  9.     double gross_cost ;
  10.     double length_discount ;
  11.     double total_discounts ;
  12.     double tax;
  13.     double net_cost;
  14.     double time_discount;
  15.         int length_min, length_hour;
  16.     int end_min, end_hour;
  17.     int call_hour, call_min;
  18.  
  19.     cout << "What was the start time of your phone call? Write as 1900 instead of 7:00 PM\n";
  20.     cin >> start_time;
  21.  
  22.  
  23. //Any call started at or after 7:00 P.M. (1900 hours) but before 8:00 A.M. (0800 hours) is discounted 60 percent.
  24. /*
  25.     if (start_time >= 1900 || start_time <= 800)
  26.     {
  27.         time_discount = (length * 0.25) * 0.6;
  28.     }*/
  29.  
  30. //Asking for the length.
  31.     cout << "What was the length of your phone call in minutes?\n";
  32.     cin >> length;
  33.  
  34.     if (start_time >= 1900 || start_time <= 800)
  35.     {
  36.        time_discount = (length * 0.25) * 0.6;
  37.     }
  38.  
  39. //make time look nice
  40.     /*length_min = length % 100;
  41.     length_hour = length / 100; Commented by ME
  42.     */
  43.  
  44.     length_min = length % 60;
  45.     length_hour = length / 60;
  46.  
  47.     call_min = start_time % 100;
  48.     call_hour = start_time / 100;
  49.  
  50.     end_hour = call_hour + length_hour;
  51.     end_min = call_min + length_min;
  52.  
  53.     if (end_min >= 60)
  54.     {
  55.         end_hour++;
  56.     }
  57.  
  58.     //end_time = (call_hour + length_hour) * (100) + end_min;//commented by ME
  59.  
  60.     end_time = (end_hour) * (100) + end_min;
  61.  
  62.  
  63. //Any call at least 20 minutes long receives a 30 percent discount on its cost (after any other discount is subtracted).
  64.  
  65.     gross_cost = length * 0.25;
  66.  
  67.     if(length >=20)
  68.     {
  69.         length_discount = 0.3 * (gross_cost - time_discount);
  70.     }
  71.  
  72. // if time doesn't apply
  73.     else(length <=21);
  74.     {
  75.         length_discount = 0.3 * gross_cost;
  76.     }
  77.  
  78.  
  79. //calculations
  80.     //end_time = start_time + length;//Commented by ME
  81.     total_discounts = time_discount + length_discount;
  82.     gross_cost = length * 0.25;
  83.     time_discount = gross_cost * 0.6;
  84.     gross_cost = total_discounts + (tax + net_cost);
  85.     tax = (gross_cost - total_discounts) * 0.08;
  86.     net_cost = gross_cost + tax - total_discounts;
  87.  
  88. // Print the result.
  89.     cout << "Start Time: " << start_time << '\n';
  90.     cout << "End Time: " << end_time << '\n';
  91.     cout << "Length: " << length/60 << ":" << length%60 <<endl; //changed by ME
  92.  
  93.     cout.unsetf(ios::scientific);
  94.  
  95.     cout << "Gross Cost: " << length * 0.25 << '\n';
  96.     cout << "Time Discount: " << (length * 0.25) * 0.6 << '\n';
  97.     cout << "Length Discount: " << length_discount << '\n';
  98.     cout << "Total Discounts: " << time_discount + length_discount << '\n';
  99.  
  100.     cout << "Tax: " << (length * 0.25) - (time_discount + length_discount) * 0.08 << '\n';
  101.     cout << "Net Cost: " << (length * 0.25) + tax - total_discounts << '\n';
  102.     return 0;
  103. }
  104.  
  105.  
Thanks
Raghuram
Jan 15 '08 #4
-----------------
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!
Jan 15 '08 #5
Hi Raghuram!
Thank you for correcting my program..
I got it!!
This program must be easy for you but hard for me!!

thanks,
Claire
Jan 15 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: los | last post by:
Hi, I'm trying to create a program similar to that of Google's desktop that will crawl through the hard drive and index files. I have written the program and as of now I just put the thread to...
2
by: Stuart | last post by:
Hi all, I have a problem that I just cannot figure out for the life of me. I am trying to set up a database that contains time slots (6 X 4hr periods to cover the day) starting from 00:00:00...
77
by: Charles Law | last post by:
Hi guys I have a time critical process, running on a worker thread. By "time critical", I mean that certain parts of the process must be completed in a specific time frame. The time when the...
7
by: Patrick Kowalzick | last post by:
Dear all, I just wondered if it is possible to count the number of classes created via a template class at compile time. To show what I mean I post an example, which is not working but carries...
5
by: Erich Schreiber | last post by:
In the Python Library Reference the explanation of the time.sleep() function reads amongst others: > The actual suspension time may be less than that requested because > any caught signal will...
12
by: Russ | last post by:
I'm interested in setting up a web page where live data can be displayed in real-time on the web page. For example: I would like to display a (nice looking) graph of some data value versus time...
38
by: vashwath | last post by:
Might be off topic but I don't know where to post this question.Hope some body clears my doubt. The coding standard of the project which I am working on say's not to use malloc.When I asked my...
5
by: Ray Tomes | last post by:
Hi Folks I am an old codger who has much experience with computers in the distant past before all this object oriented stuff. Also I have loads of software in such languages as FORTRAN and...
20
by: Maurice | last post by:
Hi all, I have an application, designed in Visual Studio 2005, which will run mainly on Windows XP computers. If I run the application on a Windows XP computer having the Windows XP Theme...
2
by: Ryan | last post by:
Hello everyone, Here is what I would like to accomplish, I have a webpart 'portal' where each webpart is in an updatepanel. I would like the page to load and the webparts to load as they become...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.