473,799 Members | 3,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

13 New Member
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 1892
Ganon11
3,652 Recognized Expert Specialist
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 Recognized Expert Top Contributor
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 Recognized Expert Top Contributor
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
clairelee0322
13 New Member
-----------------
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
clairelee0322
13 New Member
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
4013
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 sleep for 1 second after indexing a couple of files. I'm wondering if anyone knows of a way that I could make so that the program will run at full speed only runs after the computer has been idle for a while. I've looked at the "nice" command...
2
1686
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 - 03:59:59 etc etc etc. DB Fields look like this
77
4614
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 process starts is not especially important, but it must be complete within a small number of seconds. The operations I am performing do not take a long time (hundreds of milliseconds), but as each part of the process is complete, my worker thread...
7
5951
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 the idea: static int counter = 0; // this variable can be changed only at runtime... template <typename T> struct want_to_be_counted;
5
9105
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 terminate the sleep() following execution > of that signal's catching routine. Also, the suspension time may > be longer than requested by an arbitrary amount because of the > scheduling of other activity in the system. I don't understand the...
12
6566
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 and have the graph update every second without the user having to do anything like hit a refresh button. The data to plot is readily available from an application running on the server - I can expose it in whatever way is needed (currently easily...
38
2566
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 lead(I have just started working) he said we should not use dynamic allocation in real time systems, the code will not run in predictable time period.Can anybody tell what does he mean?Why the execution time becomes unpredictable? Thanks
5
3532
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 BASIC, QBASIC etc that is very useful except that it really doesn't like to run on modern operating systems and has hopeless graphics resolution and lack of ease of use in some ways.
20
2222
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 selected the controls look nice rounded, etc. just like in Windows XP. But if I run the application on a Windows XP computer having the Windows
2
1115
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 available. Currently, the page renders after all controls/webparts are loaded. Then when there are postbacks, the updatepanels work great. Is there a way to do this with theses controls (built-in ajax.net type stuff) or will I need to finally...
0
9688
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10490
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10259
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9077
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.