I am trying to write a function GetSex()
that sets an enum value, Sex
, and prints it to the console in C++ using a value from an external .txt file but I am getting all sorts of compile errors due to incorrect syntax.
What do I have wrong here?
Code:
Sex GetSex(fstream&); //function prototype
enum Sex {M, F};
int main()
{
fstream inStream("InputText.txt"); //create fstream object
Sex mySex; // create enum object
mySex = GetSex(inStream);
cout << "Sex of first person is " << mySex;
return 0;
}
Sex GetSex(fstream& inStream)
{
//read in next letter in file
char sexChar;
inStream.get(sexChar);
// set enum from char
Sex mySex = static_cast<Sex>(sexChar);
return mySex;
}
InputText.txt contains single chars each on a new line indicating the sex of a person:
M
F
M
F
Errors:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
(10,5): error C2146: syntax error: missing ';' before identifier 'GetSex'
(21,9): error C2146: syntax error: missing ';' before identifier 'mySex'
(21,9): error C2065: 'mySex': undeclared identifier
(22,5): error C2065: 'mySex': undeclared identifier
(22,13): error C3861: 'GetSex': identifier not found
(23,42): error C2065: 'mySex': undeclared identifier
(42,11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
(42,1): error C2086: 'int Sex': redefinition
(10): message : see declaration of 'Sex'
(42,5): error C2146: syntax error: missing ';' before identifier 'GetSex'
(43,1): error C2143: syntax error: missing ';' before '{'
(43,1): error C2447: '{': missing function header (old-style formal list?)