GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Dec-2008, 12:22
amorr amorr is offline
New Member
 
Join Date: Dec 2008
Posts: 2
amorr is on a distinguished road

C++ program to calculate time remaining


Hi there. My nephew is taking a required course in C++. I, however, only know Visual Basic and am having a hard time helping him. I could use your help. Here is his assignment:

I noticed that when the code has :: it gives an emoticon. Sorry!

For the assignment modify the three files that we used for the C++ example that involved time. Add the ability to create a deadline. Your software should provide the user with the capability of setting a deadline, inspecting a deadline, and calculating the amount of time remaining between the current time (the time defined already in the class) and the deadline.

---------------------------------
Here are the given files:
time2.cpp:
CPP / C++ / C Code:
{ 
   setTime( hr, min, sec );  // validate and set time

} // end Time constructor

// set new Time value using universal time, perform validity
// checks on the data values and set invalid values to zero
void Time: :setTime( int h, int m, int s )
{
   hour = ( h >= 0 && h < 24 ) ? h : 0;
   minute = ( m >= 0 && m < 60 ) ? m : 0;
   second = ( s >= 0 && s < 60 ) ? s : 0;

} // end function setTime

// print Time in universal format
void Time: :printUniversal()
{
   cout << setfill( '0' ) << setw( 2 ) << hour << ":"
        << setw( 2 ) << minute << ":"
        << setw( 2 ) << second;

} // end function printUniversal

// print Time in standard format
void Time: :printStandard()
{
   cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
        << ":" << setfill( '0' ) << setw( 2 ) << minute
        << ":" << setw( 2 ) << second 
        << ( hour < 12 ? " AM" : " PM" );

} // end function printStandard
time2h.txt:

CPP / C++ / C Code:
// timeTest.cpp 
// Demonstrating a default constructor for class Time.
#include <iostream>

using namespace std;

// include definition of class Time from time2.h
#include "time2.h"

int main()
{
   Time t1;               // all arguments defaulted
   Time t2( 2 );          // minute and second defaulted
   Time t3( 21, 34 );     // second defaulted 
   Time t4( 12, 25, 42 ); // all values specified
   Time t5( 27, 74, 99 ); // all bad values specified

   cout << "Constructed with:\n\n"
        << "all default arguments:\n  ";
   t1.printUniversal();  // 00:00:00
   cout << "\n  ";
   t1.printStandard();   // 12:00:00 AM

   cout << "\n\nhour specified; default minute and second:\n  ";
   t2.printUniversal();  // 02:00:00
   cout << "\n  ";
   t2.printStandard();   // 2:00:00 AM

   cout << "\n\nhour and minute specified; default second:\n  ";
   t3.printUniversal();  // 21:34:00
   cout << "\n  ";
   t3.printStandard();   // 9:34:00 PM

   cout << "\n\nhour, minute, and second specified:\n  ";
   t4.printUniversal();  // 12:25:42
   cout << "\n  ";
   t4.printStandard();   // 12:25:42 PM

   cout << "\n\nall invalid values specified:\n  ";
   t5.printUniversal();  // 00:00:00
   cout << "\n  ";
   t5.printStandard();   // 12:00:00 AM
   cout << endl;

	// keep output visible
   char anychar;
   cout << "\n press any key and then the enter key to finish ";
   cin >> anychar;

   return 0;

} // end main

timetest.txt:

CPP / C++ / C Code:
// timeTest.cpp 
// Demonstrating a default constructor for class Time.
#include <iostream>

using namespace std;

// include definition of class Time from time2.h
#include "time2.h"

int main()
{
   Time t1;               // all arguments defaulted
   Time t2( 2 );          // minute and second defaulted
   Time t3( 21, 34 );     // second defaulted 
   Time t4( 12, 25, 42 ); // all values specified
   Time t5( 27, 74, 99 ); // all bad values specified

   cout << "Constructed with:\n\n"
        << "all default arguments:\n  ";
   t1.printUniversal();  // 00:00:00
   cout << "\n  ";
   t1.printStandard();   // 12:00:00 AM

   cout << "\n\nhour specified; default minute and second:\n  ";
   t2.printUniversal();  // 02:00:00
   cout << "\n  ";
   t2.printStandard();   // 2:00:00 AM

   cout << "\n\nhour and minute specified; default second:\n  ";
   t3.printUniversal();  // 21:34:00
   cout << "\n  ";
   t3.printStandard();   // 9:34:00 PM

   cout << "\n\nhour, minute, and second specified:\n  ";
   t4.printUniversal();  // 12:25:42
   cout << "\n  ";
   t4.printStandard();   // 12:25:42 PM

   cout << "\n\nall invalid values specified:\n  ";
   t5.printUniversal();  // 00:00:00
   cout << "\n  ";
   t5.printStandard();   // 12:00:00 AM
   cout << endl;

	// keep output visible
   char anychar;
   cout << "\n press any key and then the enter key to finish ";
   cin >> anychar;

   return 0;

} // end main
Last edited by LuciWiz : 01-Dec-2008 at 12:31. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 02-Dec-2008, 16:04
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: C++ program to calculate time remaining


Quote:
Originally Posted by amorr
... nephew is taking a required course in C++. I, however, only know Visual Basic
So: It's a learning opportunity for you, right?
Quote:
Originally Posted by amorr
...could use your help...Add the ability to create a deadline.

It is not clear whether the assignment is to add another class or add a data member to the existing class or to use the present classes and make two instances of Time objects: One would be "current time" and the other would be the "deadline."

In any case you will have to subtract current time from the deadline to calculate the remaining time.

Additional new features will be stuff to, somehow, read time information from the user and either use it in a constructor, or to have class functions that use this to update the fields in the Time objects.

So: where would you like to start?

I like to think about how the information, "current time" and "deadline time," can be used to calculate time remaining.

One possibility:

1. Convert hours:minutes:seconds since midnight to seconds since midnight for both time values.

2. Subtract the "current" seconds from "deadline" seconds.

3. Convert the result back to hours:minutes:seconds.

This immediately raises a question: Are we always going to assume that "current" time and "deadline" time are on the same day? If not, then we also have to take days into account. Since you didn't mention it, I will assume that they are both on the same day.

My point is that when you write a program like this (or like anything else), think about what the program is supposed to do. It's embarrassing to spend lots of time on a program and then find that there are some basic limitations that keep it from doing what you had in mind (or discovering that what you had in mind simply isn't do-able with the structure given---what if the deadline is supposed to be next Tuesday?)

Here's the "bottom-up" approach: Make sure you can enter data and do calculations:
CPP / C++ / C Code:
//
// Showing time entry and calculations
// Note that there should (probably) be validation of input values
//
#include <iostream>
#include <iomanip>

using namespace std;

const int SECONDS_PER_MINUTE = 60;
const int SECONDS_PER_HOUR   = 3600;

int calc_seconds(int h, int m, int s);
int main()
{


    int hours, minutes, seconds;
    int total_current_seconds;
    int total_deadline_seconds;

    while (1) {
        cout <<  "Enter current time in hours, minutes, and seconds: ";
        cin >> hours >> minutes >> seconds;
        if (!cin || hours < 0 || minutes < 0 || seconds < 0) {
            cout << "Goodbye." << endl;
            break;
        }
        cout << setfill('0');
        cout << "You entered " << setw(2) << hours
             << ":" << setw(2) << minutes
             << ":" << setw(2) << seconds
             << endl;
        total_current_seconds = calc_seconds(hours, minutes, seconds);
        cout << "Current time in seconds since midnight = " 
             << total_current_seconds << endl << endl;


        cout <<  "Enter deadline time in hours, minutes, and seconds: ";
        cin >> hours >> minutes >> seconds;
        if (!cin || hours < 0 || minutes < 0 || seconds < 0) {
            cout << "Goodbye." << endl;
            break;
        }
        cout << "You entered " << setw(2) << hours
             << ":" << setw(2) << minutes
             << ":" << setw(2) << seconds
             << endl;
        total_deadline_seconds = calc_seconds(hours, minutes, seconds);
        cout << "Deadline time in seconds since midnight = " 
             << total_deadline_seconds << endl << endl;
        cout << "Difference = " 
             << total_deadline_seconds - total_current_seconds << endl << endl;
        // If you need to: 
        // Convert the difference back to hours, minutes, seconds Using
        // using the modulus operator %
    }
    return 0;
}
int calc_seconds(int h, int m, int s)
{
     return (h * SECONDS_PER_HOUR) + (m * SECONDS_PER_MINUTE) + s;
}

A run:
Code:
Enter current time in hours, minutes, and seconds: 1 2 3 You entered 01:02:03 Current time in seconds since midnight = 3723 Enter deadline time in hours, minutes, and seconds: 4 5 6 You entered 04:05:06 Deadline time in seconds since midnight = 14706 Difference = 10983 Enter current time in hours, minutes, and seconds: quit Goodbye.

Regards,

Dave

Footnote: Instead of converting to seconds, you could do modulo-time subtraction:
Subtract seconds. If the result is less than zero, then add 60 and set a "borrow" flag to 1, else set the "borrow" flag to zero.

Subtract minutes and subtract the borrow flag. If the result is less than 0 , then add 60 and set the "borrow" flag to 1, else set the "borrow" flag to zero.

Etc.

If I were designing the classes, I probably would just keep everything in seconds and convert from/to hours:minutes:seconds as required for input/output operations. For many (most?) computers these days, the date/time inside the computer is maintained as number that counts the number of seconds from some arbitrary "time zero" known as the "epoch."
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Python - Need help with time card script/wage calculator sigkill9 Python Forum 5 12-Mar-2008 22:01
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 11:13
Hard Time W/ Program Kres10_98 C++ Forum 5 09-Aug-2007 10:57
My randomly generated quote program monalin C++ Forum 4 18-Oct-2005 17:18
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 08:10

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:44.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.