ok guys one quick question for you. If im reading a file say formated like so..

123 d52.55 w52.66 d55.55 d66.66
456 w55.55 d55.55 d66.6


and i want to read the first variable so 123 and match it up with another file. how do i go about reading the rest of the line without running into the next line. I want to devide up each part ie. d is for deposit and then amount and for the account number 123. I have a program that runs but it assigns all withdrawl and deposits for 123 so it skips over 456 and dosnt read it.

int fillArrayT( PersonAcct customerArray[7], ifstream& inFileT)
{

	char tranType = 'w';
	float amount;
	int accountno;
	int i=0;
	customerArray[i].withdrawl = 0;
	customerArray[i].deposit = 0;

	while(!inFileT.eof())
	{
		cout << " outer while loop" << endl; // testing purpose
			inFileT >> accountno;
			for ( i = 0 ; i< 6 ; i++)
			if ( accountno == customerArray[i].acct_num )
			{ 
				cout << " TESTING" << endl; // testing purpose

				while(inFileT >> tranType >> amount)  
				{
					cout << " money" << endl; // testing purpose
					switch(tranType)    

					{       
					case 'd': // deposit   

						customerArray[i].deposit = customerArray[i].deposit + amount;
						customerArray[i].timesD = 0;
						customerArray[i].timesD +=1;
						cout << "value d" << amount ; // testing purpose

						break;    




					case 'w': // withdrawal    


						customerArray[i].withdrawl =	customerArray[i].withdrawl - amount;
						customerArray[i].timesW = 0;
						customerArray[i].timesW +=1;
						cout << " value w"<< amount; // tesing purpose
						break;    
					}   
						

				}
				
			
		}
	}

Dani AI

Generated

Short answer: read and parse the file line-by-line instead of extracting transactions directly from the ifstream. Your inner while(inFileT >> tranType >> amount) keeps pulling tokens across newline boundaries, so once you match account 123 it keeps consuming every following token (including lines for 456). was on the right track with stringstreams — use std::getline to get one line, then parse that line with std::istringstream so parsing stops at the line end.

A minimal pattern to follow:

#include <string>
#include <sstream>

std::string line;
while (std::getline(inFileT, line)) {
    if (line.empty()) continue;
    std::istringstream iss(line);

    int accountno;
    if (!(iss >> accountno)) continue;

    int idx = findAccountIndex(customerArray, 7, accountno); // implement lookup

    if (idx < 0) continue; // missing account

    char tranType;
    float amount;
    while (iss >> tranType >> amount) {
        if (tranType == 'd') { customerArray[idx].deposit += amount; ++customerArray[idx].timesD; }
        else if (tranType == 'w') { customerArray[idx].withdrawl -= amount; ++customerArray[idx].timesW; }
    }
}

Notes and troubleshooting

  • Initialize every account element (deposit, withdrawl, timesD, timesW) before parsing; don’t reset counts inside the transaction loop (your code sets timesD = 0 then +1 each time, which prevents counting multiple deposits).
  • Avoid while(!inFile.eof()) — prefer while(getline(...)) or while(inFile >> accountno) plus getline for the rest of the line.
  • If lookups are frequent, replace the linear scan with an unordered_map keyed by account number for O(1) access.
  • Validate tokens (e.g., token length, conversion success) if the file may be malformed.

This approach keeps each line self-contained and prevents transactions from leaking into the next account.

You would want to read the line into an array, and split it at the spaces, so you have little blocks of data. Obviously the first array value (position 0) will be "123", so you can just leave that one, but the next bit would be "d52.55". You would want to read the first character of that string to determine a deposit or withdrawal, and put the value of "52.55" into a variable of type float.

Here's part of a program I made a while ago (The test string there was from someone else's project that I helped with a while back). It uses tokens with stringstream to split strings into arrays. Have a look through and see if you can adapt it to be put in your code.

#include <string>
#include <sstream>
#include <iostream>
using namespace std;
string SplitVerts(string data)
{
	string in_data=data;
	string array_data[20];
	stringstream split(in_data);
	string token = ",";
	int i = 0;
	while (split>>token){
		array_data[i] = token;
		i++;
	}
	int j = 0;
	while (j < 20){cout<<array_data[j]<<endl; j++;}
}

int Main(){
           cout<<"105:1:CME,20100601,07:34:22.796,GRC,GE,201009,FUT,XGGRC,0G4LHZ013,14ijpol1vsu7l7,Fill,0000D9DB,B,00000,99.155,2,99.155,20100601,07:27:34"<<endl;                                                                                                                              
}

Other than that, do a few google searches and see what you can turn out.
Hope this helps :) (If so, please upvote :P )

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.