aba

closed account (jiEA7k9E)
aba
Last edited on
closed account (D80DSL3A)
It looks like you closed the for loop too late. Lines 21-23 should be outside the loop.
Move the } on line 27 to line 20 and remove the break. It works. I just tested it in the C++ shell here. It even extracts the 123 from hello123abc


EDIT: Just for fun, I did this earlier after seeing your thread title.
Extract all of the integers from a string and stuff them into a vector:
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
#include<iostream>
#include<vector>
using namespace std;

bool isDigit( char c ){ return (c>='0' && c<='9'); }

void find_nums( string& s, vector<int>& v )
{
    v.clear();
    bool digit = false;
    int n=0;
    for(size_t i=0; i<s.length(); ++i)
    {
        if( isDigit( s[i] ) )
        {
            digit = true;
            n *= 10;
            n += (int)( s[i] - '0' );
        }
        else if( digit )
        {
            v.push_back(n);
            n = 0;
            digit = false;
        }
    }
}

int main()
{
    vector<int> vec;
    string str("hey12what6.89 g0ing 12 on1127over here5.");

    find_nums( str, vec );

    for(size_t i=0; i<vec.size(); ++i)
        cout << vec.at(i) << ' ';

    cout << endl;
    return 0;
}

EDIT2: I just realized I neglected to #include<string>, yet the code builds both in Code::Blocks and the C++ shell here.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main(){	
	string s ="abc123v";
	string s2 = "";
	for(auto x: s){
		if(isdigit(x)) s2 += x;
	}
	int n= stoi(s2);
	cout << n;
return 0;
}
closed account (jiEA7k9E)
thanks fun2code for helping.
Why did you delete your original post?

Now how can other people learn from it?

What gives? Why would you even think doing that is a good idea?
Why did you delete your original post?

have seen before, this kind of thread deletion after problem solved.
some kind of inferiority complex maybe?
closed account (18hRX9L8)
anup30 wrote:
some kind of inferiority complex maybe?

Someone suggested that it's because the OP wants to hide from his/her professor(s) that he/she is getting online help.
Topic archived. No new replies allowed.