Question

I realize this is my second posting on this same topic and I thank you all for your patience with my less than stellar attempt at getting this to work. It's been a couple days since I last posted and I'm still trying to figure out why the loop insists on terminating after it has read 15 of the entries in the input file.

My professor provides us with a linker that contains the main() function and the two files that are present in the parameter, a sequential access input file and a random access output file, hence the acronym-ed names in the header. I have gotten all other instances working and such but me and my instructor haven't been able to figure out what is going on and I could really use some more help and any suggestions would be greatly appreciated.

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

const int DESC_SIZE = 37;

struct Item
{
int itemId;
char description[DESC_SIZE];
double cost, price;
};

int processFile(const char* saifFile, const char* raofFile)
{
fstream outFile, inFile;
Item Inventory;
int counter = 0;
int errorCode = 0;

inFile.open(saifFile, ios::in);
outFile.open(raofFile, ios::out | ios:: binary | ios::trunc);
if (!inFile.fail())
{
    cout << " Part ID  Part Cost  Part Price   Part Description" << endl;
    cout << " =======  =========  ==========   ================" << endl;
    inFile >> Inventory.itemId;
    if (!inFile.eof())
    {
    while (!inFile.eof() && counter <= 100 && errorCode == 0)
        {
            inFile >> Inventory.cost >> Inventory.price;
            inFile.getline(Inventory.description, DESC_SIZE);
            if (Inventory.itemId != counter)
                errorCode = -4;
            if (Inventory.cost < 0)
                errorCode = -5;
            if (Inventory.price < 0)
                errorCode = -6;
            cout << "      " << Inventory.itemId << "     " << setw(5) << Inventory.cost << "       " << setw(5) << Inventory.price <<" " << Inventory.description << endl;
            counter++;
            inFile >> Inventory.itemId;
        }
        if (!inFile.eof())
            errorCode = -3;
    }
    else
        errorCode = -2;
}
else
    errorCode = -1;
inFile.close();
switch (errorCode)
{
case -1:
    cout << "ERROR: Cannot open input and/or output file.\n";
    break;
case -2:
    cout << "ERROR: Empty input file.\n";
    break;
case -3:
    cout << "ERROR: More than 100 records in the input file.\n";
    break;
case -4:
    cout << "ERROR: Item id numbers out of sequence in the input file.\n";
    break;
case -5:
    cout << "ERROR: Found record with negative cost in input file.\n";
    break;
case -6:
    cout << "ERROR: Found record with negative price in input file.\n";
    break;
}
if (errorCode != 0)
    return errorCode;
return counter;

}

Was it helpful?

Solution

My best guess is that it's the following code that's causing the problem:

inFile >> Inventory.cost >> Inventory.price;
inFile.getline(Inventory.description, DESC_SIZE);

If there is a newline left after the input going into Inventory.price, it will disrupt the following getline() statement, as it stops when it reaches a newline character. You'll have to ignore it with ignore():

inFile >> Inventory.cost >> Inventory.price;
inFile.ignore(); // <==
inFile.getline(Inventory.description, DESC_SIZE);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top