5 Replies - 6482 Views - Last Post: 23 January 2009 - 11:50 AM Rate Topic: -----

#1 emerald_gal  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 64
  • Joined: 21-November 08

Reading tokens of string into variables.

Post icon  Posted 22 January 2009 - 04:41 AM

How can we take a string as input from the user and read different parts of it into variables. For example:
12:34:20
is to be read into hour=12, minutes=34 and seconds=20.
Is This A Good Question/Topic? 0
  • +

Replies To: Reading tokens of string into variables.

#2 ghst  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 29-October 08

Re: Reading tokens of string into variables.

Posted 22 January 2009 - 04:50 AM

The string class has a find function which finds certain characters in a string and returns the index of where it's located in the string. Or you could just write your own function that iterates through a string looking for ':'

View Postemerald_gal, on 22 Jan, 2009 - 03:41 AM, said:

How can we take a string as input from the user and read different parts of it into variables. For example:
12:34:20
is to be read into hour=12, minutes=34 and seconds=20.

Was This Post Helpful? 0
  • +
  • -

#3 epper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 20-January 09

Re: Reading tokens of string into variables.

Posted 22 January 2009 - 05:32 AM

For reading strings I believe its called indexOf('some char') which returns an in at position. Then you could use the .substr(from this point, to another point) to divide up into three separate strings. Then parse the strings if you want them as ints or output or whatever you want.
Was This Post Helpful? 0
  • +
  • -

#4 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Reading tokens of string into variables.

Posted 22 January 2009 - 01:27 PM

Is this C or C++? The methods for splitting strings and converting to numbers are totally different in both languages.

This recent thread might give you a couple of ideas - http://www.dreaminco...h...c=81224&hl=

This post has been edited by Bench: 22 January 2009 - 01:34 PM

Was This Post Helpful? 0
  • +
  • -

#5 emerald_gal  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 64
  • Joined: 21-November 08

Re: Reading tokens of string into variables.

Posted 22 January 2009 - 11:35 PM

Its actually C++, I know i can do this with strtok function, but i am not sure about the arguments, how will they change in the first, second and third sunsequent calls to the function.
Was This Post Helpful? 0
  • +
  • -

#6 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Reading tokens of string into variables.

Posted 23 January 2009 - 11:50 AM

strtok is a C function - I wouldn't advise using it. if you're writing C++, then use the C++ libraries.

All you need is 3 simple tools....
std::istringstream tokeniser("12:34:20"); - a 'buffer' to hold your raw, un-parsed data of "12:34:20" and do some tokenising.

std::string token; - a plain simple string to hold individual tokens after they've been separated/'tokenised' out of the buffer

int num; - an int to hold the converted number token.


you can grab a token from the buffer in C++ with just 1 line
std::getline(tokeniser, token, ':');

and you can also convert a string to a number, using just 1 line
std::istringstream(token) >> num;


put it all together for an instant tokeniser
#include <string>
#include <sstream>
#include <iostream>

int main()
{
    int num;
    std::string token;
    std::istringstream tokeniser("12:34:56");

    while( std::getline(tokeniser, token, ':') )
        if ( std::istringstream(token) >> num )
            std::cout << num << std::endl;
} 

Depending what you need the number for, it might be useful to put the number in a vector, or some other array/container for later use instead.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1