Thread: String Classes- Need Help!

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    1

    Question String Classes- Need Help!

    Hello!
    I need help with my program- I am very confused and can't figure out what's wrong. I have about 13 errors. Any hints on what to do next?

    Thank you!
    Leanna

    include <cstdlib>
    #include <iostream>
    using namespace std;

    class Time
    {
    private:
    long hr, min;
    char meridian[5];

    public:
    void Set(long h, long m, char mer[]);
    void Set(char string_time[]);
    Time(long h = 12, long m = 0, char mer[] = "AM");
    Time(char string_time[]);
    void Add( long hour, long minute);
    void Tick();
    bool Equals(Time t);
    void Show();
    };
    Time::Time(long h, long m, char mer[])
    {
    };
    Time::Time(char string_time[])
    {
    };

    void Time::Set(long h, long m, char mer[])
    {
    if ((h <= 0) || (h > 12) || (m < 0) || (m > 59))
    cout << "Error: Number invalid. \n";
    else
    hr = h;
    min = m;
    strncpy( meridian, mer, 3);

    };

    void Time::Set(char string_time[])
    {

    if ((strlen(string_time) != 8)|| (string_time[2] != ':') || (string_time[7] != 'M'))
    {
    cout << "Whoa! Somethin's wrong with that number! \n";
    return ;
    }

    else
    char string_hr[3];
    char string_min[3];
    char mer[5];

    strncpy( string_hr, string_time, 2);
    string_hr[2] = '\0';

    strncpy( string_min, (string_time + 3) , 2);
    string_min[3] = '\0';

    strncpy( mer, (string_time + 6), 2);
    mer[3] = '\0';


    long h;
    sscanf( string_hr[2], "%ld", &h);

    long m;
    sscanf( string_min[2], "%ld", &m);

    if ((h > 12) || (h < 0) || (m > 60) || (m < 0))
    {
    cout << "That number's out of bounds! \n";
    }

    Set(h, m, mer);

    };

    void Time::Add (long hour, long minute)
    {
    int k;
    Time.h = Time.h + hour;
    Time.m = Time.m + minute;

    if (Time.m >= 60)
    Time.m = Time.m - 60;
    Time.h++;

    else if (Time.h > 12)
    Time.h = Time.h - 12;

    for
    (k = 0; k <= 12; k++)
    Time.mer = "AM";

    if
    (k = 12; k > 12 < 24; k++)
    Time.mer = "PM";

    if (k = 24)
    k = 0;
    continue;
    };

    void Time::Tick()
    {
    Time.m = Time.m++;
    };

    bool Equals(Time t)
    {
    Time.Time == t;
    };

    void Time::Show()
    {
    cout << Time.Time(long h, long m, char mer[]) << endl;
    };

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You declare hr and min in your class, but then call them h and m in some of your member functions. Also to refer to a class variable within a member function, just say hr and min, not Time.hr and Time.min. For example, Time::Add() would look something like:
    Code:
    void Time::Add (long hour, long minute)
    {
    int k;
    hr = hr + hour;
    min = min + minute;
    
    if (min >= 60)
    {
    min = min - 60;
    hr++;
    }
    
    else if (hr > 12)
    hr = hr - 12;
    
    for
    (k = 0; k <= 12; k++)
    Time.mer = "AM";
    
    if
    (k = 12; k > 12 < 24; k++)
    Time.mer = "PM";
    
    if (k == 24)
    k = 0;
    continue;
    }
    I have no idea what that meridian stuff is at the bottom, so I left it alone. Also, whenever you have more than one statement within an if() or for(), you must enclose the statements within braces {}. I noticed this problem in several places. Also, be sure to use double equals (==) for conditionals, as = is assignment.

    > if (k = 24)
    if (k == 24)

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    First things first. Use code tags (look in the FAQ) and tell us what your errors are.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    Here' one I was just working on if you want to look at it to see.
    Shows you how you can do the AM PM thing. and to check to see wether setTime is within the ranges.
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    //========================================================================================
                                       //class definition
    class Time {
    
    public:
    	Time();
    	void setTime( int, int, int ); 
    	void printUniversal();
    	void printStandard();
    
    private:							
    	int hour;					
    	int minute;
    	int second;
    
    };
    
    Time::Time()						
    {
    	hour = minute = second = 0;
    }
    
    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;
    }
    
    void Time::printUniversal()
    {
    	cout << setfill ( '0' ) << setw( 2 ) << hour << ":"
    		<< setw( 2 ) << minute << ":"
    		<< setw( 2 ) << second;
    }
    
    void Time::printStandard()
    {
    	cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) 
    		<< ":" << setfill( '0' ) << setw( 2 ) << minute
    		<< ":" <<setw( 2 ) << second
    		<< ( hour < 12 ? " AM" : " PM" ) ;
    }
    
    //=========================================================================================
    
    int main()
    {
    	Time t;
    
    	cout << "The initial universal time is ";
    	t.printUniversal();
    
    	cout << "\nThe initial standard time is ";
    	t.printStandard();
    
    	t.setTime( 13, 27, 6 );
    
    	cout << "\n\nUniversal time after setTime is ";
    	t.printUniversal();
    
    	cout << "\nStandard time after setTime is ";
    	t.printStandard();
    
    	t.setTime( 99, 99, 99 );
    
    	cout << "\n\nAfter attempting invalid settings:";
    		t.printUniversal();
    	
    	cout << "\nStandard time: ";
    	t.printStandard();
    
    	cout << endl;
    	
    	return 0;
    }
    big146

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. += operator
    By BKurosawa in forum C++ Programming
    Replies: 8
    Last Post: 08-05-2007, 03:58 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM