Thread: string to int:

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

    string to int:

    I just posted below what I have so far. I am on my last function of this program before I tackle main. I must say I am pretty proud of myself for writing 500 lines of code and not asking on here or google till now .

    What I am trying to do it compare the computers date with what birthday in the <list>, which are all strings. Here is what I was thinking, using <sstream> and converting the string to an int. However it sounds great till I notice it will be one solid int and not day,month,year, hummmm.

    I would like to iterate the list, convert them to three separate int's (day,month,year) push them into new list of int's and compare them in the if statement against the computers date.

    Code:
    void addressbook::GenBirthdayCards()
    {
        struct ConvertDate
        {
            int month,day,year;
        };
        
        for (std::list<MyaddressBook>::iterator it = Mylist.begin(); it!=Mylist.end(); ++it) {
            it->DOB;
        }
        
        time_t t = time(0);   // get time now
        struct tm * now = localtime( & t );
        
        if (now->tm_mon +1 && now->tm_mday && now->tm_yday + 1900 ==) {
            //this is where I noticed I have an issue
        }
      
    }
    Code:
    #ifndef __addressBook__
    #define __addressBook__
    
    #include <iostream>
    #include <string>
    #include <list>
    
    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();
        ~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();
        
    };
    
    
    #endif
    Last edited by jocdrew21; 01-14-2014 at 02:51 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you talking about something like "20140114"? Then you probably want
    Code:
    std::stringstream temp(DOB);
    int year, month, day;
    temp >> std::setw(4) >> year >> std::setw(2) >> month >> std::setw(2) >> day;
    I've just typed all that into this box, so it's not tested. I believe setw only lasts for one read, so you have to do it again afterwards.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Code:
    struct ConvertDate
        {
            int month,day,year;
        };
        
        std::stringstream temp_address(DOB);
        temp_address >>std::setw(2) ConvertDate.month
                     >>std::setw(2) >> ConvertDate.day
                     >> std::setw(4)>> ConvertDate.year;
        
        std::list<ConvertDate>DateConvertlist;
        
        DateConvertlist.push_back(temp_address);
    I think my issue lays with DOB. I do not think I am doing this correctly.
    Last edited by jocdrew21; 01-14-2014 at 04:19 PM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm going to assume that most of the code you posted in here is something that you actually want to use. Keep in mind how DOB is actually formatted before you throw code at it, trying to parse it.

    Code:
       ConvertDate& theDate = // wherever the day comes from
       time_t t = time(0);   // get time now
        struct tm * now = localtime( & t );
         
        if (now->tm_mon +1 == theDate.month &&
           now->tm_mday == theDate.day) {
            // generate birthday card
        }
    That's what I think you need.

    FYI: You were only born once, so the year is somewhat irrelevent
    Last edited by whiteflags; 01-14-2014 at 05:16 PM.

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Thank you white flags, yes that is what I am looking for.

    I am getting an error when I pass temp_address into the list. It has all the variables and I named them apart of the struct, whats the deal?

    Another question is, should I use *convert or leave it as convert. Would *convert create a deep copy and the other a shallow?

    Code:
    struct ConvertDate
        {
            int month,day,year;
        }convert;
        
        std::list<ConvertDate>DateConvertlist;
        
        for (std::list<MyaddressBook>::iterator it = Mylist.begin(); it!=Mylist.end(); ++it)
        {
            std::stringstream temp_address(it->DOB);
            
            temp_address
            >>std::setw(2)>> convert.month
            >>std::setw(2)>> convert.day
            >>std::setw(4)>> convert.year;
            
            DateConvertlist.push_back(temp_address);
        }

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    temp_address is not the result of your parsing, the variable convert is.

    Another question is, should I use *convert or leave it as convert. Would *convert create a deep copy and the other a shallow?
    This doesn't make any sense. First of all, convert is not declared as a pointer. Second of all, the semantics of deep vs. shallow copies doesn't matter, since ConvertDate, the type, contains no pointers. A deep copy just means that all of the pointer members are allocated, so that they point to new locations instead of sharing the locations with the older, original object (as in a shallow copy).

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Ok I have not tested it yet but it seems to be good to go. Thank you very much whiteflags, I get tunnel vision at times. As for my question about deep copy vs shallow. I know how to do it using pointers but I only know that <list> is a doubly linked list, I do not know if is uses shallow or deep copy. I really hope I am not making myself look like a idiot here.

    In my last loop, am I destroying the list properly? I would like to check the lists DOB compare it to the computer date and once done give the memory back to the free store.

    Code:
    void addressbook::GenBirthdayCards()
    {
       struct ConvertDate
        {
            int month=1,day=1,year=1900;
            std::string FirstName;
            std::string LastName;
        }convert;
        
        std::list<ConvertDate>DateConvertlist;//making new list to put DOB in as int's
       
        for (std::list<MyaddressBook>::iterator it = Mylist.begin(); it!=Mylist.end(); ++it)
        {
            std::stringstream temp_address(it->DOB); //converting DOB to ints reads something like "02021990"
            
            //reading the whole int and breaking them down to the correct object
            temp_address
            >>std::setw(2)>> convert.month //reading the first two int's and storing them in month
            >>std::setw(2)>> convert.day    //reading the second two int's and storing them in day
            >>std::setw(4)>> convert.year; //reading the last four numbers and setting it to year
            
            //taking the names of the currect node and in order to know who's birthday it is
            convert.FirstName=it->firstName;
            convert.LastName=it->lastName;
            
            //pushing the current nodes information into a new list
            DateConvertlist.push_back(convert);
        }
        
        time_t t = time(0);   // get time now
        struct tm *now = localtime( & t );
        
        //iterates through the new list "DateConvertList" and compares DOB to computers date
        for (std::list<ConvertDate>::iterator i = DateConvertlist.begin(); i != DateConvertlist.end(); ++i)
        {
            if (now->tm_mon +1 == i->month &&  //comparing the computers current date to that of the nodes
                now->tm_mday == i->day &&
                now->tm_year + 1900 == i->year)
            {   //if it matches, a card is generated and reads:
                std::cout<<"Dear "<<i->FirstName<<" "<<i->LastName<<": "<<std::endl;
                std::cout<<std::endl<<std::endl;
                std::cout<<"May your anniversary 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";
            }
            
        }
        
        //returning memory back to the free store
    for (std::list<ConvertDate>::iterator destroy = DateConvertlist.begin(); destroy != DateConvertlist.end(); destroy++)
        {
            DateConvertlist.remove(*destroy);
        }
        
    }

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I know how to do it using pointers but I only know that <list> is a doubly linked list, I do not know if is uses shallow or deep copy. I really hope I am not making myself look like a idiot here
    std::list uses the copy semantics of the template type. If your object has deep copy semantics, then it will in your list, because the only thing list ever does in that regard is call your object's copy constructor or assignment operator.

    In my last loop, am I destroying the list properly?
    std::list destroys itself -- you do nothing.
    Last edited by whiteflags; 01-15-2014 at 05:06 AM.

  9. #9
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    My compiler is giving me several errors which all say
    Code:
    Invalid operands to binary expression(std::__1::__list_iterator<MyaddressBook, void *>' and std::__1::__list_iterator<MyaddressBook,void*>')
    What does this even mean?

    Here is an example of one function I used a iterator in:

    Code:
    void addressbook::deleteContact(std::string name)
    {
        //nothing was in the list
        if (Mylist.empty())
        {
            std::cout<<"The list is empty"<<std::endl;
        }
        for (std::list<MyaddressBook>::iterator it = Mylist.begin(); it!=Mylist.end(); ++it)
        {
            if (it->firstName == name) //looking for the first name
            {
                Mylist.remove(*it);
                std::cout<<name<<" Has been deleted."<<"\n";
            }
            if (it->lastName == name) // looking for the last name
            {
                Mylist.remove(*it);
                std::cout<<name<<" Has been deleted."<<"\n";
            }
            else
            {       //name could not be found
                std::cout<<"The name "<<name<<" was not found."<<std::endl;
            }
        }
    }

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You do not need to dereference the iterator you pass to std::list::remove.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-01-2013, 10:11 PM
  2. Replies: 1
    Last Post: 04-27-2013, 04:36 AM
  3. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  4. Replies: 7
    Last Post: 06-16-2011, 06:21 PM
  5. Replies: 1
    Last Post: 10-31-2005, 11:36 AM