I have to develop a C++ program that reads the data records from the input file and stores them in a dynamic array of structs, which should be sized just enough for the problem. (TIP: Your program should first scan the input file to determine the number of records to be processed.) After loading the data into the array, the program then processes the array and write appropriate output to two files (named assign06a.out and assign06b.out, say).

i was trying to compile it and keep coming up with major errors can anyone tell me what im doing wrong to make it read from a file. oh and we have to us implematation file client file and aplication file. here are my implematation and client file.


#ifndefSTRUCT_INVENTORY_H
#defineSTRUCT_INVENTORY_H

#include<iostream>
#include<fstream>
#include<cstring>
#include<cstdlib>


struct inventory
{

int id;
char description[41];
double wholesalePrice;
double retailPrice;
double avgWeeklySales;
double quantityInStock;
};

int numofRecords();

void getdata(ifstream fin);

#endif

implemntation file here:


#include<iostream>
#include<cstdlib>
#include<cstring>
#include"assignment9.h"
using namespace std;

const int MAX_LEN = 30;


int numofRecords()
{

int numRecords;

cout <<"number of courses to process: ";
cin >> numRecords;

cin.ignore(999, '\n');

return numRecords;
}

int getdata(ifstream fin, ofstream fout)
{

char inputfilename[MAX_LEN + 1];

cout << "Input filename(up to"<<MAX_LEN<<" characters): ";
cin.get(inputfilename, MAX_LEN + 1, '\n');
cin.ignore(999, '\n');


fin.open(inputfilename, ios::in);


if(fin.fail())

{


cout<<"Error opening input file"<< inputfilename;
cout<<"press enter or return to continue...."<<endl;

cin.get();

exit(EXIT_FAILURE);
}

int numofRecords = 0;
inventory records;

fin >> ws

while(!fin.eof())
{
fin >> records.id;
if(fin.eof())

{
cerr<<"Input file contains an incomplete record"<<endl;
break;
}

fin.ignore(999,'\n');

fin>>ws

fin.get(record.description, MAX_LEN +1,'\n');
if(fin.eof())
{

cerr<<"inputfile contains an incomplete record"<<endl;
break;
}

fin.ignore(999,'\n');

fin >>ws

fin >>records.wholesalePrice;

if(fin.eof())
{
cerr<<"inputfile contains an incomplete record"<<endl;
break;
}
fin.ignore(999,'\n');


fin >> ws
fin >>records.retailPrice;
if(fin.eof())
{
cerr << "inputfile contains an incomplete record"<<endl;
break;
}
fin.ignore(999,'\n');


fin >> ws
fin>>record.avgWeeklySales;
if(fin.eof())
{
cerr << "inputfile contains an incomplete record"<<endl;
break;
}

fin >> ws
fin >>records.quantityInStock;

numofRecods ++;

fin.seekg(0);
fin.clear();

return numofRecords;

}

cout << records.id<< endl;
cout << records.description;
cout << records.wholesalePrice<< endl;
cout << records.retailPrice<< endl;
cout << records.avgWeeklySales<< endl;
cout << records.quantityInStock << endl;
cout << endl;
cout << "# of courses scanned: " << numOfCourses << endl;
cout << endl;

The declaration void getdata(ifstream fin); and its definition

int getdata(ifstream fin, ofstream fout)
{
...
}

does not match. You will get a link error.
Also I think I see a mismatch in the {} braces. This also should give you an EOF not found error. Use properly indented code, and you will catch most of these braces errors.

Those are some errors I see and there maybe more.
The other time you want us to find us the compiler errors, please post the error messages too. That is easy for us than copying and pasting your code and compiling it ourselves.

Use the CODE tags too. It makes the code more readable.

Edited 3 Years Ago by Dani: Fixed formatting

Use [code][/code] tags around your code.

Note: this code

cout<<"Error opening input file"<< inputfilename;
cout<<"press enter or return to continue...."<<endl;

if executed, would look like this:

Error opening input fileinput.txtpress enter or return to continue....
This article has been dead for over six months. Start a new discussion instead.