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.
Reading tokens of string into variables.
Page 1 of 15 Replies - 5359 Views - Last Post: 23 January 2009 - 11:50 AM
Replies To: Reading tokens of string into variables.
#2
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 ':'
emerald_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.
12:34:20
is to be read into hour=12, minutes=34 and seconds=20.
#3
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.
#4
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 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
#5
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.
#6
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
and you can also convert a string to a number, using just 1 line
put it all together for an instant tokeniser
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.
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.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|