Problem with C++ check file method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    return 0;
}
void loadUp()
{
    cout << "Legends of Archaea starting up...\n";
    cout << "Checking for stats file...";

    /* Check for file statement */
    bool ifstream chfl(char *f)
    {
        if (f.good())
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    switch(chfl("stats.sta"))
    {
    case true:
        cout << "Stat file loaded. Checking for saves file...\n";
        break;

    case false:
        cout << "Stats file not found.\n";
        cout << "Attempting to create stats file...";

        fstream statFile;
        statFile.open("stats.sta");
        statFile << "HEALTH: 100\n";
        statFile << "MANA: 50\n";
        statFile << "GOLD: 00\n";
        statFile << "INVENTORY: \n";
        statFile.close();

        cout << "Stats file created. Checking for saves file...\n";
        break;
    }
}


When it gets to the 'chfl' boolean function, it says "expected initializer before 'chfl'" What does that mean?

I'm not exactly sure what's wrong with that. I'm a very much noob, so please excuse if there's a stupid mistake or something simple and obvious, I tried.

Any help would be awesome, thanks.
Last edited on
None of this...
1
2
3
4
5
6
7
8
9
10
11
12
    /* Check for file statement */
    bool ifstream chfl(char *f)
    {
        if (f.good())
        {
            return true;
        }
        else
        {
            return false;
        }
    }

...makes much sense in C++.

If you want to make a function, you can't do that inside another function, so you'll have to move it outside of loadUp().

Also you'll need to use the proper syntax:
http://cplusplus.com/doc/tutorial/functions/
Last edited on
Alright. thanks. I didn't know that.
defining function inside function .. that will not work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void loadUp()
{
    cout << "Legends of Archaea starting up...\n";
    cout << "Checking for stats file...";

 
    switch(chfl("stats.sta"))
    {
    case true:
        cout << "Stat file loaded. Checking for saves file...\n";
        break;

    case false:
        cout << "Stats file not found.\n";
        cout << "Attempting to create stats file...";

        fstream statFile;
        statFile.open("stats.sta");
        statFile << "HEALTH: 100\n";
        statFile << "MANA: 50\n";
        statFile << "GOLD: 00\n";
        statFile << "INVENTORY: \n";
        statFile.close();

        cout << "Stats file created. Checking for saves file...\n";
        break;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
   /* Check for file statement */
    bool ifstream chfl(char *f)
    {
        if (f.good())
        {
            return true;
        }
        else
        {
            return false;
        }
    }
Topic archived. No new replies allowed.