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;
        }
    }
}
2
  • 3
    loop is -1, and you access record[loop]? Commented Aug 18, 2014 at 1:48
  • 1
    Your void function tries to return an int. Also, don't do while (eof). Commented Aug 18, 2014 at 1:55

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

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>> ?
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.