Extracting Integer From a txt File

Look at this code. this programme take a string from user store it into a text file then extract digits from that text file from a line and store it into another string leter on i will convert that into intereger veriable. but the problem is it extract all the digits from the line don't tream them seperatily if there will be two or more integers within a line.
look this line.

"There are 500 cars in wich 200 are new"
i want to take 500 as an integer and then 200 as another integer.
but my prgramme shows the result 500200 .
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
36
37
38
39
40
41
42
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <cctype>
using namespace std;

int main()
{

string textread;
string textshow;
string integer;
ofstream myfile;
myfile.open("example.txt", ios::trunc);
cout<<"please enter the text ";
getline(cin,textread);
myfile << textread;
myfile.close();

ifstream showfile;
showfile.open("example.txt", ios::in);
getline(showfile,textshow);
showfile.close();
for(int i= 0; i < textshow.size(); i++)
{
        if (isdigit(textshow[i]))
        {
           for(int j = i; j<textshow.size(); j++)
           {
                   if (isdigit(textshow[j]))
                  integer = integer+textshow[j];
                  i=j;
                  break;
                  }
                  } 
                  } 
                  
                  cout<<integer;             

    system ("PAUSE");
}


Please discuss and if there is any solution.
please any one have any idea ?
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
#include <string>
#include <iostream>
#include <sstream>
#include <vector>

std::vector<int> extract_ws_delimited_integers( const std::string& str )
{
    std::vector<int> result ;

    std::string word ;
    std::istringstream stm(str) ;
    while( stm >> word )
    {
        try
        {
            std::size_t pos ;
            int n =  std::stoi( word, &pos ) ;
            if( pos == word.size() ) result.push_back(n) ;
        }
        catch(...) {} //
    }
    
    return result ;
}

int main()
{
    auto seq = extract_ws_delimited_integers( "There are 500 cars in wich 200 are new" ) ;
    for( int v : seq ) std::cout << v << '\n' ;
}
Topic archived. No new replies allowed.