2

my text file was like

Jason Derulo
91 Western Road,xxxx,xxxx
1000
david beckham
91 Western Road,xxxx,xxxx
1000

i'm trying to get the data from a text file and save it into arrays however when i want to store the data from the text file into array it loop non-stop. what should i do ? the problem exiting in looping or the method i get the data from text file ?

code:

#include <iostream>
#include <fstream>

using namespace std;

typedef struct {

    char name[30];
    char address[50];
    double balance;

} ACCOUNT;

//function prototype
void menu();
void read_data(ACCOUNT record[]);

int main() {
    ACCOUNT record[31]; //Define array 'record'  which have maximum size of 30
    read_data(record);  
}
//--------------------------------------------------------------------

void read_data(ACCOUNT record[]) {
    ifstream openfile("list.txt");              //open text file 

    if (!openfile) {
        cout << "Error opening input file\n";
        return 0;
    } else {
        int loop = -1;                  //size of array 
        cout << "--------------Data From File--------------"<<endl;
        while (!openfile.eof())  {
        if (openfile.peek() == '\n') 
            openfile.ignore(256, '\n');
        openfile.getline(record[loop].name, 30);
        openfile.getline(record[loop].address, 50);
        openfile >> record[loop].balance;
        }
        openfile.close();               //close text file

        for (int i = 0; i <= loop + 1; i++) {
            cout << "Account "  << endl;
            cout << "Name         : " << record[i].name << endl;
            cout << "Address      : " << record[i].address << endl;
            cout << "Balance      : " << record[i].balance << endl;
        }
    }
}
Yu Hao
  • 111,229
  • 40
  • 211
  • 267
Lcr
  • 75
  • 7
  • 3
    `loop` is `-1`, and you access `record[loop]`? – Yu Hao Aug 18 '14 at 01:48
  • 1
    Your `void` function tries to return an `int`. Also, don't do [`while (eof)`](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – user657267 Aug 18 '14 at 01:55

1 Answers1

1

Use ifstream::getline() instead of ifstream::eof() in tandem with >>. The following is an illustrative example, (and for simplicity I didn't check to see if the stream opened correctly).

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

#define ARR_SIZE 31

typedef struct {
    char name[30];
    char address[50];
    double balance;
} ACCOUNT;

int main() {
  ACCOUNT temp, record[ARR_SIZE];
  ifstream ifile("list.txt");
  int i=0;
  double d=0;

  while(i < ARR_SIZE) {
    ifile.getline(temp.name, 30, '\n');//use the size of the array
    ifile.getline(temp.address, 50, '\n');//same here

    //consume the newline still in the stream:    
    if((ifile >> d).get()) { temp.balance = d; }

    record[i] = temp;
    i++;
  }

  for (int i=0; i < ARR_SIZE; i++) {
    cout << record[i].name << "\n" 
         << record[i].address << "\n" 
         << record[i].balance << "\n\n";
  }
  return 0;
}

Another recommendation would be to use vectors for record array, and strings instead of char arrays.

REFERENCES:

Why does std::getline() skip input after a formatted extraction?

Community
  • 1
  • 1
jrd1
  • 9,026
  • 4
  • 30
  • 48
  • Thank you very much !!! i get it..but why you want to use the use for .get() for the "balance" , cant we just direct use ifile>> ? – Lcr Aug 18 '14 at 06:08
  • @noobies: We use `.get()`, because `getline()` doesn't remove the `'\n'` from the stream. `.get()` does that for us. See the cited link for more. – jrd1 Aug 18 '14 at 06:12