Why does this code not work?

Ok so in my program it's supposed to load names from a file and i wanted to make soem error handling in case the file was empy or missing so i did, however the if statement for checking if the file is empty doesnt seem to be working, when i run my program the cmd box displays really strange symbols and starts beeping, so what am i doing wrong?

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
void GetCustomerNames(Player &player, Weapon &weapon, Customer &customer)
{
    ifstream loadCustomerNames;
    string tempNameStorage;
    string nameSize;

    loadCustomerNames.open("customer names.txt");

    if(loadCustomerNames.fail())
    {
        cout << "There was an error loading the customer names" << endl;
    }
    else if(!loadCustomerNames.fail())
    {
        for(int i = 0; i < 3; i++)
        {
            getline(loadCustomerNames, tempNameStorage);
            if(tempNameStorage.empty())
            {
                cout << "Error file was empty" << endl;
            }
            else if(!tempNameStorage.empty())
            {
                customer.customerName.push_back(tempNameStorage);
            }

        }

        for(int i = 0; i < 3; i++)
        {
            cout << customer.customerName[i] << endl;;
        }
    }
}
Are you sure "customer names.txt" is really a plain text file? You didn't save it with Word, did you? Open it in Notepad to be sure it really is plain text.
Last edited on
im 110% sure its a plain text file
Let's see the file then, or part of it anyways.
Also, doesn't this seem a bit silly?
Line 9: if(loadCustomerNames.fail())
...
Line 13: else if(!loadCustomerNames.fail())
Same goes for lines 18 and 22.
Last edited on
the file is empty, theres nothin in it to see.
That's your problem - the .fail() state is only set when you try to actually read something, not when you just open a blank file or read the last byte.
Ok so i did this, is this better? It works anyways

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
void GetCustomerNames(Player &player, Weapon &weapon, Customer &customer)
{
    ifstream loadCustomerNames;
    string tempNameStorage;
    string nameSize;

    loadCustomerNames.open("customer names.txt");

    //If the file is missing then execute this code

    if(loadCustomerNames.fail())
    {
        cout << "There was an error loading the customer names" << endl;
    }
    else
    {
        getline(loadCustomerNames, tempNameStorage);

        if(tempNameStorage.empty())
        {
            cout << "Error file was empty, cannot proceed." << endl;
        }
        else
        {
            for(int i = 0; i < 3; i++)
            {
                customer.customerName.push_back(tempNameStorage);
            }

            for(int i = 0; i < 3; i++)
            {
                cout << customer.customerName[i] << endl;;
            }
        }
    }
}
Topic archived. No new replies allowed.