Reputation: 10587
I'm just starting to learn C++. What are my options for testing if a string starts with a number? My function is_page_number
does the trick (I think) in the following program. Is this a bad idea? How can I use a regex here? If any part of the code is not well-written, any comment is appreciated.
#include <string>
#include <iostream>
#include <fstream>
using std::string;
using std::cout;
using std::ifstream;
using std::endl;
bool is_page_number(const string& aline) {
return aline[0] == '1' | aline[0] == '2' | aline[0] == '3' | aline[0] == '4' | aline[0] == '5' | aline[0] == '6' | aline[0] == '7' | aline[0] == '8' | aline[0] == '9';
}
int main() {
const string temp_filename("test_input.txt");
ifstream input(temp_filename.c_str());
string one_line;
while (getline(input,one_line)) {
if(is_page_number(one_line)) {
cout << "page number: ";
}
cout << one_line << endl;
}
}
Upvotes: 5
Views: 3878