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:
Please tell me what I'm doing wrong, because the program crashes if I don't enter a 4 digit number.
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.
}
Comment