strtok function

This is a discussion on strtok function within the C++ Programming forums, part of the General Programming Boards category; I have an assignment that uses the strtok function to read two times, then outputs the first and last hours ...

  1. #1
    Registered User hello124's Avatar
    Join Date
    Mar 2005
    Location
    In a house
    Posts
    3

    strtok function

    I have an assignment that uses the strtok function to read two times, then outputs the first and last hours and minutes from each time (by the way this is only a small section of my assignment)

    The problem that I have is getting the first times minutes to be displayed.
    I have tried all ways I can think of to get the first times minutes, but I have spent hours trying to figure it out I just hope someone can show me how I could do this or go about doing this

    All my other times work perfectly

    I am extremely new to C++ and I am not allowed to use pointers (not that I understand how to use them anyway)

    Here is my code so far:

    Code:
    #include<iostream>
    #include<conio>
    #pragma hdrstop
    
    using namespace std;
    
    //---------------------------------------------------------------------------
    
    #pragma argsused
    int main(int argc, char* argv[])
    {
    char firsttime[8];
    char lasttime[8];
    int firsthour = 0;
    int firstmins = 0;
    int lasthour = 0;
    int lastmin = 0;
    
    
    cout << "Enter first time: ";   //firsttime input
    cin.getline( firsttime, 8 );
    
    cout << "Enter second time: ";   //lasttime input
    cin.getline( lasttime, 8 );
    
    
    firsthour = atoi( strtok( firsttime, ":" ) );
    lasthour = atoi( strtok( lasttime, ":" ) );
    
    firstmins = atoi( strtok( ?????? what do i put in here ???? ) );
    lastmins = atoi(strtok( NULL, lasttime ) );   // this part works fine
    
    
    cout << firsthour << "\n";
    cout << lasthour << "\n";
    cout << firstmins << "\n";
    cout << lastmins << "\n";
    
    getch();
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Code:
    firsthour = atoi( strtok( firsttime, ":" ) );
    firstmins = atoi( strtok( NULL, ":" ) );
    
    lasthour = atoi( strtok( lasttime, ":" ) );
    lastmins = atoi(strtok( NULL, ":" ) );
    when you use NULL in the first parameter, it's like saying "start off with the last string i gave you"....a little more precisely, it says "start off at the index +1 where you last found ':' "
    Last edited by misplaced; 04-23-2005 at 01:48 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    More specifically, you can't strtok() two strings at the same time.
    As misplaced shows, deal with one string completely then move onto the next one.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User hello124's Avatar
    Join Date
    Mar 2005
    Location
    In a house
    Posts
    3

    Thanks guys

    OMG Its just sooooo obvious, I donít know how I missed looking it at that way.

    my code now works like it should

    Thanks guys Uís are all good

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I am extremely new to C++ and I am not allowed to use pointers
    OMG Its just sooooo obvious, I donít know how I missed looking it at that way.
    uhhhhmmm...ok, I'll ask then: why the heck can't you strtok() two strings at the same time?
    Last edited by 7stud; 04-23-2005 at 05:47 AM.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > why the heck can't you strtok() two strings at the same time?
    Because in strtok( NULL, ":" ), NULL means resume at the place you stopped in the previous strtok call.

    strtok( firsttime, ":" ) starts things off, and a series of NULL parameters means walk through the string that started at firsttime.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User hello124's Avatar
    Join Date
    Mar 2005
    Location
    In a house
    Posts
    3
    Quote Originally Posted by 7stud
    uhhhhmmm...ok, I'll ask then: why the heck can't you strtok() two strings at the same time?
    wellllll.... in my case i was going to the end of the last string that was entered. while really i wanted to go to the end of just one string at a time

    it is possible to strtok two strings at the same time... but not when you are wanting to output the last string after the token in the first entered strtok function (eg using NULL to get to the last string after the token in the first entered strtok).
    As you just bypass the first entered null string and go to the last entered null string
    Last edited by hello124; 04-23-2005 at 06:16 AM.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Well, it seems strange to me that when you call strtok() on two different char arrays, there aren't two different places in memory where the strings are being chopped up. But, I realize now that strtok() is not called by the char arrays, e.g.

    str1.strtok(":");
    str2.strtok(":");

    and therefore when you do this:

    strtok( NULL, ":" );

    there is no reference to which string you are calling the function on, so that must have something to do with it.
    Last edited by 7stud; 04-23-2005 at 06:24 AM.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Let me clear the air in case there's any confusion.

    strtok will parse a string with more than one token in it with successive calls.
    like so:

    Code:
    char t[] = "12:45:58"
    char *p = 0;
    p = strtok(t, ":"); 	// p[...] == "12"
    p = strtok(0, ":"); 	// p[...] == "45"
    p = strtok(0, ":"); 	// p[...] == "58"
    And this is why you can't (fully) strtok 2 strings at once
    Code:
    char t[] = "12:45:58"
    char s[] = "1:15:20";
    char *p = 0;
    p = strtok(t, ":"); 	// p[...] == "12"
    p = strtok(s, ":");     // p[...] == "1";
    p = strtok(0, ":"); 	// p[...] == "15"
    p = strtok(0, ":"); 	// p[...] == "20"
    p = strtok(t, ":"); 	// p[...] == "12"
    strtok is also somewhat "global", consider:
    Code:
    void strTokNull(void)
         { cout << strtok(0, ":") << endl;  }
    
    void strTokNew(char *s)
         {  cout <<  strtok(s, ":") << endl; }
    
    int main()
    {
        char s[] = "12:58:20";
        strTokNew(s);
        strTokNull();
    
        return 0;
    }
    if "s" was declared within strTokNew, when we call strTokNull, "s" no longer exists and strtok magically returns null without seg faulting.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 08:29 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM

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