2 Replies - 836 Views - Last Post: 27 April 2013 - 06:25 PM Rate Topic: -----

#1 tyark  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 27-September 12

Writing Array to a .txt File

Posted 25 April 2013 - 06:10 PM

Hi, I seem to be having a problem with writing an array to a .txt file.

1. I load the .txt file into the array.
2. via a menu (only option 1 works at the moment) a function is called to add a new customer
3. the new customer data is recorded into the array (initialized at 20).
4. Returned, is the new number of total customers (pretty much customers + 1)
5. then I exit the menu loop in main and continue to the last part of the program which calls a function to save the customers


6. the problem is that when I write the new array to the text file, the last element of the array just gets attached to the end without creating a new line.
7. I'm confused because the previous customer data seems to get a new line every time the loop is finished.


What I tried:
I tried adding and end line at the end of the save customers loop, but that seems to just create more spaces.

The Question:
Can anyone please tell me what I am doing wrong, or which function is causing the problem. I have tried checking every function for a logic error and the only function I think is causing an error is the New Customer function, but I have no idea about how to fix it.






#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>
#include <fstream>

using namespace std;

struct account
{
    string acctNum;
    string name;
    float cBal;
    float sBal;
};


// PROTOTYPES
int menu();                         // done!
char subMenu();                     // done!
int loadCustomers(account[]);       // done!
void saveCustomers(account[], int); // done!
int newCustomer(account[], int);
int deleteCustomer(account[], int);
int findCustomer(account[], int);
void deposit(account[], int);
void withdrawal(account[], int);
void balance(account[], int);
void bankBalance(account[], int);



int main()
{
    const int size = 20;
    int customers = 0, choice = 0;
    account myAccount[size];
    customers = loadCustomers(myAccount);
    cout << "Loaded " << customers << " Customers\n";
    choice = menu();
    do
    {
        if (choice == 1)
        {
            customers = newCustomer(myAccount, customers);
            choice = menu();
        }
    }while (choice != 7);
    saveCustomers(myAccount, customers);
    return 0;
}




int menu()
{
    int choice = 0;
    do
    {
        cout << "Main Menu\n";
        cout << "==============\n";
        cout << "1.  New Account\n";
        cout << "2.  Delete Account\n";
        cout << "3.  Deposit\n";
        cout << "4.  Withdrawal\n";
        cout << "5.  Balance\n";
        cout << "6.  Bank Balanace\n";
        cout << "7.  Exit\n";
        cout << "==============\n";
        cout << "Enter Choice: ";
        cin >> choice;
    }while (choice < 1 || choice > 7);
    return choice;
}




int loadCustomers(account myAccount[])
{
    int count = 0;
    ifstream myFile;
    myFile.open("customer.txt");
    if (!myFile)
        cout << "no account file found";
    else
    {
        while (!myFile.eof())
        {
            getline(myFile, myAccount[count].acctNum, '#');
            getline(myFile, myAccount[count].name, '#');
            myFile >> myAccount[count].cBal;
            myFile.ignore(); 
            myFile >> myAccount[count].sBal;
            count++;
        }
    }
    myFile.close();
    return count;
}




void saveCustomers(account myAccount[], int customer)
{
    ofstream myFile;
    myFile.open("customerNEW.txt");
    int i = 0;
    
    while (i < customer)
    {
        myFile << myAccount[i].acctNum;
        myFile << '#';
        myFile << myAccount[i].name;
        myFile << '#';
        myFile << myAccount[i].cBal;
        myFile << '#';
        myFile << myAccount[i].sBal;
        i++;
    }
    myFile.close();
}



int newCustomer(account myAccount[], int customer)
{
    cout << "Enter The Account Number: ";
    cin.ignore();
    getline(cin, myAccount[customer].acctNum);
    cout << "Enter the Desired Name: ";
    getline(cin, myAccount[customer].name);
    
    myAccount[customer].cBal = 0;
    myAccount[customer].sBal = 0;
    customer++;
    return customer;
}





Origional text file looks like this: (customers.txt)
230#Random Person4#1000#500
130#Random Person3#20000#50000
140#Random Person2#1000#5500
150#Random Person1#100#150

Saved text file looks like this: (customersNew.txt)
230#Random Person4#1000#500
130#Random Person3#20000#50000
140#Random Person2#1000#5500
150#Random Person1#100#150123#Random Person0#0#0


Expected text file:
230#Random Person4#1000#500
130#Random Person3#20000#50000
140#Random Person2#1000#5500
150#Random Person1#100#150
123#Random Person0#0#0


Thank you for taking the time to read this and for helping me out.
:)/>

Is This A Good Question/Topic? 0
  • +

Replies To: Writing Array to a .txt File

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,563
  • Joined: 25-December 09

Re: Writing Array to a .txt File

Posted 25 April 2013 - 09:06 PM

Your problem is being caused by how you're reading your file. In the following snippet:

        while (!myFile.eof())
        {
            getline(myFile, myAccount[count].acctNum, '#');
            getline(myFile, myAccount[count].name, '#');
            myFile >> myAccount[count].cBal;
            myFile.ignore();
            myFile >> myAccount[count].sBal;


The last line leaves the end of line character in the input buffer. And since the next input is a getline() that is using the '#' as a delimiter this end of line character is being added to the acctNum field. So instead of having '1','3','0' for the second line you have '\n', '1', '3','0'. You need to remove this end of line character. You can use the following line after the last line above.

myFile.ignore(100,'\n');


You will then need to print a new line character when you write your file.

Also using eof() to control an input loop will usually lead to an extra entry. The eof() flag is not set until after you read past the end of line. I really recommend using getline() to get the entire line from the file and then using a stringstream to parse the line:

      string line;
      while (getline(myFile, line))
      {
         istringstream ins(line);
         getline(ins, myAccount[count].acctNum, '#');
         getline(ins, myAccount[count].name, '#');
         ins >> myAccount[count].cBal;
         ins.ignore();
         ins >> myAccount[count].sBal;
         count++;
      }


Jim
Was This Post Helpful? 3
  • +
  • -

#3 tyark  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 27-September 12

Re: Writing Array to a .txt File

Posted 27 April 2013 - 06:25 PM

Sorry for the late response, but I've been at school and work.

This helped me with my dilemma. Thank you for explaining what was going on, I didn't take into account the '\n' on the keyboard buffer. I can't thank you enough for taking the time to look through my code and helping me solve the problem, I really do appreciate it! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1