0
votes

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?)

3
You should add the errors you get to the question.HolyBlackCat

3 Answers

1
votes

You'll have to write some sort of conversion function. Something like:

std::optional<Sex> CharToSex(char c) {
    switch (c) {
        case 'M':
        case 'm':
            return Sex::M;
        case 'F':
        case 'f': 
            return Sex::F;
        default:
            return std::nullopt;
    }
}

If you don't like std::optional then you could use an exception instead.

You'll also have to declare CharToSex above where you use it but that's not relevant to your question.

Your compiler error specifically is due to Sex not being known in GetSex()

Just swap around the enum definition and function declaration.

1
votes

After moving enum Sex {M, F}; above the function declaration, and adding the missing includes and using namespace std;, I get no errors.

But still, Sex mySex = static_cast<Sex>(sexChar); doesn't do what you think it does. There's no built-in way to convert a string (or a character) to a enum. (See the other answer for how it can be done.)

Instead, this line treats sexChar as a numeric value, and creates a Sex with that numeric value. static_cast<Sex>(0) would give you M, and static_cast<Sex>(1) would give you F. Casting any other number to Sex causes undefined behavior. (In some cases it's allowed to cast a number to a enum even if it doesn't have a constant with that value, but it doesn't apply here; you can find more details at cppreference).

0
votes

Replace:

Sex GetSex(fstream&);
enum Sex { M, F };

in:

enum Sex { M, F };
Sex GetSex(fstream&);