Thanks MC.

by Unseen (no login)

I really appreciate the help. I have found that people in other forums on C++ answer with code understandable only to them and then they get annoyed if you don't understand it! They easily forget how hard it was for them to learn it in the first place!

Good point about the array, C++ makes me confused so i end up dping weird things for no reason sometime. I dont quite see what you did to make the struct normal (as you say) so it uses . instead of ->

I know you said file I/O is not your thing, but...everything seems to work apart from loading the numbers from the file! They are allways 0 in the program, i can't see why it does this though.

This version has as you suggested, binary format for the file, and i managed to fix the records size problem i was having to.

// JohnOnyon_10036178_Coursework1_C++.cpp : Defines the entry point for the console application.

// NOTE : This code has not been designed with re-use in mind. It has been coded to
// perform a specific task and in doing so may break OOP rules.

#include "stdafx.h"
#include "iostream"
#include "iomanip"
#include "fstream"
#include "string"
#include "ctime"

using namespace std;

//Prototype function declerations
ifstream& OpenRecords(const char* FilePntr);

//This struct defines how the records file is both built and read.
//4 SalesRecord structures must exist for a complete record section.
//Each SalesRecord is an individual employees sales for that day.
struct SalesRecord
{int Item1, Item2, Item3, Item4, Item5;};

int main ()
// Title
cout << " Employee sales records.\n" << endl;

// Get the file stream
ifstream& fileStream = OpenRecords("SalesRecords.txt");

// Count the number of entries, create array to match and load the data.
// This is done by seeking for the end if file flag and dividing the pointers
// position by the size of the SalesRecord structure.
int NumberOfRecords = fileStream.tellg () / sizeof(SalesRecord);
cout << "\n " << NumberOfRecords << " records in file."<< endl;

// Create array of SalesRecord struct and store the data in it.
SalesRecord* SalesRecs = new SalesRecord[NumberOfRecords]();

cout << "\n\n Loading records, please wait..." << endl;
for(int i = 0; i < NumberOfRecords; i++)
fileStream >> SalesRecs[i].Item1 >> SalesRecs[i].Item2 >> SalesRecs[i].Item3 >> SalesRecs[i].Item4 >> SalesRecs[i].Item5;
// Debug line
cout << SalesRecs[i].Item1 << SalesRecs[i].Item2 << SalesRecs[i].Item3 << SalesRecs[i].Item4 << SalesRecs[i].Item5 << endl;
// Allow the user to see the final screen before exit.
delete fileStream; //This generates a warining: deleting 'void*' is undefined
delete SalesRecs;
return 0;

// This function checks for the records files existance, if it exists, returns a pointer to the
// open file stream, If it does not exist then the program creates a dummy records file and
// restarts.
ifstream& OpenRecords(const char* FilePntr)
ifstream* InFileStream;
// Make sure the records file exists before running the program
cout << "\n Welcome, performing record check..." << endl;
// Attempt to open the file, if good return pointer of open file stream else report error and exit.
InFileStream = new ifstream(FilePntr,ios_base::binary);
if (InFileStream->good())
cout << "\n Record file found."<<endl;
else // If file not found create a dummy records file and restart the loading.
cout << "\n RECORD FILE NOT FOUND!\nCreating dummy data file...\n"<<endl;

//Delete the old filestream
delete InFileStream;
ofstream* OutFileStream;
SalesRecord sRec;

//make a new filestream for output (ofstream)
OutFileStream = new ofstream(FilePntr,ios_base::binary);

// Seed random number generator

// Set limits for false sales data
int MaxSales = 10, MinSales = 0;

// Create a sales record structure to use to write the file.
for (int TotalRec = 0; TotalRec <= 29; TotalRec++)
// create 4 records per section (4 employees, one for each)
for (int RecCnt = 0; RecCnt <= 3; RecCnt++)
sRec.Item1 = (rand() % MaxSales) + MinSales;
sRec.Item2 = (rand() % MaxSales) + MinSales;
sRec.Item3 = (rand() % MaxSales) + MinSales;
sRec.Item4 = (rand() % MaxSales) + MinSales;
sRec.Item5 = (rand() % MaxSales) + MinSales;

// Dump the random numbers to the file.
*OutFileStream << sRec.Item1 << sRec.Item2 << sRec.Item3 << sRec.Item4 << sRec.Item5;
//Debug line - Remove from final
//*CheckFileStream << " : " << TotalRec << RecCnt << " : ";
//Delete the old filestream
delete OutFileStream;
//make a new filestream for input (ifstream)
InFileStream = new ifstream(FilePntr,ios_base::binary);
return *InFileStream;

Posted on May 1, 2011, 10:10 PM

Respond to this message   

Return to Index

Response TitleAuthor and Date
You're welcome.Michael Calkins on May 2
 Well, this one outputs binaryUnseen on May 2
  Yes, it does.Michael Calkins on May 2
   Erm..Unseen on May 2
    This one seems to work :-)Michael Calkins on May 2
     You the man MC!Unseen Machine on May 2
      *ywMichael Calkins on May 2
     Here's my re-writeUnseen on May 2
      2nd Version -Unseen on May 2
       Nearly there...Unseen on May 2
        Re: Nearly there...Michael Calkins on May 2
         Thanks again MCUnseen on May 2
          arrays... (edited to fix post)Michael Calkins on May 3
           Multidimensional arrays and expandable arrays in C++ChronoKitsune on May 3
    fstream vs ifstream/ofstream (and other things about your code)ChronoKitsune on May 2
     Re: fstream vs ifstream/ofstream (and other things about your code)Michael Calkins on May 2
      (FIXED) Well, I'm not sure how well defined the behaviour is, but...ChronoKitsune on May 3
       *Thank you.Michael Calkins on May 3

 Copyright © 1999-2018 Network54. All rights reserved.   Terms of Use   Privacy Statement