Archived

This topic is now archived and is closed to further replies.

weird values

This topic is 5003 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I keep getting -858993460 for minute and second in output I''ve looked over the code but can''t see the problem....
// header1.h

// Declaration of the Time class

// Mmeber functions defined in sourc1.cpp


// preprocesser directives that

// prevent multiple inclusions of header file

#ifndef HEADER1_H
#define HEADER1_H

class Time {
public:
	Time( int = 0, int = 0, int = 0 );

	// set functions

	void setTime( int, int, int );
	void setHour( int );
	void setMinute( int );
	void setSecond( int );

	// get functions

	int getHour();
	int getMinute();
	int getSecond();

	void printMilitary();
	void printStandard();

private:
	int hour;
	int minute;
	int second;
};

#endif
// Time class function implementations

#include <iostream>

using std::cout;

#include "header1.h"

// constructor function definition

Time::Time( int hr, int min, int sec )
{
	setTime( hr, min, sec ); 
}

// set values for hour, minute, second

void Time::setTime( int h, int m, int s )
{
	setHour( h );
	setMinute( m );
	setSecond( s );
}

// set hour value

void Time::setHour( int h )
{
	hour = ( h >= 0 && h < 24 ) ? h : 0; 
}

// set minute value

void Time::setMinute( int m )
{
	hour = ( m >=0 && m < 60 ) ? m : 0;
}

// set second value

void Time::setSecond( int s )
{
	hour = ( s >=0 && s < 60 ) ? s : 0;
}

// get hour value

int Time::getHour() { return hour; }

// get minute value()

int Time::getMinute() { return minute; }

// get second value()

int Time::getSecond() { return second; }

// print Military time

void Time::printMilitary()
{
	cout << ( hour < 10 ? "0" : "" ) << hour << ":"
		 << ( minute < 10 ? "0" : "" ) << minute;
}

void Time::printStandard()
{
	cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
		 << ":" << ( minute < 10 ? "0" : "" ) << minute
		 << ":" << ( second < 10 ? "0" : "" ) << second
		 << ( hour < 12 ? " AM" : " PM" );
}
// main()

#include <iostream>

using std::cout;
using std::endl;

#include "header1.h"

void incrementMinutes( Time &, const int );

int main()
{
	Time t;

	t.setHour( 17 );
	t.setMinute( 34 );
	t.setSecond( 25 );

	cout << "Result of setting all valid values:\n"
		 << "	Hour: " << t.getHour()
		 << "	Minute: " << t.getMinute()
		 << "	Second: " << t.getSecond();

	t.setHour( 234 );
	t.setMinute( 43 );
	t.setSecond( 6373 );

	cout << "\n\nResult of attempting to set invalid hour and"
		 << " second:\n  Hour: " << t.getHour()
		 << "  Minute: " << t.getMinute()
		 << "  Second: " << t.getSecond() << "\n\n";

	t.setTime( 11, 58, 0 );
	incrementMinutes( t, 3 );

	return 0;
}

void incrementMinutes( Time &tt, const int count )
{
	cout << "Incrementing minute " << count
		 << " times:\nStart time: ";
	tt.printStandard();

	for ( int i = 0; i < count; i++ ) {
		tt.setMinute( ( tt.getMinute() + 1 ) % 60 );

		if ( tt.getMinute == 0 )
			tt.setHour( ( tt.getHour() + 1 ) % 24 );

		cout << "\nminute + 1; ";
		tt.printStandard();
	}

	cout << endl;
}

Share this post


Link to post
Share on other sites