Iterator should read number from string. Stops after first numebr

I've written this code to extract numbers from a string using a string iterator.
The iterator picks up the first number and decides to call it a day.
Why's it so?

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
 string myAge = "I am 23 years old";
 string::iterator iterator;
 string number;
 stringstream convertToString;
 for(iterator = myAge.begin();iterator!=myAge.end();iterator++)
 {

              if(*iterator >= '0' && *iterator <='9')
              {
               convertToString << *iterator;
               convertToString >> number;
              }
             
 } 
 cout << number <<endl;   
 system("pause");
 return 0;
}

}


Output
 
2
Last edited on
You don't need to call convertToString >> number until after your "for" loop.
Your usage of stringstream is erroneous.
You read a char into convertTostring and output it into string number.

The erroneous usage causes the stringstream unable to clear the stream after puting '2' into number. That's why you see 2 as output.

You can either declare number as a char, or manually clear stringstream after the code
convertToString >> number;


Either way, the stringstream becomes healthy but you will still not observe "23".

If I were you, I would delete all these code and restart.
That's pretty harsh, h9uest, don't you think? This is the beginner's forum.
@Kooth: Oh right, right, right. Makes sense.

@h9guest: I want to keep it simple, but what are some of the functions you'd use if you were to code this. If you could give links, i could read them up.

Thanks Both of you :)
@Kooth:
Oh, I'm sorry about that.
In fact, I didn't mean it. I'm not a native speaker of English, so my wording might look harsh while in fact, I really don't mean it :)

This is the beginner's forum, I absolutely understand it, and I would atcually consider myself as a beginner as well.


You don't need to call convertToString >> number until after your "for" loop.

With all my respects, I'd like to point out that your solution doesn't work. The reason, I believe, still lies in the erroneous usage of stringstream.


@waqqassheikh:
If my previous post makes you unhappy in anyway, my appologies. I didn't mean it.

I'm afraid I can't give you proposals until you give the requirement of the function. For example, if the string contains multiple numbers, like "I am 12 years old 10 years ago.", do you want to extract the two numbers as two numbers or simply 1210?

In addition, do you HAVE to use iterator of stringstream? etc.


As an exercise, you probably want to think about how to do it by yourself. In case you need more help, get back here with a more rigorous definition of the problem and we'll see how we can help.

This code works for me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
    {
    string myAge = "I am 23 years old";
    string::iterator iterator;
    string number;
    stringstream convertToString;

    for( iterator = myAge.begin(); iterator != myAge.end(); iterator++ )
        {
        if( *iterator >= '0' && *iterator <='9' )
            {
            convertToString << *iterator;

            }   /*  if( a number )  */

        }   /*  for( iterator != myAge.end()    */

    convertToString >> number;

    cerr << "number is " << number << endl;

    cout << number <<endl;

    return 0;

    }
        /*  main()  */

Topic archived. No new replies allowed.