program hangs

This is a discussion on program hangs within the C++ Programming forums, part of the General Programming Boards category; Could you tell me why in the following code it works fine if I take the 'while' loop out but ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    96

    program hangs

    Could you tell me why in the following code it works fine if I take the 'while' loop out but as it stands it outputs all the parts of 'today' that it has parsed out and then hangs.

    Code:
    #include <ctime>
    #include <iostream>
    #include <string.h>
    #include <array>
    
    using namespace std;
    
    int main()
    {
    time_t now;
    now=time(NULL);
    char * today=ctime(&now);
    cout<<"The date is: "<<today<<endl;
    
    array<string,7> info = {{"a", "a", "a", "a", "a", "a", "a"}};
    //day month date hour minute second year
    
    char *temp;
    int x=0;
    temp = strtok (today," :");
    info[x]=temp;
    
    while (today != NULL)
            {
            cout<<temp<<endl;
            x++;
            temp = strtok (NULL, " :");
            info[x]=temp;
            }
    
    for(int x(0);x<=6;x++)
    cout<<"Info ["<<x<<"] is : "<<info[x]<<endl;
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,207
    The while loop can only end if today becomes NULL. The body of the loop does not change today. Changing temp does not change today - they are different pointers.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    96
    How do I update 'today' then; I tried putting 'today=temp' at the end of the loop but that doesn't appear to work either it still outputs the parsed strings and then hangs.
    That indicates to me that it is getting to the end of 'today' but the while loop is not recognising that.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,207
    Read up on strtok(). It returns NULL if there are no tokens left to retrieve.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    96
    I'm sorry to appear slow here but I thought I had read it correctly so I changed the loop to be 'temp != NULL' but that doesn't seem to work either.
    Could you advise how I should alter the code so that it does run correctly, I am obviously not getting the logic here.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,697
    What are you actually trying to accomplish?

    There may be easier ways to accomplish changing your time_t into an array than trying to parse the C-string.

    Why are you not converting the C-string to a std::string and then using string streams instead of strtok()?

    You could also convert your time_t variable into a struct tm and then use the data in this structure to fill in your array using string streams.

    Jim

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    96
    I am just trying to get the day, month and year from my computers time to use as variables later in the p;rogram.

    Yes I will look at using string manipulation but I would still like to understand why I can't get the loop to finish properly - I understood that strtok did change the initial string on each pass so it should eventually reach NULL; is this not right?

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    2,697
    I am just trying to get the day, month and year from my computers time to use as variables later in the p;rogram.
    Then I would suggest that you use the localtime() to convert the time_t to a struct tm then you will have all the information in separate member variables.

    but I would still like to understand why I can't get the loop to finish properly - I understood that strtok did change the initial string on each pass so it should eventually reach NULL; is this not right?
    I suggest that you run the program through your debugger, single stepping through your loop watching the variables. You are testing for temp == NULL at the beginning of your loop then you call strtok() then you write to your string. The last write is trying to place NULL into a std::string, causing a segmentation fault, and at that point x == 8 which will cause accessing the array out of bounds.

    Jim
    Last edited by jimblumberg; 10-11-2011 at 09:48 AM.

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    96
    Thanks Jim that was the clue I needed.

    I just moved the allocation to the string to before x was incremented so that the increment didn't take it out of the array bounds and it works fine just as I want it to.

    I have now found an easier way using cstdlib and iss and no doubt there are others - learning slowly but surely I hope.

    Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program hangs
    By abh!shek in forum C Programming
    Replies: 4
    Last Post: 05-25-2008, 08:02 PM
  2. Replies: 8
    Last Post: 04-12-2007, 12:42 PM
  3. program hangs after execution
    By nizbit in forum C Programming
    Replies: 6
    Last Post: 04-16-2005, 09:52 AM
  4. program hangs during runtime
    By nizbit in forum C Programming
    Replies: 8
    Last Post: 02-19-2005, 09:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21