Hello, what's the best way to sort a string to ints & chars. For example:

"John Doe M 36"

to
John Doe (char[])
M (char)
36 (int)

I researched about istreamstring sin(), but I'm not yet fully familiar with it, any tips. Thanks!

## All 6 Replies

Yes, use an istringstream.

``````string s = "John Doe M 36";
istringstream ss( s );

string name, given_name, surname;
char sex;
unsigned age;

ss >> given_name >> surname >> sex >> age;
name = given_name + " " + surname;``````

Hope this helps.

Thank you, but my problem is I have to anticipate that the user might enter the data on a different format like:

M 36 John Doe Mary F 45

Any tips? Thanks again!

This is code btw,

``````#include <iostream>
#include <string>
#include <sstream>

int main()
{
std::string token, text("M 36 John Doe Mary F 45");
std::istringstream iss(text);
while ( getline(iss, token, '') )
{
std::cout << token << std::endl;
}
return 0;
}``````

But all of them are still strings, I want to set each one as data type appropriate.

Something you can try,

1) Create tokens from data, are garu suggested.
2) Insert those tokens into a vector
3) Call std::partition on the vector giving it your own comparison
3.a) You own comparison, could return true if the token is digit, thus separating digits from strings
4) From there you can proceed to go on further

Require your users to use specific input formats. Something like "M 36 John Doe Mary F 45" is nonsense at best and malformed otherwise. If you have two people then that should assume two inputs. If your user puts nonsense into the program, tell him!

So, You can assume one of the following by specifically restricting your user to:
Given Name, Surname, Age, and Sex, where the name is in one of the standard forms: "First Last" or "Last, First".

``````M 36 John Doe    M 36 Doe, John
36 M John Doe    36 M Doe, John
M John Doe 36    M Doe, John 36
36 John Doe M    36 Doe, John M
John Doe 36 M    Doe, John 36 M
John Doe M 36    Doe, John M 36``````

You can easily determine which is which by examining the type of the string.
If you have "M" or "F" or "Male" or "Female" or "m" or "f" or "male" or "female" or "MALE" or some other variation, then you know it represents the sex. If you have a number than it should represent the age. Otherwise it must be a name: If it ends with a comma (,) then it represents the last name -- otherwise it is given name. The next name item is either the surname or the given name, depending on what the first encountered name is.

There is a potential issue, mind you. Not all people have such simple names. Something like "Juaquin Antonio Carrera Hernandez III" is entirely possible. How will you parse that? What if he doesn't have a second given name, as in "Juaquin Carrera Hernandez III"? Is "Carrera" his middle name or his father's family surname? You can, of course, require that the input only have the first given name and the father's familial surname, which takes us back to the simple case above, but allows for potential conflicts for two people, say "Juan Cuevas Garcia" and "Juan Cuevas Castro" -- both would be listed as "Juan Cuevas".

See how this gets really obnoxious, really fast?

Do you have specific guidelines as to what kinds of input you can expect? Or can you require input to have a specific structure? Otherwise, you are pretty jacked and will have to rely on some heuristics to (hopefully) get it right most of the time.

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.