Thread: inputting time in separate compilation

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    14

    inputting time in separate compilation

    Ok, for my program, we have to do separate compilation of 3 files: main.cpp, time.h, and time.cpp. We can only modify the time.cpp file. The part (well, actually the first part so far) that i'm having trouble with is when the user inputs the time.

    This is the format that the user inputs the time in: [hour][colon][minute][optional whitespace][indicator only the first letter 'a' or 'p' is important] I've tried a few ways to get this to input, but i can figure it out. I think if someone can help me out with that, I can pretty much do the rest of the program very easily. I've posted everything, but I don't think that it is neccessary..just the last file. thanks



    time.h

    Code:
    /// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ///
    /// !!!!!!!!!!!!!!!!!!!!   *DON'T* MODIFY THIS FILE   !!!!!!!!!!!!!!!!!!!! ///
    /// !!!!!!!!!!!!!!!!!!!!  DO *NOT* TURN IN THIS FILE  !!!!!!!!!!!!!!!!!!!! ///
    /// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ///
    
    #ifndef TIME_H_GUARD
    #define TIME_H_GUARD
    
    //////////////////////////////////////////////////////////////////////////////
    ///                   DEFINITION OF THE  Time  STRUCTURE                   ///
    //////////////////////////////////////////////////////////////////////////////
    
    struct Time {
      int hour;     // in 0..23  \ military clock; days begin at midnight = 00:00;
      int minute;   // in 0..59  / 6:53 AM = 06:53, noon = 12:00, 11:02 PM = 23:02
      };
    
    
    //////////////////////////////////////////////////////////////////////////////
    ///   PROTOTYPES OF FUNCTIONS FOR USE BY CLIENTS OF THE  Time  STRUCTURE   ///
    //////////////////////////////////////////////////////////////////////////////
    
    Time timeMake(int h, int m, bool isPM);
    // pre:   h  in 1..12,  m  in 0..59,  isPM  true  iff PM or noon
    // post:  returns a valid  Time  structure set to the specified time
    
    Time timeDifference(Time t1, Time t2);
    // pre:   t1  is a valid  Time,  t2  is a valid  Time
    // post:  returns a valid  Time  set to as many minutes after midnight as
    //        there are minutes between  t1  and  t2  when they are considered
    //        as belonging to the same day; the difference in minutes is taken
    //        absolutely and is always considered  >= 0  regardless of whether
    //        t1  is earlier in the day than  t2  or not
    
    void timePrint(Time t);
    // pre:   t  is a valid  Time
    // post:  the given time is printed to  cout  in the style of "7:03 AM"
    //        (without quotes); the final indicator is taken from "midnight",
    //        "AM", "noon", and "PM" (without quotes) as appropriate
    
    bool timeRead(Time *t);
    // pre:   t  is a valid pointer to a not-necessarily-valid  Time
    // post:  an attempt is made to read a time from  cin;  if successful,
    //        the  Time  pointed to by  t  reflects what was read and  true
    //        is returned; on failure,  false  is returned and  t  has been
    //        set to midnight; the valid input format for a time is exactly
    //
    //          [HOUR][COLON][MINUTE][OPTIONAL-WHITESPACE][INDICATOR]
    //
    //        where  [HOUR]  is either exactly one or exactly two decimal digits
    //        giving an integer in 1..12,  [COLON]  is an explicit ':' (without
    //        the single quotes),  [MINUTE]  is exactly two decimal digits giving
    //        an integer in 0..59,  [OPTIONAL-WHITESPACE]  is any sequence of zero
    //        or more whitespace characters, and  [INDICATOR]  is any non-empty
    //        string without whitespace of which only the first character matters:
    //        'A' or 'a' indicates AM (or midnight for 12:00), 'P' or 'p' indicates
    //        PM (or noon in the case of 12:00), 'N' or 'n' means noon (but this
    //        is only allowed for 12:00), and 'M' or 'm' means midnight (only
    //        allowed for 12:00); whitespace separates a time from any item
    //        immediately before and/or after it; remark: times output by
    //        timePrint()  are suitable for reading back in with  timeRead()
    //
    //        Valid examples:   3:00a             1:03pm      09:59ante
    //                          09:05P            12:00AM     12:08AAy,ix$bq\kh
    //                          5:09 p            4:07 Am     07:10 post_meridiem
    //                          02:08 A           12:43 pM    12:00  nOOnT1M3
    //                          12:00 midnight    12:00N      12:00 moontime
    
    #endif
    main.cpp

    Code:
    
    #include "Time.h"
    // for structure  Time
    // and function   timeMake()
    // and function   timeRead()
    // and function   timeDifference()
    // and function   timePrint()
    
    #include <cstdlib>
    // for macro  EXIT_SUCCESS
    // and macro  EXIT_FAILURE
    
    #include <iostream>
    using std::cout;
    using std::cerr;
    
    
    int main(void)
     {
        Time t1, t2;
    
        cout << "1st time? ";
    
        if(!timeRead(&t1)) {
            cerr << "Failed to read first time!\n";
            return(EXIT_FAILURE);
            }
    
        cout << "2nd time? ";
    
        if(!timeRead(&t2)) {
            cerr << "Failed to read second time!\n";
            return(EXIT_FAILURE);
            }
    
        Time delta = timeDifference(t1, t2);
    
        cout << "Noon: \"";             timePrint(timeMake(12,  0, true));
        cout << "\", midnight: \"";     timePrint(timeMake(12,  0, false));
        cout << "\", L2 ends: \"";      timePrint(timeMake(10, 50, false));
        cout << "\",\nL3 ends: \"";     timePrint(timeMake( 1, 50, true));
        cout << "\"; difference of  ";  timePrint(t1);
        cout << "  and  ";              timePrint(t2);
        cout << "  is  ";               timePrint(delta);
        cout << ".\n";
    
        return(EXIT_SUCCESS);
     }
    time.cpp --> the modifiable file--it should go in the timeRead func

    Code:
    #include "Time.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    
    
    //////////////////////////////////////////////////////////////////////////////
    ///        PROTOTYPES OF ANY AND ALL FUNCTIONS PRIVATE TO THIS FILE        ///
    //////////////////////////////////////////////////////////////////////////////
    
    // --delete this comment and replace it with the prototypes of any and all--
    // -- helper functions you write; such functions are private to this file --
    
    
    //////////////////////////////////////////////////////////////////////////////
    ///             DEFINITIONS OF FUNCTIONS PROTOTYPED IN  Time.h             ///
    ///            AND ANY & ALL PRIVATE FUNCTIONS PROTOTYPED ABOVE            ///
    //////////////////////////////////////////////////////////////////////////////
    
    string time1;
    
    
    Time timeMake(int h, int m, bool isPM)
     {
        // --delete this comment and the stub code below, then add your own code--
    
        Time t;
        t.hour   = 0;
        t.minute = 0;
    
        return(t);
     }
    
    
    Time timeDifference(Time t1, Time t2)
     {
        // --delete this comment and the stub code below, then add your own code--
    
        Time t;
        t.hour   = 0;
        t.minute = 0;
    
        return(t);
     }
    
    
    void timePrint(Time t)
     {
        // --delete this comment and the stub code below, then add your own code--
    
        cout << "FIX-ME";
     }
    
    
    bool timeRead(Time *t)
     {
        
    	
    
    
        return(true);
     }

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    read your text books/ helpfiles for istringstreams. learn how they can help you.

    use getline() to get input as a string. construct an istringstream from the input. Then parse out what you need. If i give example ill write function for you so sorry to your texts you go!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    14
    ack, finally. this is what i got for my function

    Code:
    bool timeRead(Time *t)
     {
    	 cin>>time1;
    	 if(time1[4] != 'A'||'a'||'p'||'P'||'n'||'N'||'m'||'M'){
    		 cin>>time2;
    		time1+=time2;
    	 }
    	
    	
        return(true);
     }
    now onto the timeMake function.....

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    if(time1[4] != 'A'||'a'||'p'||'P'||'n'||'N'||'m'||'M'){
    		 cin>>time2;
    		time1+=time2;
    	 }
    That line of code is not going to work. Itt'l always evaluate to true since you're not employing any boolean logic with the characters (besides 'A'). An easier (and correct) way to do this would be:
    Code:
    const char validTime[] = "APNM";
    for (int i = 0; i < 4; ++i)
    {
       if (validTime[i] == toupper(time1[4]))
       {
           cin >> time2;
           time1 += time2;
           break;
       }
    }

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    14
    i tried using that code and it wouldn't work...i guess i can't implement it correctly/i don't understand it.

    anyways, i found out that my code didn't work after all

    but, i made some new code

    Code:
    bool timeRead(Time *t)
     {
    	 cin>>time1;
    	 cout<<time1[2]<<endl<<time1[5];
    
    
    	 if(time1[2]==':' && time1[5] == ('A'||'a'||'p'||'P'||'n'||'N'||'m'||'M')){
    		 cout<<"hello"<<endl;
    		 return (true);}
    	 if(time1[1]==':' && time1[4] == ('A'||'a'||'p'||'P'||'n'||'N'||'m'||'M')){
    		 cout<<"hello"<<endl;
    		 return (true);}
    		 
    	 
         cin>>time2;
    	 time1+=time2;
    	 return(true);
    	 
    	 
    	 
    
     }

    but here is my problem with it. the if statements wont evaluate--> it still does the extra cin>> if the time is input as 12:35a or 1:34p. It works if the time is inputted as 12:35 a or 1:34 p.....

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    14
    fixed that :-D

    Code:
    bool timeRead(Time *t)
     {
    	 cin>>time1;
    	 cout<<time1[2]<<endl<<time1[5];
    
    
    	  if(time1[2]==':' && ((time1[5] == 'A' || time1[5] == 'a') || (time1[5] == 'P' || time1[5] == 'P')
    		  || (time1[5] == 'n' || time1[5] == 'N') || (time1[5] == 'M' || time1[5] == 'm'))){
    		 	 return (true);}
    	 if(time1[1]==':' && ((time1[4] == 'A' || time1[4] == 'a') || (time1[4] == 'P' || time1[4] == 'P')
    		  || (time1[4] == 'n' || time1[4] == 'N') || (time1[4] == 'M' || time1[4] == 'm'))){
    		
    		 return (true);}
    		 
    	 
         cin>>time2;
    	 time1+=time2;
    	 return(true);
    	 
    	  
    
     }

    what i got so far on the next function....what do you guys recommend for change part of the string (the first 1 or 2 characters in the string to an int?) i think the istreamsting is only for a whole string.

    Code:
    Time timeMake(int h, int m, bool isPM)
     {
        
    	int hourCounter=0;
        Time t;
        
    	if(time1[1]==':'){
    		if(time1[4]==('n'||'N')){
    			t.hour   = 12;
    			t.minute = 0;
    			isPM=true;
    			return(t);}
    		if(time1[4]==('m'||'M')){
    			t.hour   = 0;
    			t.minute = 0;
    			isPM=false;
    			return(t);}
    		if(time1[4]==('p'||'P')){
    			hourCounter++; 
    			isPM=true;}
    	}
    	if(time1[2]==':'){
    		if(time1[5]==('n'||'N')){
    			t.hour   = 12;
    			t.minute = 0;
    			isPM=true;
    			return(t);}
    		if(time1[5]==('m'||'M')){
    			t.hour   = 0;
    			t.minute = 0;
    			isPM=false;
    			return(t);}
    		if(time1[5]==('p'||'P')){
    			hourCounter++; 
    			isPM=true;}
    	}
    		
    	if(hourCounter>0 && time1[1]==':'){
    		t.hour   = 12+time1[0];
    		t.minute = time1[2]+time1[3];
    	}
    
    	if(hourCounter>0 && time1[2]==':'){
    		t.hour   = 1+time1[0];
    		t.minute = time1[2]+time1[3];
    	}
    
        return(t);
     }

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    14
    Ok, it's almost there. On main.cpp, when the last 3 functions are called, i get (it seems like) memory addresses --> when timePrint(t1), timePrint(t2), and timePrint(delta) are called.

    Also, sometimes it will use only one cin>> when a time is entered without the space between the actually time and the am/pm. It seems like it does it if i restart my computer. Otherwise it won't do it. I have no problems with it accepting times with the space.


    Any ideas on either?


    (my new and imporved code)

    Code:
    #include "Time.h"
    #include <iostream>
    #include <sstream>
    #include <string>    //includes these files
    using namespace std;
    
    
    
    
    //////////////////////////////////////////////////////////////////////////////
    ///        PROTOTYPES OF ANY AND ALL FUNCTIONS PRIVATE TO THIS FILE        ///
    //////////////////////////////////////////////////////////////////////////////
    class time
    {
    private:
    void swapValues(Time &v1, Time &v2);
    
    
    };      
    
    
    
    //////////////////////////////////////////////////////////////////////////////
    ///             DEFINITIONS OF FUNCTIONS PROTOTYPED IN  Time.h             ///
    ///            AND ANY & ALL PRIVATE FUNCTIONS PROTOTYPED ABOVE            ///
    //////////////////////////////////////////////////////////////////////////////
    string time1, time2;
                        //global variables
    
    
    Time timeMake(int h, int m, bool isPM)
    {
        Time t;
        
    	
    	t.hour=h;
    	t.minute=m;
    	if(isPM && t.hour!=12)
    		t.hour+=12;			//if time is noon, 12 hours isn't added for military time
    
        return(t);
     }
    
    void swapValues(Time &v1, Time &v2)
    //pre:receives two vars of type time
    //post:swaps the two and returns them
    {
    	Time temp;
    	temp=v1;
    	v1=v2;
    	v2=temp;
    }
    
    
    Time timeDifference(Time t1, Time t2)
     {
        
    
        Time t;
        
    	if(t2.hour>t1.hour)
    		swapValues(t1,t2);	//swaps the 2 vars if second time inputted is larger than first
    	
    	
    	if(t1.hour==t2.hour){		//subtraction case if hours are same
    		
    		if(t1.minute>t2.minute){//minutes greater on first input
    			t.hour=0;
    			t.minute=(t1.minute-t2.minute);
    		} 
    		
    		if(t1.minute==t2.minute){//minutes equal
    			t.hour=0;
    			t.minute=0;
    		}
    		
    		if(t1.minute<t2.minute){//minutes greater on seconf input
    			t.hour=0;
    			t.minute=(abs(t1.minute-t2.minute));
    		}
    
    										
    		return(t);
    	}
    	
    	if(t1.hour>t2.hour){ //subrataction case when time 1 is larger
    		t.hour=(t1.hour-t2.hour);
    		if(t1.minute==t2.minute){ //both minutes are equal
    			t.minute=0;
    			return(t);
    		}
    		if(t1.minute>t2.minute){ //first minute is larger
    			t.minute=(t1.minute-t2.minute);
    			return(t);
    		}
    		if(t1.minute<t2.minute){ //second minute is larger
    			t.hour--;
    			t.minute=((t1.minute+60)-t2.minute);
    			return(t);
    		}
    	}
    
    	return(t1);
    
     }
    
    
    void timePrint(Time t)
     {
       
    	if(t.minute<=9)
    		cout << t.hour<<":0"<<t.minute<<endl;  //if minute is less than 10, a '0' is outputted 
    	else									   //before so time doesn't look like this "12:9"
    	cout << t.hour<<":"<<t.minute<<endl;
     }
    
    
     bool timeRead(Time *t)
     {
    	 int hour, minute, spot1=-48, spot2=-48, spot3=-48, spot4=-48;
    	 //spot variables initialized to -48 for char-->int conversion
    	 bool nite, input=true;
    
    	 cin>>time1;
    	 
    	 if(time1[2]==':'){        //determines if cin is needed because of the space between the time and the								
    		 if (time1[5] == 'A' || time1[5] == 'a'){   //am/pm distinction
    			input=false;}
    		  if (time1[5] == 'P' || time1[5] == 'P')
    			input=false;
    		  if (time1[5] == 'n' || time1[5] == 'N')
    			input=false;
    		  if (time1[5] == 'M' || time1[5] == 'm')
    			input=false;
    	 }
    
    	 if(time1[1]==':'){
    		  if (time1[4] == 'A' || time1[4] == 'a')
    			input=false;
    		  if (time1[4] == 'P' || time1[4] == 'P')
    			input=false;
    		  if (time1[4] == 'n' || time1[4] == 'N')
    			input=false;
    		  if (time1[4] == 'M' || time1[4] == 'm')
    			input=false;
    	 }	
    	 if(input){  
         cin>>time2;
    	 time1+=time2;}
    
    	 	if(time1[1]==':'){			//sets inputted string to integers
    		if(time1[4]==('n'||'N')){
    			hour   = 12;
    			minute = 0;
    			nite=true;
    			timeMake(hour, minute, nite);}
    		if(time1[4]==('m'||'M')){
    			hour   = 0;
    			minute = 0;
    			nite=false;
    			timeMake(hour, minute, nite);}
    		if(time1[4]==('p'||'P')){
    			nite=true;}
    	}
    	if(time1[2]==':'){
    		if(time1[5]==('n'||'N')){
    			hour   = 12;
    			minute = 0;
    			nite=true;
    			timeMake(hour, minute, nite);}
    		if(time1[5]==('m'||'M')){
    			hour   = 0;
    			minute = 0;
    			nite=false;
    			timeMake(hour, minute, nite);}
    		if(time1[5]==('p'||'P')){
    			nite=true;}
    	}
    		
    	if(time1[1]==':'){
    		spot1+=time1[0];
    		if(nite){
    			spot1+=12;}
    		hour=spot1;
    		spot2+=time1[2];
    		spot3+=time1[3];
    		minute=(spot2*10)+spot3;
    	}
    
    	if(time1[2]==':'){
    		spot1+=time1[0];
    		spot2+=time1[1];
    		
    		if(nite){
    			spot1+=1;
    			spot2+=2;}
    		hour=(spot1*10)+spot2;
    		spot3+=time1[3];
    		spot4+=time1[4];
    		minute=(spot3*10)+spot4;
    	}
    
    	timeMake(hour, minute, nite);
    	 
    	return(true);
    	 
    	  
    
     }
    thanks for your help!
    Last edited by sameintheend01; 03-13-2003 at 04:54 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. relation between bugs and compilation time
    By KIBO in forum Tech Board
    Replies: 10
    Last Post: 01-29-2009, 01:29 PM
  3. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  4. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM