I've been learning C/C++ and I'm currently doing an ATM project. It all works fine but I would like to improve it so when i quit the program and it will load the data used previously.

currently I use a struct like this

struct database {
       int pin, sort, account, trans;     // pin, sortcode, account number and what transaction number their on
       float balance;                     // balance         
       string history[20];                // last 20 transactions
       string name;                       // name
} data [PEOPLE];                          // name is data and their are "PEOPLE" amount of records

and I currently load my data at the start like this and imports the data into the variables

void populate()

{
     int i;

     data[0].pin=1234;
     data[0].sort=123456;
     data[0].account=12341234;
     data[0].balance=456.43;
     data[0].name = "Mr. Sam Berwick";
     data[0].trans= 0;
     for(i=0;i!=19;i++)
     {
           data[0].history[i] = " ";
     }

     data[1].pin=2341;
     data[1].sort=234561;
     data[1].account=23412341;
     data[1].balance=4532.78;
     data[1].name = "Mr. David Reynolds";
     data[1].trans= 0;
     for(i=0;i!=19;i++)
     {
           data[1].history[i] = " ";
     }
}

How would I set it up so It imports a text file like this

1234
123456
12341234
456.43
Mr. Sam Berwick
0




















2341
234561
23412341
etc

Recommended Answers

All 2 Replies

Here's a good place to start: C++ File I/O

You just need to be consistent with your input data and know when to read and what you're reading. Post back if you have any questions.

EDIT: All data you read from text files is stored as strings. You will need to convert those strings to integers / floats if you want to be able to store them as such. These links will help you with that:

atoi - String to Integer Conversion
atof - String to Float Conversion

Maybe he should use a std::stringstream to convert a string to an integer/float. After all that's on Bjarne Stroustrup's website.

There are also infinite ways to do what you're asking, I would suggest playing with std::ifstream and std::ofstream. I don't usually like to suggest an approach, but I think formatting the data file like you have it (without those extra empty lines) will work just fine, as long as your data is separated on each line, and consistent.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.