Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Difficulty with void Function and inFile (http://www.go4expert.com/forums/difficulty-void-function-infile-t23807/)

Sendo 9Nov2010 11:03

Difficulty with void Function and inFile
 
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.

xpi0t0s 9Nov2010 13:40

Re: Difficulty with void Function and inFile
 
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.

xpi0t0s 9Nov2010 13:43

Re: Difficulty with void Function and inFile
 
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).

Sendo 9Nov2010 21:12

Re: Difficulty with void Function and inFile
 
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...

Sendo 9Nov2010 21:22

Re: Difficulty with void Function and inFile
 
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.

Sendo 9Nov2010 21:33

Re: Difficulty with void Function and inFile
 
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. :|

Sendo 10Nov2010 04:23

Re: Difficulty with void Function and inFile
 
Problem solved.

xpi0t0s 10Nov2010 14:31

Re: Difficulty with void Function and inFile
 
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.


All times are GMT +5.5. The time now is 23:18.