Write your question here.
Ok, im dealing with a string in the form of a line that i want to extract integers from with stringstreams. That overall is not my problem , its however the fact that there are some characters like "//" in that line i cant get around. It have to be with some sort of stringstream that i solve this with not a C language substitute like sscanf. i'll show an example of the line below.
1 2 3 4
f 4//1 13//1 9//1 // Please how do i deal with this line with the backlash in stringstream mechanics.
//This line below howver i can deal with no problem cuz no weird character (//) is in it
234 2 554 1122 907 girl to boy
So could 1 of u C++ masters detangle this please cuz i cant find something this specific on the web not even google
i need all the integers and yes it an obj file.
Can it be done with a stringstream mechanic or are u gonna refer me to sscanf cuz i've already tried the sscanf route and its causing me problem other places down the road. Only when im using stringsteams all problems go away.
- Use getline to get the whole line
- Put that into a stringstream
- Stringstream (with >> ) the output into:
--- the initial char (here, f)
--- a vector of strings
- For each string in that vector, tokenise it at the / symbol. You will have to say what to do about any absent integers (in your case, the middle one, as there is nothing between the //)
In that example it was simpler because it only extracted the first integer in each triad. So, in your example, it would effectively have taken
f 4 13 9
If you can give me a compelling reason why you need the (non-existent) middle integer and third integer as well then I'll have another look at it (when I have some time).
First off thank u very much for helping me, but no i dont need the middle // only the integers. when u say tolkenise do u mean this:
string line;
ifstream fe( filename );
while ( getline( fe, line ) )
{
istringstream bat( line )
char f;
string da, db, dbb, dbbb, dd;
// then something like this below. could u plz edit it out right what im suppose to do
bat>>f >>da >>'/'>>'/'>>db >> dc>>'/'>>'/'>>dbb>> dd>>'/'>>'/'>>dbbb;
}
See the way i set it up at the bottom line of code in this reply is that what u mean to solve the issue?
As in my example (please read it, or at least the readFile() subroutine).
Get whole vectors of strings first, e.g., "13//1". Then go through that individual string and pick out the integers (probably 13 0 1 in this instance, because the middle one is missing) by breaking down into substrings at the / symbol. See http://www.cplusplus.com/reference/string/string/
(I don't know regex, but it is possible that might also do the job for you).
In your example you can't write things like >> '/', since '/' isn't the name of a variable.
Hey lastchance (1980) i got it solved i used vector of string to edit out the '/' into ' ' (empty space) then use an istringstream to convert my desireables to int. Hence getting the results i wanted, i'll show u below:
for ( int i=0; i<veck.size(); i++) // to note: 'vect' is a vector of strings storing values
{
char f;
int a, b, b1, b2, c, d;
..................
.................
...............
for ( int j=0; i<(veck[i])[j].size();j ++)
{
if( (veck[i])[j]=='/')
{
(veck[i])[j]=' ';
}
}
string str(veck[i]);
istringstream thn(str);
thn>>f>>a>>b>>c>>b1>>d>>b2; // with the for loop i got rid of the '/' and replace them with spaces making it become a line without the / as a result of this its now easy to convert the string in the line to int(s) without any problems.
}
Well i just wanted u to know i solved it and if it wasnt for you to help bottle neck the possibilities this could've taken me a week or worse more. Hope this coding in my last reply will help u assist others to come. Similar to when u referred me to links earlier.So thank u and good bye.
@gore7,
The actual format for each triad is
i/j/k
where i, j, k are integers. However, you are allowed to miss some out, this being distinguished by adjacent /
So your program would change i/j/k to i j k
and i//k to i k
These have differing numbers of integers - so unless all triads are of that type then you may have a problem.