0

im reading a number of detals into a linked list and I want to stop at a certain point, the file list constains the following details: ID name surname and town, after these are all through there is then accoutn numbers : number balance, it is a bank account list, heres my code. I want it to stop when the bank accoutn numbers begin as I plan to place them into a different linked list.

#include "list.h"   
#include "customer.h"
#include <iostream>             // cout
#include <fstream>      

#define FN  "file.txt"  // hardwired file name
#pragma once
using namespace std;

customerlist customerlist;  

int main () {

int data ;

ifstream in;
in.open(FN);
if (in.fail()) {
    cout << "unable to open " << FN << endl;
    getchar();  // type key to dismiss window
    return 0;
}

while (!in.eof()) {



    customer *ustomer = new customer();
    in >> ustomer->ID >> ustomer->name >> ustomer->lastname >> ustomer->town;
    cout << ustomer->ID<< " " << ustomer->name << "  " << ustomer->lastname<< "  "         <<     ustomer->town << endl ;
    customerlist.add(ustomer);

}
in.close();



  return 0;
}

sample of input data:

80013484 ADAMS           Aiden           Clonakilty
80034596 ADAMS           Anna-Marie      Athlone

accoutn info:

90009074 80007964      11640
90000034 80007964      -6458

1 Answers1

0

If I understand correctly that the customer list and the list of transactions between accounts are both in the file file.txt, then your question seems to be:

"How can my program distinguish between lines in an input file that provide customer data and lines that provide transaction data?"

Given that question, there are two generic approaches to the problem:

1. Make the input file easy to parse

The simplest approach, if we are allowed to modify the input file, is to ensure that the input file has a format that is easy to parse. As an example, we could change the input file so that it reads something like:

C 80013484 ADAMS           Aiden           Clonakilty
C 80034596 ADAMS           Anna-Marie      Athlone
T 90009074 80007964      11640
T 90000034 80007964      -6458

Given this input file, the input code could be written as:

char flag;
in >> flag;
switch (flag)
    {
    case 'C':    customer *ustomer = new customer();
                 in >> ustomer->ID >> ustomer->name >> 
                     ustomer->lastname >> ustomer->town;
                     customerlist.add(ustomer);
                 break;
    case 'T':    transaction *ransaction = new transaction();
                 // etc.
                 break;
    }

Or, 2. Parse the existing file

If we can't change the format of the existing file, we need to find a way to distinguish the different types of record from the existing context. In this case, the fact that the customer records have four fields and the transaction records have three fields, or the fact that the customer records have a non-numeric second field while the transaction records have numeric second fields might be used to distinguish them.

To count fields, the second answer to Split a string in C++? shows a straightforward approach.

Alternatively, we could initially assume that every record is a transaction record and then deal with the error that will occur when we try to read the second field into an integer. See How to catch invalid input in C++? for a good discussion of the different ways of doing this.

Whichever approach was used, there would need to be some additional error-checking code for any practical application.

Community
  • 1
  • 1
Simon
  • 9,962
  • 1
  • 26
  • 43