1>------ Build started: Project: Enhancing class time, Configuration: Debug Win32 ------
1> Ex09_07.cpp
1>..\Ex09_07.cpp(3): fatal error C1083: Cannot open include file: 'stdafx.h': No such file or directory
1> Time.cpp
1>c:\users\jonathan\documents\visual studio 2010\projects\exercise9.7\exercise9.7\Time.h(28): warning C4520: 'Time' : multiple default constructors specified
1>..\exercise9.7\exercise9.7\Time.cpp(35): warning C4244: 'initializing' : conversion from 'time_t' to 'int', possible loss of data
1>..\exercise9.7\exercise9.7\Time.cpp(76): error C2039: 'cout' : is not a member of 'std'
1>..\exercise9.7\exercise9.7\Time.cpp(76): error C2065: 'cout' : undeclared identifier
1>..\exercise9.7\exercise9.7\Time.cpp(76): error C3861: 'setfill': identifier not found
1>..\exercise9.7\exercise9.7\Time.cpp(76): error C3861: 'setw': identifier not found
1>..\exercise9.7\exercise9.7\Time.cpp(77): error C3861: 'setw': identifier not found
1>..\exercise9.7\exercise9.7\Time.cpp(77): error C3861: 'setw': identifier not found
1>..\exercise9.7\exercise9.7\Time.cpp(83): error C2065: 'cout' : undeclared identifier
1>..\exercise9.7\exercise9.7\Time.cpp(84): error C3861: 'setfill': identifier not found
1>..\exercise9.7\exercise9.7\Time.cpp(84): error C3861: 'setw': identifier not found
1>..\exercise9.7\exercise9.7\Time.cpp(84): error C3861: 'setw': identifier not found
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
stdafx.h
#include <iostream>
#include <cstdlib>
#include "Time.h"
#include <iomanip>
#include "stdlib.h"
using std::cout;
using std::endl;
using std::cin;
using std::atexit;
using std::exit;
const int MAX_TICKS = 30;
//ref class Time::setTime( 23, 59, 57 ); // set time
int main()
{
Time t; // create Time object
t.setTime (23,59,57);
// output Time object t's values
for ( int ticks = 1; ticks < MAX_TICKS; ++ticks )
{
Time.printStandard(); // invokes function printStandard
cout << endl;
Time.tick(); // invokes function tick
} // end for
cout<<"enter 1 to terminate prgm. with function exit";
cout<<"\nEnter 2 to terminate prog. normally";
int answer;
cin>>answer;
if (answer==1)
{
exit(EXIT_SUCCESS);
}
return 0;
} // end main
// Exercise 9.7 Solution: Time.cpp
// Member-function definitions for class Time.
#pragma once
#include "stdafx.h"
using namespace System;
#include <ctime>
using std::time;
#include "Time.h" // include definition of class Time from Time.h
Time::Time(int hour, int minute, int second)
{
const int CURRENT_YEAR = 2004;
const int START_YEAR = 1970;
const int HOURS_IN_A_DAY = 24;
const int MINUTES_IN_AN_HOUR = 60;
const int SECONDS_IN_A_MINUTE = 60;
const int DAYS_IN_A_YEAR = 365;
const int DAYS_IN_A_LEAPYEAR = 366;
const int TIMEZONE_DIFFERENCE = 5;
int leapYear = 0;
int days;
setTime(hour, minute, second);
// calculate leap year
for ( int y = START_YEAR; y <= CURRENT_YEAR; y++ )
{
if ( isLeapYear( y ) )
leapYear++;
} // end for
// calculate current second, minute and hour
for ( int s = 0; s < SECONDS_IN_A_MINUTE; s++ )
{
for ( int m = 0; m < MINUTES_IN_AN_HOUR; m++ )
{
for ( int h = 0; h <= HOURS_IN_A_DAY; h++ )
{
if ( isLeapYear( CURRENT_YEAR ) )
days = DAYS_IN_A_LEAPYEAR;
else
days = DAYS_IN_A_YEAR;
for ( int d = 0; d <= days; d++ )
{
if ( s + m * SECONDS_IN_A_MINUTE +
h * MINUTES_IN_AN_HOUR * SECONDS_IN_A_MINUTE +
d * HOURS_IN_A_DAY * MINUTES_IN_AN_HOUR *
SECONDS_IN_A_MINUTE == dayTimeInSeconds )
{
setTime( h-TIMEZONE_DIFFERENCE, m, s );
} // end if
} // end for
} // end for
} // end for
} // end for
} // end Time constructor
// set new Time value using universal time; ensure that
// the data remains consistent by setting invalid values to zero
void Time::setTime( int h, int m, int s )
{
hour = ( h >= 0 && h < 24 ) ? h : 0; // validate hour
minute = ( m >= 0 && m < 60 ) ? m : 0; // validate minute
second = ( s >= 0 && s < 60 ) ? s : 0; // validate second
} // end function setTime
// print Time in universal-time format (HH:MM:SS)
void Time::printUniversal()
{
std::cout << setfill( '0' ) << setw( 2 ) << hour << ":"
<< setw( 2 ) << minute << ":" << setw( 2 ) << second;
} // end function printUniversal
// print Time in standard-time format (HH:MM:SS AM or PM)
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
// check if a year is a leap year
void Time::tick()
{
setSecond( getSecond() + 1 ); // increment second by 1
if ( getSecond() == 0 )
{
setMinute( getMinute() + 1 ); // increment minute by 1
if ( getMinute() == 0 )
setHour( getHour() + 1 ); // increment hour by 1
} // end if
} // end function tick
bool Time::isLeapYear( int y )
{
if ( ( y % 400 == 0 ) || ( ( y % 4 == 0 ) && ( y % 100 != 0 ) ) )
return true;
else
return false;
} // end function isLeapYear