Results 1 to 3 of 3
I'm trying to get my program to go through the string typed in by the user and strip it of EVERYTHING but the numbers. I can't place my finger on ...
- 05-08-2010 #1Just Joined!
- Join Date
- May 2010
- Posts
- 3
[SOLVED] deleting parts of a string
I'm trying to get my program to go through the string typed in by the user and strip it of EVERYTHING but the numbers. I can't place my finger on what I'm missing. Help please?

Code:
PS everything compiles ok, but I get a runtime error anytime I hit enter after entering any data.Code:#include<iostream> #include<string> using namespace std ; int main() { string myString ; cout << "Please enter data: " ; getline( cin, myString ) ; // disgard anything that's not an int for ( int x = 0 ; myString.length() ; x++ ) if ( !isdigit( myString[ x ] ) ) myString.erase( x, 1 ) ; // display contents cout << myString << endl ; } // end main
- 05-08-2010 #2
I tried this and it appears to work...As you delete characters from your string it shrinks, so to correct for this --x.
Code:#include<iostream> #include<string> using namespace std ; int main() { string myString ; cout << "Please enter data: " ; getline( cin, myString ) ; // disgard anything that's not an int for ( int x = 0 ; x < myString.length(); x++ ) if ( !isdigit( myString[ x ] ) ) { myString.erase( x, 1 ) ; --x; } // display contents cout << myString << endl ; } // end mainMake mine Arch Linux
- 05-08-2010 #3Just Joined!
- Join Date
- May 2010
- Posts
- 3
That solved it! I knew it was something small I was missing.
Thanks a bunch dude!


