Difficulty with void Function and inFile

Sendo's Avatar, Join Date: Nov 2010
Light Poster
I'm trying to help my girlfriend with her coding and we seem to be stuck. Her code is suppose to open a file, read the information, list the errors, and also code it under a void function.
Currently the code is like this
Code:
#include <fstream>
#include <iostream>
#include <iomanip>

using namespace std;

void GetFile ( int &, int &, char &, char &, float &, int, int);

ifstream inFile;
ofstream outFile, Errorfile;

int main ()
{
    int Adult, Children, Count = 0, Error=0, cError;
    char MealType, Weekend, mError, wError;
    float Deposit, dError;
    inFile.open("C:\\DataFile6D.txt");
    if (!inFile)
    {
        cout << "Error in opening file." << endl;
        return 1;
    }
    while (inFile)
    {
        GetFile(Adult, Children, MealType, Weekend, Deposit, Error, Count);
        
        


    }
}

void GetFile (int&Adult, int&Children, char& MealType, char &Weekend, float &Deposit, int Error, int count)
{
    while (count <= 5)
    {
    Errorfile.open ("C:\\Errorfile.out");
    inFile >> Adult >> Children >> MealType >> Weekend >> Deposit;
    if (Adult < 0)
    {
        Errorfile << "Number of adults error in record: " << Adult << " " << Children << " " << MealType << " " << Weekend << " " << Deposit << " " << endl;
        Error++;
    }
    if (Children < 0)
    {
        Errorfile << "Number of childrens error in record: " << Adult << " " << Children << " " << MealType << " " << Weekend << " " << Deposit << " " << endl;
        Error++;
    }
    if (MealType != 'S')
    {
        if (MealType != 'D')
        {
            Errorfile << "Meal type error in record: " << Adult << " " << Children << " " << MealType << " " << Weekend << " " << Deposit << " " << endl;
            Error++;
        }
    }
    if (Weekend != 'Y')
    {
        if (Weekend != 'N')
        {
            Errorfile << "Weekend error in record: " << Adult << " " << Children << " " << MealType << " " << Weekend << " " << Deposit << " " << endl;
            Error++;
        }
    }
    if (Deposit < 0)
    {
        Errorfile << "Deposit error in record: " << Adult << " " << Children << " " << MealType << " " << Weekend << " " << Deposit << " " << endl;
        Error++;
    }
    count++;
    }
    
    
}
The datafile we're reading from looks like this
Code:
10    0    S    Y    100.00
27    3    D    Y    57.50
125    17    D    N    0.00
4    0    S    N    25.00
0    25    S    Y    23.75
250    43    D    N    500.00
0    0    D    N    0.0
10    0    R    Y    10.00
17    3    D    R    15.00
5    0    D    Y    275.00
-3    10    D    Y    20.00
14    -1    S    N    30.00
20    3    D    Y    -10.00
Biggest problem we're having is that the output only shows one line read.. (tested by putting negatives down the first 5 rows... only row one is shown with errors)
We did the count just to try to run it down 5 rows I believe. We can't get it to read the whole thing @_@

Thanks for the help in advance.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Errorfile.open ("C:\\_xfr\\Errorfile.out");

No mode specified. So it'll use the default. Which is...? (Previous post deleted cos I thought I'd made a mistake here. But I didn't.)

Also why is the loop inside GetFile? GetFile doesn't get from files (lesson here: don't use misleading function names), so the loop will only process the same line 5 times.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Also you never close Errorfile, so the changes you make won't be saved (they'll be rolled back when main exits and the cleanup code does its thing).
0
Sendo's Avatar, Join Date: Nov 2010
Light Poster
Okay so I made the following adjustments. And commented out the while statement inside of the function.
Code:
void GetFile (int&Adult, int&Children, char& MealType, char &Weekend, float &Deposit, int Error, int count)
{
    //while (inFile)
    //{
    Errorfile.open ("C:\\Errorfile.out", ios::out);
    inFile >> Adult >> Children >> MealType >> Weekend >> Deposit;
    if (Adult < 0)
    {
        Errorfile << "Number of adults error in record: " << Adult << " " << Children << " " << MealType << " " << Weekend << " " << Deposit << " " << endl;
        Error++;
    }
    if (Children < 0)
    {
        Errorfile << "Number of childrens error in record: " << Adult << " " << Children << " " << MealType << " " << Weekend << " " << Deposit << " " << endl;
        Error++;
    }
    if (MealType != 'S')
    {
        if (MealType != 'D')
        {
            Errorfile << "Meal type error in record: " << Adult << " " << Children << " " << MealType << " " << Weekend << " " << Deposit << " " << endl;
            Error++;
        }
    }
    if (Weekend != 'Y')
    {
        if (Weekend != 'N')
        {
            Errorfile << "Weekend error in record: " << Adult << " " << Children << " " << MealType << " " << Weekend << " " << Deposit << " " << endl;
            Error++;
        }
    }
    if (Deposit < 0)
    {
        Errorfile << "Deposit error in record: " << Adult << " " << Children << " " << MealType << " " << Weekend << " " << Deposit << " " << endl;
        Error++;
    }
    
    //count++;
    //}
    Errorfile.close();
    
}
I left the name as is because I think she might be adding more stuff that actually "get's from file?" not too sure though.

I also adjusted my Datafile like this:
Code:
-10    0    S    Y    100.00
27    3    D    Y    57.50
125    17    D    N    0.00
4    0    S    N    25.00
0    25    S    Y    23.75
250    43    D    N    500.00
0    0    D    N    0.0
10    0    R    Y    10.00
17    3    D    R    15.00
5    0    D    Y    275.00
-3    10    D    Y    20.00
14    -1    S    N    30.00
20    3    D    Y    -10.00
Currently it's only showing the bolded line in my errorfile, when the other red lines are also errors.. I did adjust the first line after my first test run(the first line was originally didn't have the -10) but I erased the stuff in Errorfile and it's still only outputting the bolded line and none of the rest. I'm just confused as to why it's reading the same line over and over again now...
0
Sendo's Avatar, Join Date: Nov 2010
Light Poster
OH wait... my bad it's giving me the last line.... so I guess it's overriding somehow?
20 3 D Y -10.00
This one.
0
Sendo's Avatar, Join Date: Nov 2010
Light Poster
Nevermind, I erased the negative on there and now my Errorfile is blank... so it seems to be either reading only the last line... or erasing everything each time it runs through the lines. :|
0
Sendo's Avatar, Join Date: Nov 2010
Light Poster
Problem solved.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Interesting. When I used ios::out, it didn't write all output to the file, probably cos it overwrote the previous contents; I had to use ios::app instead to append. Anyway if it works that's the main thing.