Thread: Noob tryin to make a library

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    4

    Noob tryin to make a library

    I am working on a project for my class, and i am trying to put my functions into a library. It isnt working for some reason. here is what i have:
    main file for my program:
    Code:
    /*	Name: Shawn Pappas
    	CSC121-002
       	Program Title: My Liver for a Clock!
    	Levels Attempted: 1) Add (Level 1) to display the time in a pretty format with leading 0's using a simple branch
    					  2)Add (Level 2) to display the time in American format (AM-PM, that is) as well as 24-hour format.
    					  3)Add (Level 1) to adjust the time from Greenwich Mean Time to US Central Time. 
    	Program Description: This program takes the number of seconds since Jan. 1st 1970 (GMT) and converts it into the 
    						 current time GMT. It also displays the current time in US Central Time. US Central Time is currently 
    						 computed as -5 GMT because of Daylight Savings Time. Otherwise the conversion would be -6. 
    						 I found the current hours by dividing the amount of seconds since Jan. 1st 1970 by how many seconds 
    						 were in an hour,then used % to figure out how many hours were remaining that were not complete days.
    						 I did the same for minutes and seconds. 
    */
    #include <iostream>
    #include <ctime>
    #include "timegmt.h"
    using namespace std;
    int main()
    {
    	short hour_gmt, min, sec;
    	const short sec_hour = 60*60, sec_min = 60, hour_day = 24;
    	string colon = ":";
    	time_t seconds;
    	seconds = time (NULL);	
    	hour_gmt = (seconds/sec_hour)%hour_day; // Calculates the currnet hours GMT
    	min = (seconds/sec_min)%sec_min; // Calculates the current minutes GMT
    	sec =  seconds%sec_min; //Calculates the current seconds GMT
    	cout << endl << "The current time in GMT is " << hour_gmt << colon;
    	lead_0(min); 
    	cout << min << colon;
    	lead_0(sec);
    	cout << sec << ".";
    // American Standard Time format AM/PM
    	cout << " (";
    	hour_ast(hour_gmt);
    	lead_0(min);
    	cout << min << colon;
    	lead_0(sec);
    	cout << sec;
    	am_pm(hour_gmt);
    // Convert to Central Time
    	cout << "The current time CST (Central Standard Time) is ";
    	cout << conv_cst(hour_gmt) << colon;
    	lead_0(min);
    	cout << min << colon;
    	lead_0(sec);
    	cout << sec << colon;
    // CST American Standard Time format AM/PM
    	cout << " (";
    	hour_ast(conv_cst(hour_gmt));
    	lead_0(min);
    	cout << min << colon;
    	lead_0(sec);
    	cout << sec;
    	am_pm(conv_cst(hour_gmt));
    	return 0;
    }
    timegmt.h:
    Code:
    #ifndef TIMEGMT_H_INC
    #define TIMEGMT_H_INC
    
    void lead_0(short time);
    void am_pm(short time);
    void hour_ast(short time);
    short conv_cst(short time);
    
    #endif
    and timegmt.c:
    Code:
    #include "timegmt.h"
    #include <iostram>
    using namespace std;
    
    
    void lead_0(short time)
    {
    	if (time < 10)
    	{
    		cout << "0";
    	}
    	return;
    }
    void am_pm(short time)
    {	
    	if (time > 12)
    	{
    		cout << " PM.)\n";
    	}
    	else 
    	{
    		cout << " AM.)\n";
    	}
    	return;
    }
    void hour_ast(short time)
    {
    	if (time == 0) 
    	{	 cout << "12:";
    	}
    	if (time > 12)
    	{
    		cout << time-12 << ":";
    	}
    	if ((time != 0) && (!(time > 12)))
    	{
    		cout << time << ":";
    	}
    	return;
    }
    short conv_cst (short time)
    {
    	short cst_conv = -5, hour_cst;
    	if (time + cst_conv < 1)
    	{
    		hour_cst = time+24+cst_conv;
    	}
    	else
    	{	
    		hour_cst = time + cst_conv;
    	}
    	return(hour_cst);
    }
    Could someone please tell me what I am doing wrong?

    Also, my instructor looked over my origional program before I tried to put the functions into a library, and wanted me to change my functions from:
    Code:
     
    void hour_ast(short time)
    {
    	if (time == 0) 
    	{	 cout << "12:";
    	}
    	if (time > 12)
    	{
    		cout << time-12 << ":";
    	}
    	if ((time != 0) && (!(time > 12)))
    	{
    		cout << time << ":";
    	}
    	return;
    to:
    Code:
    void hour_ast(short time)
    {
    	if (time == 0) 
    	{	 cout << "12:";
    	}
    	else (time > 12)
    	{
    		cout << time-12 << ":";
    	}
    	if ((time != 0) && (!(time > 12)))
    	{
    		cout << time << ":";
    	}
    	return;
    The problem is that it errors out when i compile it this way. Was my instructer wrong, or is there something else i should do?
    Last edited by raven420smoke; 09-21-2005 at 10:39 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What errors? not knowing which language you're using?
    and timegmt.c: <---- this is C

    #include <iostram> <--- this is C++, and an inability to spell.


    If all is well, then all you should be doing is
    g++ prog.cpp timegmt.cpp


    > else (time > 12)
    Try
    else if (time > 12)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    4
    timegmt.cpp is the file name.
    #include <iostream> (it was a typo)

    The error i get when i compile it is this:
    Code:
    undefined reference to 'lead_0(short)'
    it lists the same error for every time a function is called in the main program, changing the lead_0 for the function name.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Like I said, you need to compile BOTH programs on the command line
    Or add both to your project
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's an import library?
    By chiefmonkey in forum C++ Programming
    Replies: 1
    Last Post: 06-19-2009, 05:00 PM
  2. Establishing 'make clean' with GNU make
    By Jesdisciple in forum C Programming
    Replies: 9
    Last Post: 04-11-2009, 09:10 AM
  3. How to make a Packet sniffer/filter?
    By shown in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2009, 09:51 PM
  4. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  5. 'functions' in make?
    By mart_man00 in forum C Programming
    Replies: 1
    Last Post: 06-21-2003, 02:16 PM