0

The program is supposed to read transactions from a file and calculate who owes money to whom. But it should be possible to add a transaction through the keyboard. The code works fine when reading from a file (the calculations are correct and all) but adding a transaction through the keyboard does not work and I don't know why.

case 1:
        {
            cout << "Add transaction: " << endl;
            
            cin.ignore(); // Ignores Enter
            getline(cin, str); // reads a transaction (ex, 211105 food Nate 200 2 Walter Pete)
            
            stringstream stream(str);
            translist.read(stream);

            break; 
        }
void TransactionList::read(istream& is) {
    Transaction t;

    while (t.readTrans(is)) {
        addTrans(t);

    }
}
bool Transaction::readTrans(istream& is) {
    
    is >> date >> type >> name >> amount >> num_friends;

    for (int i = 0; i < num_friends; i++) {
        is >> friends[i];
    }

    return !is.eof();
}
void TransactionList::addTrans(Transaction& t) {

    trans[num_trans] = t;
    num_Trans++;
}
int main() {

    ifstream infil("transactions.txt"); //reads from a file
    ofstream utfil("a.txt");

    TransactionList translist; // Declaring an array of all the transactions

    translist.read(infil); //sending the stream to the read-function
    translist.print(utfil);

    PersonList personlist = translista.FixPersons(); // Makes some calculations etc 
    

    
    menu();
    options(translista, personlista); //a function with some cases (see case 1 at the top)
    
    return 0;
}
Schafa
  • 11
  • 2
  • Your mistake is in `readTrans()` you are checking `eof()` after processing the extracted data, the correct order is 1. extract, 2. test `good()`, 3. process extracted result – Ben Voigt Nov 05 '21 at 16:50

0 Answers0