Thread: compare time string:

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    compare time string:

    My if statement will seem confusing but this is what I was doing. Is there a way I can break down my string DOB into three strings i.e. string DAY, string MONTH string YEAR. That seems to be the only way to handle this. Unless I iterate through my whole and break down the DOB variable into int's using string stream. I think it would be easier on the computer to just convert one string (time()) instead.

    Or maybe converting the string stream below on line (9,10,11) to a solid string and comparing that way. However that method is giving me issues too.

    Code:
    void addressbook::GenBirthdayCards()
    {
    
        bool found = false;
        
        time_t t = time(0);   // get time now
        struct tm *now = localtime(&t);  //date will read 2014120 meaning 2014-1-20 but will have no punctuations. 
        
        std::stringstream year(now->tm_year+1900);
        std::stringstream month(now->tm_mon+1);
        std::stringstream day(now->tm_mday);
        
        for (auto it = Mylist.begin(); it!= Mylist.end();++it)
         {
         if (month == it->MONTH &&  //comparing the computers current date to that of the nodes
          day == it->DAY &&
          year == it->YEAR)  //YEAR DAY and MONTH are just examples
         {   //if it matches, a card is generated and reads:
         std::cout << "Dear " << it->FirstName << " " << it->LastName << ": " << std::endl;
         std::cout << std::endl << std::endl;
         std::cout << "May your Birthday be the best yet and we wish you more to come\n";
         std::cout << std::endl << std::endl;
         std::cout << "Respectfully,\n";
         std::cout << "Drew\n";
             found=true;
         }
         
         }
        if (!found) {
            std::cout<<"No Birthdays are today."<<std::endl;
        }
        
    }
    Code:
    struct MyaddressBook {
        std::string firstName;
        std::string lastName;
        std::string address;
        std::string aniversary;
        std::string DOB;
    };
    
    class addressbook {
        
    private:
        std::list<MyaddressBook> Mylist;
        
    public:
        addressbook();
       
        void addContact(MyaddressBook&);
        void readfile(std::ifstream&,MyaddressBook&);
        void updateFile(MyaddressBook&);
        void EditNameOrDate();
        void PrintAddressBook();
        void GenBirthdayCards();
        void GenAnnCard();
        void deleteContact(std::string);
        void sortContacts();
        void ExitProgram();
        
    };
    Last edited by jocdrew21; 01-20-2014 at 05:34 AM.

  2. #2
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Code:
     std::stringstream year(now->tm_year+1900);
        std::stringstream month(now->tm_mon+1);
        std::stringstream day(now->tm_mday);
        std::stringstream ss;
        ss << day << month << year;
        std::string result = ss.str();
        std::cout << result <<"/n";  //just to check my output before I start plugging thing in....
    I am getting an error in this one too. Now what if I just make the whole thing a solid string and compare it to DOB, that would be great. What am I doing wrong?
    Last edited by jocdrew21; 01-20-2014 at 05:37 AM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jocdrew21
    Is there a way I can break down my string DOB into three strings i.e. string DAY, string MONTH string YEAR.
    Yes: take substrings.

    Quote Originally Posted by jocdrew21
    Or maybe converting the string stream below on line (9,10,11) to a solid string and comparing that way. However that method is giving me issues too.
    Your issues are because you are not using the stringstream correctly. Given an integer x, if you want a string, you would write something like:
    Code:
    stringstream ss;
    ss << x;
    string result = s.str();
    Actually, with C++11, there is the std::to_string function that can be used instead:
    Code:
    string result = to_string(x);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Lazerlight thank you so much....
    Code:
    void addressbook::GenBirthdayCards()
    {
    	
        bool found = false;
        
    	time_t t = time(0);   // get time now
    	struct tm *now = localtime(&t);  //date will read 2014120 meaning 2014-01-20 but will have no punctuations
        
        std::stringstream ss;
        
        ss<<now->tm_year+1900<<"."<<now->tm_mon+1<<"."<<now->tm_mday;
        
        std::string result = ss.str();
        
    	for (auto it = Mylist.begin(); it!= Mylist.end();++it)
         {
         if (it->DOB == result)
         {   //if it matches, a card is generated and reads:
         std::cout << "Dear " << it->firstName << " " << it->lastName << ": " << std::endl;
         std::cout << std::endl << std::endl;
         std::cout << "May your Birthday be the best yet and we wish you more to come\n";
         std::cout << std::endl << std::endl;
         std::cout << "Respectfully,\n";
         std::cout << "Drew\n";
             found=true;
         }
         
         }
        if (!found) {
            std::cout<<"No Birthdays are today."<<std::endl;
        }
    
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Good to hear that you got it to work.

    By the way, I'm not clear as to what this addressbook class is for, but it looks like you are conflating the implementation of an addressbook related algorithm with code that provides an interface to the user of an addressbook program. Generally, it is good practice to decouple this. So, you might have an addressbook class that does not do any input/output by itself (at least not to std::cin and std::cout), then you will use this class to implement addressbook program that has interactive I/O with the user.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Yes that is what I am doing. I have a <list> of contact information and I am comparing it to the date to the computers date.

    So what I am understanding is for me to make a base class "address book" and with the combination of inheritance and polymorphism (using virtual of course) make a new class "the derived class" do the actual in and output....

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jocdrew21
    So what I am understanding is for me to make a base class "address book" and with the combination of inheritance and polymorphism (using virtual of course) make a new class "the derived class" do the actual in and output....
    Probably not. Think along the lines of manipulating an addressbook object.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Probably not. Think along the lines of manipulating an addressbook object.
    Hummmmm....I don't think I am quite getting it....What advantages would it give me? Once I understand what you mean of course.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jocdrew21
    What advantages would it give me?
    One advantage of separating the logic of the addressbook from the program that interacts with the user is that you break down the program into parts that can be implemented step by step, or perhaps implemented separately by a team member (should you later work in a team). Another advantage is that you could say, write a GUI version of the program, using the same addressbook class. Yet another advantage is that it tends to be easier to test: you can write automated tests for the logic of the addressbook, whereas writing automated tests for the interface may be more challenging, sometimes impossible.

    As for why you probably shouldn't be using inheritance and polymorphism: there isn't really anything polymorphic about the addressbook, at least not in this sense. If you use inheritance, you will be purely inheriting the implementation. This is not necessarily wrong, but usually using composition would then be better: you can have an entirely different class that is about controlling the view to the user, rather than modeling an addressbook.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string compare
    By ingeniousreader in forum C Programming
    Replies: 7
    Last Post: 03-02-2012, 12:43 PM
  2. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  3. Compare 2 String
    By nitediver in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 07:48 AM
  4. compare string with time
    By munna_dude in forum C Programming
    Replies: 4
    Last Post: 04-09-2007, 01:40 AM
  5. string compare
    By IceBall in forum C Programming
    Replies: 4
    Last Post: 10-12-2003, 05:26 PM