Join 136,322 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,737 people online right now. Registration is fast and FREE... Join Now!
Ok, I am really stuck on this one. I need to read a file with 4 records of 4 intergers (Account_Number, Last_Name, First_Name, Balance) and read the records into 4 seperate arrays. the starting address of any element in the account array can then be calculated as the address of the first record in the array + (Account_Number - 1000) *sizeof(int). then the program should ask for a user entered account number and then display the corresponding record information. I wrote a program to read the file but i am not sure how to read it into 4 arrays. This is what i have so far. I have also attached my data file
CODE
#include <iostream> #include <fstream> #include <cstdlib> // needed for exit() #include <string> using namespace std;
int main() { string filename = "Bank_Records_data.dat"; // put the filename up front string line; ifstream inFile;
inFile.open(filename.c_str());
if (inFile.fail()) // check for successful open { cout << "\nThe file was not successfully opened" << "\n Please check that the file currently exists." << endl; exit(1); }
// read and display the file's contents while (getline(inFile,line)) cout << line << endl;
inFile.close();
cin.ignore(); // this line is optional
return 0; }
This post has been edited by zerogee: 2 Sep, 2008 - 04:35 PM
int Account_Number[100], Balance[100]; string Last_Name[100], First_Name[100];
then read them in a while loop:
cpp
int i = 0; while( !inFile.eof() ) { inFile >> Account_Number[i] >> Last_Name[i] >> First_Name[i] >> Balance[i]; i++; //count how many records you have }
Why aren't you using a struct or a class, anyway? It would be much easier. You need only one array of records.
Struct Example:
cpp
struct record { int Account_Number, Balance; string Last_Name, First_Name; }; record arrayOfRecords[100]; //.... int i = 0; while( !inFile.eof() ) { inFile >> arrayOfRecords[i].Account_Number >> arrayOfRecords[i].Last_Name >> arrayOfRecords[i].First_Name >> arrayOfRecords[i].Balance; i++; //count how many records you have }
Class Example:
cpp
class record { private: int Account_Number, Balance; string Last_Name, First_Name; public: record() { Account_Number = Balance = 0; Last_Name = ""; First_Name = ""; }; set( int acc, string last, string first, int bal ) { Account_Number = acc; Balance = bal; Last_Name = last; First_Name = first; }; ~record() { }; }; record arrayOfRecords[100]; //.... int i = 0; int acc, bal; string first, last; while( !inFile.eof() ) { inFile >> acc >> last >> first >> bal; arrayOfRecords[i].set( acc, last, first, bal ); i++; //count how many records you have }
int Account_Number[100], Balance[100]; string Last_Name[100], First_Name[100];
then read them in a while loop:
cpp
int i = 0; while( !inFile.eof() ) { inFile >> Account_Number[i] >> Last_Name[i] >> First_Name[i] >> Balance[i]; i++; //count how many records you have }
Why aren't you using a struct or a class, anyway? It would be much easier. You need only one array of records.
Struct Example:
cpp
struct record { int Account_Number, Balance; string Last_Name, First_Name; }; record arrayOfRecords[100]; //.... int i = 0; while( !inFile.eof() ) { inFile >> arrayOfRecords[i].Account_Number >> arrayOfRecords[i].Last_Name >> arrayOfRecords[i].First_Name >> arrayOfRecords[i].Balance; i++; //count how many records you have }
Class Example:
cpp
class record { private: int Account_Number, Balance; string Last_Name, First_Name; public: record() { Account_Number = Balance = 0; Last_Name = ""; First_Name = ""; }; set( int acc, string last, string first, int bal ) { Account_Number = acc; Balance = bal; Last_Name = last; First_Name = first; }; ~record() { }; }; record arrayOfRecords[100]; //.... int i = 0; int acc, bal; string first, last; while( !inFile.eof() ) { inFile >> acc >> last >> first >> bal; arrayOfRecords[i].set( acc, last, first, bal ); i++; //count how many records you have }
Was this post helpful ? -->
well the assignment calls for the file to be read into 4 arrays. but i will try your suggestion when i get home later. thx
int Account_Number[100], Balance[100]; string Last_Name[100], First_Name[100];
then read them in a while loop:
cpp
int i = 0; while( !inFile.eof() ) { inFile >> Account_Number[i] >> Last_Name[i] >> First_Name[i] >> Balance[i]; i++; //count how many records you have }
Why aren't you using a struct or a class, anyway? It would be much easier. You need only one array of records.
Struct Example:
cpp
struct record { int Account_Number, Balance; string Last_Name, First_Name; }; record arrayOfRecords[100]; //.... int i = 0; while( !inFile.eof() ) { inFile >> arrayOfRecords[i].Account_Number >> arrayOfRecords[i].Last_Name >> arrayOfRecords[i].First_Name >> arrayOfRecords[i].Balance; i++; //count how many records you have }
Class Example:
cpp
class record { private: int Account_Number, Balance; string Last_Name, First_Name; public: record() { Account_Number = Balance = 0; Last_Name = ""; First_Name = ""; }; set( int acc, string last, string first, int bal ) { Account_Number = acc; Balance = bal; Last_Name = last; First_Name = first; }; ~record() { }; }; record arrayOfRecords[100]; //.... int i = 0; int acc, bal; string first, last; while( !inFile.eof() ) { inFile >> acc >> last >> first >> bal; arrayOfRecords[i].set( acc, last, first, bal ); i++; //count how many records you have }
Was this post helpful ? -->
well the assignment calls for the file to be read into 4 arrays. but i will try your suggestion when i get home later. thx
Well i used sensui suggestion for writing in the 4 arrays and i added in a cout statement to verify the file was read in correctly. I complie the program and recieve no errors or warnings, but the program runs but nothing happens. A
A little more info, the reason i wrote the cout statement was that now i will need to write the program where it ask for an account number and when entered it returns the appropriate record. so with that being said here is what i have now;
int main() { int Account_Number[5], Balance[5]; string Last_Name[5], First_Name[5];
string filename = "Bank_Records_data.dat"; // put the filename up front string line; ifstream inFile;
int i = 0;
inFile.open(filename.c_str());
if (inFile.fail()) // check for successful open { cout << "\nThe file was not successfully opened" << "\n Please check that the file currently exists." << endl; exit(1); }
while(!inFile.eof() ) { inFile >> Account_Number[i] >> Last_Name[i] >> First_Name[i] >> Balance[i]; i++; //count how many records you have }
I hope that this will help you..., if not please reply.
still not working. I pasted the code you had previously posted and complied with 0 errors or warnings. but for some reason it still hangs on this code;
CODE
int i = 0; while ( !inFile.eof() ) { inFile >> Account_Number[i] >> Last_Name[i] >> First_Name[i] >> Balance[i]; i++; }
how do i know it hangs here. i insert a test cout statement, just befor this code and right after. the first cout statement returns ok but the other statement does not. i am at complete loss.
You can't insert a cout before that code because you don't have what to output. It works only if you first read that arrays from file and then output them with cout, like in my previous example ( I tried that code --> it works for me ).
You can't insert a cout before that code because you don't have what to output. It works only if you first read that arrays from file and then output them with cout, like in my previous example ( I tried that code --> it works for me ).
the cout statement is just a "test". i use it to see where a program stops sometime when needed. maybe it is my file it can't read. this is the file it's tries to read.
1000 Charles Butter 10435.7 1001 Hank Williams 2587.76 1002 Alfred Armstrong 600.87 1003 Amy Newman 7235.76 1004 Jessica Alba 768756