Welcome to Dream.In.Code
Getting C++ Help is Easy!

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!




Reading File into multiple array

 
Reply to this topicStart new topic

Reading File into multiple array

zerogee
2 Sep, 2008 - 04:34 PM
Post #1

New D.I.C Head
Group Icon

Joined: 20 Aug, 2008
Posts: 49

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
User is offlineProfile CardPM
+Quote Post

sensui
RE: Reading File Into Multiple Array
2 Sep, 2008 - 11:07 PM
Post #2

D.I.C Head
Group Icon

Joined: 24 Aug, 2008
Posts: 132



Thanked: 20 times
Dream Kudos: 50
My Contributions
Declare 4 arrays:
cpp
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 ? smile.gif -->
User is offlineProfile CardPM
+Quote Post

zerogee
RE: Reading File Into Multiple Array
3 Sep, 2008 - 06:05 AM
Post #3

New D.I.C Head
Group Icon

Joined: 20 Aug, 2008
Posts: 49

QUOTE(sensui @ 3 Sep, 2008 - 12:07 AM) *

Declare 4 arrays:
cpp
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 ? smile.gif -->


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
User is offlineProfile CardPM
+Quote Post

zerogee
RE: Reading File Into Multiple Array
3 Sep, 2008 - 02:41 PM
Post #4

New D.I.C Head
Group Icon

Joined: 20 Aug, 2008
Posts: 49

QUOTE(zerogee @ 3 Sep, 2008 - 07:05 AM) *

QUOTE(sensui @ 3 Sep, 2008 - 12:07 AM) *

Declare 4 arrays:
cpp
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 ? smile.gif -->


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;

CODE

#include <iostream>
#include <fstream>
#include <cstdlib>   // needed for exit()
#include <string>

using namespace std;

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  
  }  

for(i=0; i<5; i++)
  {
  cout << Account_Number[i] << "  " << First_Name[i] << "  "
         << Last_Name[i] << "  " << Balance[i];
   }

  inFile.close();

  return 0;
}


This post has been edited by zerogee: 3 Sep, 2008 - 02:46 PM
User is offlineProfile CardPM
+Quote Post

sensui
RE: Reading File Into Multiple Array
3 Sep, 2008 - 11:22 PM
Post #5

D.I.C Head
Group Icon

Joined: 24 Aug, 2008
Posts: 132



Thanked: 20 times
Dream Kudos: 50
My Contributions
Instead of:
cpp
for(i=0; i<5; i++)
{
cout << Account_Number[i] << " " << First_Name[i] << " "
<< Last_Name[i] << " " << Balance[i];
}

use this:
cpp
int n = i;
for(i=0; i<n; i++)
{
cout << Account_Number[i] << " " << First_Name[i] << " "
<< Last_Name[i] << " " << Balance[i];
}


I hope that this helps, if not reply me.
User is offlineProfile CardPM
+Quote Post

zerogee
RE: Reading File Into Multiple Array
4 Sep, 2008 - 05:33 AM
Post #6

New D.I.C Head
Group Icon

Joined: 20 Aug, 2008
Posts: 49

QUOTE(sensui @ 4 Sep, 2008 - 12:22 AM) *

Instead of:
cpp
for(i=0; i<5; i++)
{
cout << Account_Number[i] << " " << First_Name[i] << " "
<< Last_Name[i] << " " << Balance[i];
}

use this:
cpp
int n = i;
for(i=0; i<n; i++)
{
cout << Account_Number[i] << " " << First_Name[i] << " "
<< Last_Name[i] << " " << Balance[i];
}


I hope that this helps, if not reply me.


No it still does nothing, and again it complies without any errors or warnings. I think it is hanging up on this part;

CODE

while(!inFile.eof() )
  {  
  inFile >> Account_Number[i] >> Last_Name[i]  
         >> First_Name[i] >> Balance[i];  
  i++; //count how many records you have  
  }

User is offlineProfile CardPM
+Quote Post

sensui
RE: Reading File Into Multiple Array
4 Sep, 2008 - 06:10 AM
Post #7

D.I.C Head
Group Icon

Joined: 24 Aug, 2008
Posts: 132



Thanked: 20 times
Dream Kudos: 50
My Contributions
This is the complete program:
cpp
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string>

using namespace std;

int main() {
string filename = "Bank_Records_data.dat";
ifstream inFile;

int Account_Number[100], Balance[100];
string Last_Name[100], First_Name[100];

inFile.open( filename.c_str() );

if ( inFile.fail() ) {
cout << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
exit(1);
}

int i = 0;
while ( !inFile.eof() ) {
inFile >> Account_Number[i] >> Last_Name[i]
>> First_Name[i] >> Balance[i];
i++;
}

int n = i;
for ( i = 0; i < n; i++ ) {
cout << Account_Number[i] << " " << First_Name[i] << " "
<< Last_Name[i] << " " << Balance[i] << endl;
}

inFile.close();

cin.get();
return EXIT_SUCCESS;
}


for example if the contents of your Bank_Records_data.dat file are:
CODE
123 Last_Name1 First_Name1 321
321 Last_Name2 First_Name2 123
213 Last_Name3 First_Name3 312


then it will output:
CODE
123 Last_Name1 First_Name1 321
321 Last_Name2 First_Name2 123
213 Last_Name3 First_Name3 312


I hope that this will help you..., if not please reply.
User is offlineProfile CardPM
+Quote Post

zerogee
RE: Reading File Into Multiple Array
4 Sep, 2008 - 02:31 PM
Post #8

New D.I.C Head
Group Icon

Joined: 20 Aug, 2008
Posts: 49

QUOTE(sensui @ 4 Sep, 2008 - 07:10 AM) *

This is the complete program:
cpp
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string>

using namespace std;

int main() {
string filename = "Bank_Records_data.dat";
ifstream inFile;

int Account_Number[100], Balance[100];
string Last_Name[100], First_Name[100];

inFile.open( filename.c_str() );

if ( inFile.fail() ) {
cout << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
exit(1);
}

int i = 0;
while ( !inFile.eof() ) {
inFile >> Account_Number[i] >> Last_Name[i]
>> First_Name[i] >> Balance[i];
i++;
}

int n = i;
for ( i = 0; i < n; i++ ) {
cout << Account_Number[i] << " " << First_Name[i] << " "
<< Last_Name[i] << " " << Balance[i] << endl;
}

inFile.close();

cin.get();
return EXIT_SUCCESS;
}


for example if the contents of your Bank_Records_data.dat file are:
CODE
123 Last_Name1 First_Name1 321
321 Last_Name2 First_Name2 123
213 Last_Name3 First_Name3 312


then it will output:
CODE
123 Last_Name1 First_Name1 321
321 Last_Name2 First_Name2 123
213 Last_Name3 First_Name3 312


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.
User is offlineProfile CardPM
+Quote Post

sensui
RE: Reading File Into Multiple Array
4 Sep, 2008 - 11:20 PM
Post #9

D.I.C Head
Group Icon

Joined: 24 Aug, 2008
Posts: 132



Thanked: 20 times
Dream Kudos: 50
My Contributions
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 ).
User is offlineProfile CardPM
+Quote Post

zerogee
RE: Reading File Into Multiple Array
5 Sep, 2008 - 06:01 AM
Post #10

New D.I.C Head
Group Icon

Joined: 20 Aug, 2008
Posts: 49

QUOTE(sensui @ 5 Sep, 2008 - 12:20 AM) *

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

i don't know why it doesn't work for me.
User is offlineProfile CardPM
+Quote Post

sensui
RE: Reading File Into Multiple Array
5 Sep, 2008 - 06:24 AM
Post #11

D.I.C Head
Group Icon

Joined: 24 Aug, 2008
Posts: 132



Thanked: 20 times
Dream Kudos: 50
My Contributions
change:
cpp
int Account_Number[100], Balance[100];

to:
cpp
int Account_Number[100];
double Balance[100];


You read from that file some real numbers, so you must declare Balance[100] as a double, do you understand?

Hope that helps...

This post has been edited by sensui: 5 Sep, 2008 - 06:25 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 07:27AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month