I have a file, InputFile.txt, in the format, firstname
, lastname
, score
:
Mike Smith 80
James Jones 75
I want to read those values into my c++ program using an ifStream&. This is how I am doing it:
std::ifstream inStream("InputFile.txt");
std::string firstname;
std::string lastname;
int score;
while(inStream.peek() != EOF)
{
inStream >> firstname >> lastname >> score;
//further processing ignored for brevity...
}
I want to check whether the firstname
and lastname
are isalpha() == true
and that the score
is all digits (probably with isdigit()
.
The following approach is not working:
while (inStream >> firstname)
{
for (auto element : firstname)
{
if (!isalpha(element))
{
std::cerr << "Error not a alpha character.";
}
}
}
What is the easiest way to do this?