5 Replies - 1255 Views - Last Post: 29 July 2009 - 09:25 PM Rate Topic: -----

#1 hakkai2508  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 03-November 08

reading data from a file and storing them in array

Posted 29 July 2009 - 11:17 AM

please help me on this. i want to store the data from a file into an array of objects, but i dont know how.

here's my code

#include <iostream>
#include <conio>
#include "Account.h"
#include <fstream>

int main()
{
	int accType, transType;
   char accName[21];
   char accHolder[17];
   float amtWithdraw;
   float amtDeposit;

   ifstream dataFile;
   dataFile.open("name.txt", ios::in);

   Account customer[10];

   //data for the objects are stored in a file. i need to store the inside
   //respective palce in this array

   if (transType==1)
   {
   	cout<<"Please enter the amount you wish to withdraw: \n";
	  cin>>amtWithdraw;

	  customer.withdraw(amtWithdraw);
   }

   else if (transType==2)
   {
   	cout<<"Please enter the amount you wish to deposit: \n";
	  cin>>amtDeposit;

	  customer.deposit(amtDeposit);
   }

   else if (transType==3)
   {
   	cout<<"Balance in account: \n";
	  cout<<customer.getBalance();
   }

   else
   {
   	cout<<"Please enter your transaction by pressing 1, 2, or 3: \n";
   	cin>>transType;
   }

   customer.printDetail();

   getch();
   return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: reading data from a file and storing them in array

#2 wildgoose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 468
  • Joined: 29-June 09

Re: reading data from a file and storing them in array

Posted 29 July 2009 - 12:14 PM

Using transType befor its set. I'm assuming its encoded in the file but you forgot to write the input code.

An integer array? (+) is deposit (-) is withdrawal?

Allocate with growth such as realloc. But you're apparently using C++ so new[] delete[]. So overallocated, copy records, advance index.
Or possible fixed int array of 10 records but set index at 0.
uint nMaxRec = 10;
uint nRecCnt = 0;
int *acct = new int[ nMaxRec ];
if (nRecCnt == nMaxRec)
{
	int *tmp = new int[ MaxRec + nGrow ];
	for (n = 0; n < nMaxRec; n++)
	{
	   tmp[n] = acct[n];
	}
	delete[] acct;
	acct = tmp;
	nMaxRec += nGrow;
}

acct[ nRecCnt++ ] = val;


Many options open to you!


Planning on using a class? You have customer.depost() customer.withdrawal() but no class declarations.

This post has been edited by wildgoose: 29 July 2009 - 12:23 PM

Was This Post Helpful? 0
  • +
  • -

#3 hakkai2508  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 03-November 08

Re: reading data from a file and storing them in array

Posted 29 July 2009 - 07:57 PM

View Postwildgoose, on 29 Jul, 2009 - 11:14 AM, said:

Using transType befor its set. I'm assuming its encoded in the file but you forgot to write the input code.

An integer array? (+) is deposit (-) is withdrawal?

Allocate with growth such as realloc. But you're apparently using C++ so new[] delete[]. So overallocated, copy records, advance index.
Or possible fixed int array of 10 records but set index at 0.
uint nMaxRec = 10;
uint nRecCnt = 0;
int *acct = new int[ nMaxRec ];
if (nRecCnt == nMaxRec)
{
	int *tmp = new int[ MaxRec + nGrow ];
	for (n = 0; n < nMaxRec; n++)
	{
	   tmp[n] = acct[n];
	}
	delete[] acct;
	acct = tmp;
	nMaxRec += nGrow;
}

acct[ nRecCnt++ ] = val;


Many options open to you!


Planning on using a class? You have customer.depost() customer.withdrawal() but no class declarations.


i declare the class in a header file. inside the text file there are names, account numbers and transaction types in form of integers from 1-3. how can i set those values inside its respective customer account? it is something like this:

customer[1].Account(500, 1, narlina) - 500, 1 and narlina are read from file. how can i make make sure that the right data is sent to the constructor?
Was This Post Helpful? 0
  • +
  • -

#4 wildgoose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 468
  • Joined: 29-June 09

Re: reading data from a file and storing them in array

Posted 29 July 2009 - 08:17 PM

I don't understand.
Your file has a transaction amount, transaction type, and name?
As you read a line you create a record and post the information to the record.

You should probably post a couple sample lines from your text data file.
You need to create a structure that contains the account record. As you read a line record from the file, you create a data record and fill it in. You indicate that the text file contains the data record so why do you have a data entry section? Or do you read the file to input the records then append keyboard entered records, then save them back to disk when the session is over?
Was This Post Helpful? 0
  • +
  • -

#5 hakkai2508  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 03-November 08

Re: reading data from a file and storing them in array

Posted 29 July 2009 - 08:57 PM

View Postwildgoose, on 29 Jul, 2009 - 07:17 PM, said:

I don't understand.
Your file has a transaction amount, transaction type, and name?
As you read a line you create a record and post the information to the record.

You should probably post a couple sample lines from your text data file.
You need to create a structure that contains the account record. As you read a line record from the file, you create a data record and fill it in. You indicate that the text file contains the data record so why do you have a data entry section? Or do you read the file to input the records then append keyboard entered records, then save them back to disk when the session is over?


this is a sample of a line from the file:

NOR ARLINA AMIRAH *1289384758987465 1 900.0

the 1st one is name, the one which starts with asterisk is the account number, the next digit is the account type and the last one is the balance inside the account. i have to modify the code so that it can read these data and i can use it to implement member functions. the data input from keyboard is not necessary.

any help? im stuck here trying to figure out the correct way to do this
Was This Post Helpful? 0
  • +
  • -

#6 hakkai2508  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 03-November 08

Re: reading data from a file and storing them in array

Posted 29 July 2009 - 09:25 PM

View Postwildgoose, on 29 Jul, 2009 - 11:14 AM, said:

Using transType befor its set. I'm assuming its encoded in the file but you forgot to write the input code.

An integer array? (+) is deposit (-) is withdrawal?

Allocate with growth such as realloc. But you're apparently using C++ so new[] delete[]. So overallocated, copy records, advance index.
Or possible fixed int array of 10 records but set index at 0.
uint nMaxRec = 10;
uint nRecCnt = 0;
int *acct = new int[ nMaxRec ];
if (nRecCnt == nMaxRec)
{
	int *tmp = new int[ MaxRec + nGrow ];
	for (n = 0; n < nMaxRec; n++)
	{
	   tmp[n] = acct[n];
	}
	delete[] acct;
	acct = tmp;
	nMaxRec += nGrow;
}

acct[ nRecCnt++ ] = val;


Many options open to you!


Planning on using a class? You have customer.depost() customer.withdrawal() but no class declarations.


i dont understand the code. can u explain a little bit?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1