i can not read from a text file into a structure. i must really be missing something big. i haven't had c++ in years, but for this database class he wants this to be the first assignment to see how good we can code (i still have TONS more to do with the data, but i can't even get it to read properly)
i can open the file properly, just getting it into that struct seems impossible! it does the first 2 or 3 reads properly, after that its all garbage charecters.
i have put a good 10 hours into this (yes its sad that this is as far as i got) but ive been trying to figure out this problem that long, going through every book/web site i can find, and asking everyone i know. so far i have come up with nothing. i know i am missing something little...
please help, thanks!
Jon
here is the text file:
refered to as "transaction.h" in my program.
55 11/10/2002 8015 100 5 home 12345
56 12/21/2001 8025 150 2 office 34567
60 08/20/2002 8065 75 1 nursery 12345
61 03/12/2002 8025 150 2 office 45678
57 12/01/2002 8035 50 12 office 12345
58 12/20/2002 8045 200 17 office 56789
59 10/03/2002 8055 250 17 home 56789
56 12/21/2001 8045 200 7 office 34567
60 02/20/2002 8065 75 1 nursery 12345
58 12/20/2002 8025 150 9 office 56789
here's my code, almost fully commented:
Code:
#include <iostream.h>
#include <fstream.h> // Needed to open/close files
#include <stdlib.h> // Needed for exit()
#include <iomanip.h>
#include <istream.h>
ifstream transactionin; // Allows all functions access to tranaction file
struct Transaction // Declaration of Transaction Structure
{
int ordernumber;
char date[10];
int orderedproductid;
float priceperproduct;
int numberordered;
char productlineid[10];
int customerid;
};
void openfiles() // This function opens the needed files
{ // And also checks to make sure the files open properly
transactionin.open("transaction.h", ios::in, ios::nocreate); //open transaction.h file
if (transactionin == NULL) //check if file opened correctly
{
cout << "\nThe file transactionin.h was not opened, please check the file" << endl;
exit(1);
}
}
void closefiles() // This function closes the files opened earlier
{
transactionin.close();
}
void readdata() // This function reads data from the transaction file
{
const int MAXRECS = 10; //used to declare amount of space in the structure
int i = 0; //counter to increase the structure
Transaction records[MAXRECS] ; //declaring the structure with MAXRECS number
while(i<=MAXRECS) //process 10 data records, putting them into the struct.
{
transactionin >> records[i].customerid;
transactionin >> records[i].date ;
transactionin >> records[i].ordernumber ;
transactionin >> records[i].orderedproductid ;
transactionin >> records[i].numberordered;
transactionin >> records[i].priceperproduct ;
transactionin >> records[i].productlineid ;
transactionin >> records[1].customerid ;
i++;
}
cout << records[0].customerid << endl; //this record prints ok, so does date and order number
// but records[0].priceperproduct prints our garbage
cout << records[0].productlineid <<endl ;
cout << endl << records[1].customerid << endl; // prints garbage to the screen
}
void main()
{
openfiles(); // Call to the openfiles function
readdata(); // Call to the readdata function
closefiles(); // Call to the closefiles function
}