Thread: deleting parts of a string

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    9

    Red face 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:
    #include<iostream>
    #include<string>
    using namespace std ;
    
    int main()
    {
    	string myString ;
    
    	cout << "Please enter string/numbers: " ;
    	
    	getline( cin, myString ) ;
    
    	// disgard anything that's not an int
    	for ( int x = 0 ; x < 10 ; x++ )
    	if ( !isdigit( myString[ x ] ) )
    		myString.erase( x, 1 ) ;
    
    	// display contents
    	cout << myString << endl ;
    } // end main

  2. #2
    Registered User jdragyn's Avatar
    Join Date
    Sep 2009
    Posts
    96
    Are you sure every string that a user types in is going to be exactly 10 characters long?
    C+/- programmer extraordinaire

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    That's another thing... when I make the number greater than 10 (say 15) I get an error when I try to enter anything into the program at runtime...

  4. #4
    Registered User jdragyn's Avatar
    Join Date
    Sep 2009
    Posts
    96
    Well, aside from needing to use the .length() operator (or another method entirely), what exactly is it not doing that you need it to do?
    C+/- programmer extraordinaire

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    Hmmm... I tried the .length as you suggested, but still get the runtime error... I'm trying to make sure that the user doesn't enter any data other than numbers.

    Code:
    #include<iostream>
    #include<string>
    using namespace std ;
    
    int main()
    {
    	string myString ;
    
    	cout << "Please enter string/numbers: " ;
    	
    	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

  6. #6
    Registered User jdragyn's Avatar
    Join Date
    Sep 2009
    Posts
    96
    What do you think happens to the string when you erase a character from it?
    C+/- programmer extraordinaire

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    sounds to me like you need to create a way to block all input except numbers from being entered, have you tried using a
    Code:
    if(cin.fail==1){}
    which will trigger if any letter is used to represent a number (such as typing "seven" instead of "7")?

    another possible option would be to to use the pop_back() function, however u would have to swap the value u want to get rid of with the last value, then resort, if the list was sorted
    Last edited by Creatlv3; 05-07-2010 at 07:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM