Getting the first numbers from a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • toefraz
    New Member
    • Nov 2007
    • 19

    #1

    Getting the first numbers from a string

    Hey, guys. I'm writing a program that searches through a census file and returns a name for a inputted rank. I've been trying to get this function to parse the rank string to only get only the digits in the first 4 places so that it would only return a string whose value ranges from 0 - 9999. Here's what I have so far, and it's not done by any means:

    Code:
    void rankSearch()
    {
    	string str1 = "", str2 = "";
    
    	int rank, count = 0, leave;
    
    	cout << "Please enter a ranking (1-1000):  ";
    	getline(cin >> ws, str1);
    	do
    	{
    	for(count = 0; count < 4; count++)
    		{
    			if(isdigit(str1.at(count)))
    				str2.push_back(str1.at(count));
    			else
    				break;
    		}
    		
    		rank = atoi(str1.c_str());
    	
    		if(rank <= 0 || rank > 1000)
    		{
    			cout << "Invalid rank. Please try again:  ";
    			getline(cin >> ws, str1);
    			leave = 0;
    		}
    		else
    			leave = 1;
    	}while(leave == 0);
    
    	cout << "Names for that rank are:  " << rank << endl; //This doesn't actually do anything yet. Just for testing purposes.
    
    }
    Please tell me what I'm doing wrong, because the program crashes if I don't enter a 4 digit number.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    One thing I see is you are mixing getline with cin >> statements. Shouldn't it be:

    [CODE=cpp]getline(cin, str1);[/CODE]

    Next, after you setup str2 to hold all the digits, you use atoi on str1? Then what is the point of str2?

    Comment

    • toefraz
      New Member
      • Nov 2007
      • 19

      #3
      I had it where it would change str2 into a char array, then just use atoi on the the char array, but i changed it to just use a .c_str() on str1. I just didn't remove the str2. I got it working finally. I just had to fill up the string past 4 characters otherwise it would try to access out-of-bound data.

      Comment

      • toefraz
        New Member
        • Nov 2007
        • 19

        #4
        Here's my final function. Tell me if you think there is a better way to go about doing this:

        [CODE=c]void rankSearch()
        {
        string str;
        int rank = 0, count = 0, leave = 0;

        cout << "Please enter a ranking (1-1000): ";

        do
        {
        getline(cin >> ws, str);
        str += "fillfillfi ll"; //Fill up the string to prevent issues if there are less than 4 characters in it.

        for(count = 0; count < 4; count++)
        {
        if(str.at(0) == '0')
        {
        rank = -1;
        str.erase((coun t + 1), str.length());
        break;
        }
        if(isdigit(str. at(count)))
        continue;
        else
        {
        rank = -1;
        str.erase((coun t + 1), str.length());
        break;
        }
        }

        rank = atoi(str.c_str( ));

        if(rank <= 0 || rank > 1000)
        {
        cout << "Invalid rank. Please try again: ";
        leave = 0;
        }
        else
        leave = 1;
        }while(leave == 0);

        cout << endl;
        cout << "Names in rank " << rank << " are: " << endl << endl;
        for(count = 0; count < 10; count++)
        cout << " " << (count * 10) + 1900 << ": " << info[count][rank - 1][0] << " and "
        << info[count][rank - 1][1] << endl;

        }[/CODE]

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Well, I don't know exactly what your function is doing, now do I know why (or how) getline(cin >> ws, str) is working. BUT, if your function works, it works. Are there different ways to do this? Probably, but the most important thing about a program is that it works.

          Comment

          Working...