PirateMcplunder
October 17th, 2010, 09:43 AM
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 */
/* check first line for reference to the number of hops. */
found=first_input_line.find("number of hops");
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);
}
/* get first input line up to token "#" */
getline(input_buffer, before_token, '#');
/* DOESN'T WORK */
/* check first part of first line for integer type. */
found=before_token.find_first_not_of("0123456789 \t");
if (found==string::npos){
cerr << "Integer not found for the number of hops. Please check input:\n" << endl;
cerr << usage << endl;
exit (1);
}
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.
I believe I am following correct form (see: http://www.cplusplus.com/reference/string/string/find_first_not_of/). What am I missing?
Thank you.
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 */
/* check first line for reference to the number of hops. */
found=first_input_line.find("number of hops");
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);
}
/* get first input line up to token "#" */
getline(input_buffer, before_token, '#');
/* DOESN'T WORK */
/* check first part of first line for integer type. */
found=before_token.find_first_not_of("0123456789 \t");
if (found==string::npos){
cerr << "Integer not found for the number of hops. Please check input:\n" << endl;
cerr << usage << endl;
exit (1);
}
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.
I believe I am following correct form (see: http://www.cplusplus.com/reference/string/string/find_first_not_of/). What am I missing?
Thank you.