LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-18-2006, 06:49 PM   #16
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled

OK - please look at the following:

1. Please try to compile and run it.

Please cut/paste any compile/runtime errors you might encounter.

2. What's different between this code and your code?

Why?

3. What would you have done differently?

Code:
#include <iostream.h>

class  Time
{
  private:
    int hours;
    int minutes;
    int seconds;
    void setHour(int h);
    void setMinute(int min);
    void setSecond(int sec);
    void setTime(int hour, int minute, int second);

  public:
    Time();
    Time (int hh, int mm, int ss);
    void printStandard();
    void printMilitary();
};


// "Default" (no-argument) constructor
Time::Time ()
{
  setTime(0,0,0); 
}

// Overloaded constructor
Time (int hh, int mm, int ss)
{
  setTime(hh, mm, ss); 
}

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

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

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

void 
Time::setTime(int hour, int minute, int second)
{
  setHour(hour);
  setMinute(minute);
  setSecond(second);
}

void 
Time::printStandard()
{
  //print leading zeroes; for example 03, not just 3
  cout << "The Time is " 
    << ( hours > 12 ? hours-12 : hours) << ":"
    << ( minutes < 10 ? "0" : "") << minutes << ":" 
    << ( seconds < 10 ? "0" : "") << seconds 
    << ( hours < 12 ? " am" : " pm") << endl;

}

void Time::printMilitary()
{

  //print leading zeroes; for example 03, not just 3
  cout << "The Time is "
    << ( hours < 10 ? "0" : "") << hours << ":"
    << ( minutes < 10 ? "0" : "") << minutes << ":" 
    << ( seconds < 10 ? "0" : "") << seconds << endl;
}

int
main(int argc, char *argv[])
{
  Time myTime(4, 21, 4);
  myTime.printStandard();
  myTime.printMilitary();

  Time yourTime(3,2,24);
  yourTime.printStandard();
  yourTime.printMilitary();

  return 0;
}

Last edited by paulsm4; 03-18-2006 at 08:15 PM.
 
Old 03-18-2006, 08:50 PM   #17
faris10
Member
 
Registered: Nov 2005
Location: High Point,NC
Distribution: SUSE 10.1
Posts: 89

Rep: Reputation: 15
Wink

Quote:
Originally Posted by timmay9162
Code:
#include <iostream.h>

class  Time
{
	private:
		int hours;
		int minutes;
		int seconds;
		void setHour(int h);
		void setMinute(int min);
		void setSecond(int sec);
		void setTime(int hour, int minute, int second);
	public:
		Time() {setTime(0,0,0); Time myTime(4, 21, 4), yourTime(3,2,24);}
		void printStandard();
		void printMilitary();

};


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

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

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

void Time::setTime(int hour, int minute, int second)
  {
  setHour(hour);
  setMinute(minute);
  setSecond(second);
  }


void Time::printStandard()
{

	//print leading zeroes; for example 03, not just 3

	cout << "The Time is " << ( hours > 12 ? hours-12 : hours) << ":"
		  << ( minutes < 10 ? "0" : "") << minutes << ":" << ( seconds < 10 ? "0" : "") << seconds << ( hours < 12 ? " am" : " pm") <<endl;

}
void Time::printMilitary()
{

	//print leading zeroes; for example 03, not just 3

	cout << "The Time is " << ( hours < 10 ? "0" : "") << hours << ":"
		  << ( minutes < 10 ? "0" : "") << minutes << ":" << ( seconds < 10 ? "0" : "") << seconds << endl;

}

void main()
{
myTime.printStandard();
yourTime.printMilitary();

}
That is what i have now andit still does not work.

Errors:
line 14 could not find a match for 'Time::Time(int,int,int)' in Function Time::Time()
line 14 could not find a match for 'Time::Time(int,int,int)' in Function Time::Time()
line 65 Undefined symbol 'myTime' in function main()
line 65 Undefined symbol 'yourTime' in function main()

What am i todo i am getting lost from all this help and explaining and showing of bits of code and not others. Can someone show what needs to be done to get this to work.

Thanks
Tim
This code is working . I compiled with UNIX
#include <iostream.h>

class Time
{
private:
int hours;
int minutes;
int seconds;
void setHour(int h);
void setMinute(int min);
void setSecond(int sec);
void setTime(int hour, int minute, int second);
public:
Time(int x=0,int y=0,int z=0) {setTime(x,y,z);

}
void printStandard();
void printMilitary();

};


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

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

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

void Time::setTime(int hour, int minute, int second)
{
setHour(hour);
setMinute(minute);
setSecond(second);
}


void Time:rintStandard()
{

//print leading zeroes; for example 03, not just 3

cout << "The Time is " << ( hours > 12 ? hours-12 : hours) << ":"
<< ( minutes < 10 ? "0" : "") << minutes << ":" << (
seconds < 10 ? "0" : "") << seconds << ( hours < 12
? " am" : " pm") <<endl;

}
void Time:rintMilitary()
{

//print leading zeroes; for example 03, not just 3

cout << "The Time is " << ( hours < 10 ? "0" : "") << hours << ":"
<< ( minutes < 10 ? "0" : "") << minutes << ":" << (
seconds < 10 ? "0" : "") << seconds << endl;

}

void main()
{
Time myTime(4, 21, 4);
Time yourTime(3,2,24);
myTime.printStandard();
yourTime.printMilitary();
}
 
Old 03-18-2006, 08:53 PM   #18
faris10
Member
 
Registered: Nov 2005
Location: High Point,NC
Distribution: SUSE 10.1
Posts: 89

Rep: Reputation: 15
Quote:
Originally Posted by timmay9162
Code:
#include <iostream.h>

class  Time
{
	private:
		int hours;
		int minutes;
		int seconds;
		void setHour(int h);
		void setMinute(int min);
		void setSecond(int sec);
		void setTime(int hour, int minute, int second);
	public:
		Time() {setTime(0,0,0); Time myTime(4, 21, 4), yourTime(3,2,24);}
		void printStandard();
		void printMilitary();

};


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

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

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

void Time::setTime(int hour, int minute, int second)
  {
  setHour(hour);
  setMinute(minute);
  setSecond(second);
  }


void Time::printStandard()
{

	//print leading zeroes; for example 03, not just 3

	cout << "The Time is " << ( hours > 12 ? hours-12 : hours) << ":"
		  << ( minutes < 10 ? "0" : "") << minutes << ":" << ( seconds < 10 ? "0" : "") << seconds << ( hours < 12 ? " am" : " pm") <<endl;

}
void Time::printMilitary()
{

	//print leading zeroes; for example 03, not just 3

	cout << "The Time is " << ( hours < 10 ? "0" : "") << hours << ":"
		  << ( minutes < 10 ? "0" : "") << minutes << ":" << ( seconds < 10 ? "0" : "") << seconds << endl;

}

void main()
{
myTime.printStandard();
yourTime.printMilitary();

}
That is what i have now andit still does not work.

Errors:
line 14 could not find a match for 'Time::Time(int,int,int)' in Function Time::Time()
line 14 could not find a match for 'Time::Time(int,int,int)' in Function Time::Time()
line 65 Undefined symbol 'myTime' in function main()
line 65 Undefined symbol 'yourTime' in function main()

What am i todo i am getting lost from all this help and explaining and showing of bits of code and not others. Can someone show what needs to be done to get this to work.

Thanks
Tim
I do a pologize. I dono how the smily face came in, but instead of the smily face just put the letter p.
Abid
 
Old 03-19-2006, 03:04 AM   #19
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by faris10
I do a pologize. I dono how the smily face came in, but instead of the smily face just put the letter p.
Abid
It's because you had ":p". If you want to show things like that, then select the "Disable smileys in text" option below. Edit: also, please use code tags as it helps make your code more readable.
 
Old 03-19-2006, 02:47 PM   #20
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
timay9162: are you squared away with what you were trying to get your C++ "Time" class to do?

Did this thread help:
http://www.linuxquestions.org/questi...7#post2157077?

Do you understand what your instructor was trying to get you to work out for this assignment?

Please do post back and let us know.

Thanx in advance ..
 
Old 03-19-2006, 10:41 PM   #21
timmay9162
LQ Newbie
 
Registered: Feb 2006
Posts: 20

Original Poster
Rep: Reputation: 0
Code:
#include <iostream.h>

class  Time
  {
	 private:
		int hours;
		int minutes;
		int seconds;
		void setHour(int h);
		void setMinute(int min);
		void setSecond(int sec);
		void setTime(int hour, int minute, int second);
	 public:
		Time(int h = 0, int min = 0, int sec = 0) {setTime(h, min, sec);}
		void printStandard();
		void printMilitary();

  };

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

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

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

void Time::setTime(int hour, int minute, int second)
  {
	 setHour(hour);
	 setMinute(minute);
	 setSecond(second);
  }


void Time::printStandard()
  {
	 cout << "The Time is " << ( hours > 12 ? hours-12 : hours) << ":"
	 << ( minutes < 10 ? "0" : "") << minutes << ":" << ( seconds < 10 ? "0" : "") << seconds << ( hours < 12 ? " am" : " pm") <<endl;
  }

  void Time::printMilitary()
  {
	 cout << "The Time is " << ( hours < 10 ? "0" : "") << hours << ":"
	 << ( minutes < 10 ? "0" : "") << minutes << ":" << ( seconds < 10 ? "0" : "") << seconds << endl;
  }

void main()
  {
	 Time myTime(4, 21, 4), yourTime(3,0,0);
	 myTime.printStandard();
	 yourTime.printMilitary();
  }
there it is figured it out with help from everyone here i am positive this is the way my teacher wants the program to be. Itis a little different from what everyone on here has suggested but all your help got me to this point.

Thanks
Tim
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
function defn inside a function in C...!!! arunka Programming 1 02-05-2006 01:08 AM
Calling another function from a function using GTK geminigal Programming 4 07-11-2005 03:15 PM
what are the Hexadecimal function and ASCII function in Perl Bassam Programming 1 06-03-2004 01:44 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration