Difficulty with void Function and inFile

Discussion in 'C++' started by Sendo, Nov 9, 2010.

  1. Sendo

    Sendo New Member

    Joined:
    Nov 9, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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).
     
  4. Sendo

    Sendo New Member

    Joined:
    Nov 9, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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)
        //{
        [B]Errorfile.open ("C:\\Errorfile.out", ios::out);[/B]
        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++;
        //}
       [B] Errorfile.close();[/B]
        
    }
    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:
    [COLOR=Red]-10    0    S    Y    100.00[/COLOR]
    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
    [B]10    0    R    Y    10.00[/B]
    [COLOR=Red]17    3    D    R    15.00[/COLOR]
    5    0    D    Y    275.00
    [COLOR=Red]-3    10    D    Y    20.00
    14    -1    S    N    30.00
    20    3    D    Y    -10.00[/COLOR]
    
    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...
     
  5. Sendo

    Sendo New Member

    Joined:
    Nov 9, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  6. Sendo

    Sendo New Member

    Joined:
    Nov 9, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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. :|
     
  7. Sendo

    Sendo New Member

    Joined:
    Nov 9, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Problem solved.
     
  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Interesting. When I used ios::eek:ut, 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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice