Hi folks,
I have googled around, read a lot and still can't figure it out. I know a bit of C++, but am far from literate in all commands. I am much happier with QBasic (old i know, but it works).
I have made qb code and need to convert it to C++ to teach myself the basics of random files access so i can actually use the knowledge in the program i have to make for my coursework. I have started on the C++ code, but am now stuck!
QB code : (If you got Vista/7/os-x or linux and want to run this code, you can get QB64 here : http://WWW.QB64.NET/)
TYPE SalesRecord
Item1 AS INTEGER
Item2 AS INTEGER
Item3 AS INTEGER
Item4 AS INTEGER
Item5 AS INTEGER
END TYPE
REDIM TempRec(100) AS SalesRecord
OPEN "SalesRecord.txt" FOR RANDOM AS #1 LEN = LEN(TempRec())
FOR i% = 0 TO 99
TempRec(i%).Item1 = INT(RND * 5)
TempRec(i%).Item2 = INT(RND * 5)
TempRec(i%).Item3 = INT(RND * 5)
TempRec(i%).Item4 = INT(RND * 5)
TempRec(i%).Item5 = INT(RND * 5)
PUT #1, i% + 1, TempRec(i%)
NEXT
CLOSE #1
REDIM TempRec(100) AS SalesRecord
OPEN "SalesRecord.txt" FOR RANDOM AS #1 LEN = LEN(TempRec())
FOR i% = 0 TO 99
GET #1, i% + 1, TempRec(i%)
PRINT TempRec(i%).Item1; TempRec(i%).Item2; TempRec(i%).Item3; TempRec(i%).Item4; TempRec(i%).Item5
NEXT
CLOSE #1
And the C++ code i have made (VC++2008, console program)
#include "stdafx.h"
#include "iostream"
#include "fstream"
using namespace std;
//Prototype function declerations
ifstream& OpenRecords();
struct SalesRecord
{
int Item1, Item2, Item3, Item4, Item5;
};
int main ()
{
//Title
cout << " Employee sales records.\n" << endl;
//Get the file stream or quit if the records file is not found.
ifstream& fileStream = OpenRecords();
// count the number of entries, create array to match and load the data
cout << "\n Loading records, please wait..." << endl;
while(!fileStream.eof())
{
//Count or calculate number of entries, create array of SalesRecord struct and store
// the data in it
}
// Allow the user to see the final screen before exit.
system("PAUSE");
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 is exited.
ifstream& OpenRecords()
{
// Make sure the records file exists before running the program
cout << "\n Welcome, performing record check..." << endl;
// Create a pointer to the filename
const char* FilePntr = "SalesRecords.txt";
// Attempt to open the file, if good return pointer of open file stream else report error and exit.
ifstream* CheckFileStream = new ifstream(FilePntr);
if (CheckFileStream->good())
{
cout << "\n Record file found."<<endl;
}
else // Inform of file not found/or empty file and exit the program
{
cout << "\n RECORD FILE NOT FOUND!\nTHIS PROGRAM WILL NOW EXIT\n"<<endl;
delete CheckFileStream;
system("PAUSE");
system("EXIT");
}
return *CheckFileStream;
}
So what i am aiming to do is,
-Open the file
-Count the number of entries in it (all the same size)
-Create an array to hold the data
-Read it
after that i should be ok, any ideas on getting that far would be great,
Many thanks,
John