I am working on a program that involves a lot of string manipulation and after many hours of beating my head against the wall I cannot seem to figure out what is wrong with my use of the string::find_first_not_of function.
I have string::find working fine throughout the program but I cannot seem to get string::find_first_not_of to work properly.
I am attempting to identify whether or not the string before_token contains only digits and/or spaces/tabs. The input I am using does indeed only contain digits and a tab but I still end up seeing my error message every time.
The input line looks like this:
2 #number of hops
(the space between 2 and # is a tab)
Here is a snippit of the code that is not working. I can provide the rest if there isn't anything visibly wrong with this.
size_t found;
string first_input_line;
string before_token;
/* WORKS */[COLOR="Green"]
[INDENT]/* check first line for reference to the number of hops. */
found=first_input_line.find("number of hops");[/INDENT]
if (found==string::npos){
cerr << "No explicit reference to \"number of hops\" found in the comment for first input line." << endl;
cerr << usage << endl;
exit(1);
} [/COLOR]
[INDENT]/* get first input line up to token "#" */
getline(input_buffer, before_token, '#'); [/INDENT]
/* DOESN'T WORK */[COLOR="Red"]
[INDENT]/* check first part of first line for integer type. */
found=before_token.find_first_not_of("0123456789 \t");[/INDENT]
if (found==string::npos){
cerr << "Integer not found for the number of hops. Please check input:\n" << endl;
cerr << usage << endl;
exit (1);
}[/COLOR]
Any help or hints would be greatly appreciated. I really don't want to keep having to comment out my error checking code because I don't understand it.
size_t found;
string first_input_line;
string before_token;
/* WORKS */[COLOR="Green"]
[INDENT]/* check first line for reference to the number of hops. */
found=first_input_line.find("number of hops");[/INDENT]
if (found==string::npos){
cerr << "No explicit reference to \"number of hops\" found in the comment for first input line." << endl;
cerr << usage << endl;
exit(1);
} [/COLOR]
[INDENT]/* get first input line up to token "#" */
getline(input_buffer, before_token, '#'); [/INDENT]
/* DOESN'T WORK */[COLOR="Red"]
[INDENT]/* check first part of first line for integer type. */
before_token.find_first_not_of("0123456789 \t");[/INDENT] // <--- guess a 'found=' is missing here
if (found==string::npos){
cerr << "Integer not found for the number of hops. Please check input:\n" << endl;
cerr << usage << endl;
exit (1);
}[/COLOR]
There is nothing wrong with the code. before_token = "2\t", so there are not chars that aren't in "0123456789 \t" so find_first_not_of doesn't find any and returns npos. Just change == to !=. Now it doesn't make sense.