I'm trying to make a function how_many(), which counts the number of characters of a certain type, say "\n" or ",", in a file stream. Here is my (failed) attempt:
int how_many(char mychar, anifstream myfile){
int result;
while(!myfile.eof())
{
if(myfile.get()==mychar){result=result+1;}
}
myfile.close(); // Closes the filestream "my file"
return result;
}
First question: is "\n" a character or a string? (If so, then I should make the first input a string instead of a character)
Second question: can you explain the resulting error message? (or alternatively, directly point out the misuse of syntax in my code):
warning: result of comparison against a string literal is unspecified
(use strncmp instead) [-Wstring-compare]
note: candidate function not viable: no known conversion from 'const char [2]' to
'char' for 1st argument