Hey guys i need help with my program. What I need to do is to open to inout files. one containing the accout numbers with there names and balance. the second is the transactin history with accout num ie 123 d45.67 d 56.78 w 78.99. My problem is that i get the files to open and the first read into an array of structs but i dont really now how to read the d and w. I was fooling with peek but dosnt work. i know my code might not look the best im still new at this.

int fillArrayT( PersonAcct customerArray[7], ifstream& inFileT)
{
	int i=0;
	int j = 0;
	double tempdeposit = 0;
	double tempwithdrawl = 0;
	char ch;
	int temp_acct_num;
	
inFileT >> temp_acct_num;
	while ( !inFileT.eof())
	{
		
		 /* cout << " tempdeposit" << tempdeposit << endl; // testing purpose
		cout <<" temp_acct_num" << temp_acct_num << endl;// testing purpose
	cout << " costumer deposit: " << customerArray[i].deposit <<" costumer withdrawl : " << customerArray[i].withdrawl << endl;// testing purpose
	i++; // testing purpose */
		for ( j = 0 ; j<6; j++)
		{ 
			if (temp_acct_num == customerArray[j].acct_num)
			{
				do
				{
					ch = inFileT.peek();

					if (ch == 'd' || ch == 'D')
					{
						customerArray[j].deposit = 0;
						inFileT >> tempdeposit ;
							customerArray[j].deposit += tempdeposit;
					}
					else if ( ch =='w' || ch == 'W')
					{
						customerArray[j].withdrawl = 0;
						inFileT >> tempwithdrawl;
							customerArray[j].withdrawl -=	tempwithdrawl;
					}
				}while(ch== 'd' || ch == 'w');
					

					break;
			}
		}
	}
	return i;
}

Dani AI

Generated

Quick diagnosis: the run output (only the last deposit/withdrawal kept and large garbage numbers) points to three mistakes working together. In 's code the accumulators are reset inside the transaction switch (so earlier values get thrown away), the inner >> loop consumes tokens across lines (so the next account number becomes a malformed transaction), and some struct fields are never initialized (producing the -9.25596e+061 / -858993460 junk). Also avoid while(!inFile.eof()) — it causes subtle read problems.

A robust fix is line-based parsing. Read each transaction line with getline, create a stringstream for that line, extract the account number once, then parse the remainder of that stream for tokens. That confines parsing to a single account line and prevents the parser from eating the next account number. This is the same idea suggested but phrased as a checklist: get the line, pull the account id, then loop extracting either char + double pairs or tokens like d45.10 and handle them.

Initialization and accumulation rules: set deposit, withdrawl, timesD, timesW to zero when accounts are loaded (constructor or explicit initialization). Do not assign zero inside the per-transaction case; use deposit += amount and withdrawl -= amount and ++timesD/++timesW. Use double for money and a constant for array size (or better, use std::map<int,Account> for lookup to avoid repeated linear scans).

Edge cases and debugging: handle malformed tokens (the sample line with a trailing w has no value) by checking extraction success; if s >> value fails, parse the token string and validate token.size() > 1 before std::stod(token.substr(1)), or log and skip the bad token. Add temporary per-line debug prints (acct#, tokens parsed) to confirm parsing is correct before aggregation.

Recommended Answers

All 4 Replies

Please copy and paste the actual contents of the first few entries in the transction file so we can see what it really looks like. If there is a space between d or w and the value then reading the file should be easy and straight forward

char action;
float value;
int accountno;
string line;
while( getline(in,line) )
{
   stringstream s;
   s << line;
   s >> accountno;
   while(s >> action >> value)
   {
     switch(action)
     {
       case 'd': // deposit
         break;
       case 'w': // withdrawal
         break;
     }
   }
}

123 d45.10 d50.45 d198.56 w45.67
345 w34.00 d4.56 w45.13 d23.23 w23.12
639 d1000.34 d1234.56 w34.33 w

THATS THE FIRST 3 LINES

Ok -- the code I posted will work with that file. This is the test I ran on it

#include <string>
#include <sstream>
#include <iostream>

using namespace std;

int main()
{

    string line = "123 d45.10 d50.45 d198.56 w45.67";
    char action;
    float value;
    int acctno;
    stringstream s;
    s << line;
    s >> acctno;
    while( s >> action >> value)
    {
        cout << action << " " << value << '\n';
    }
}

ok I understand now how to use the switch properly but im still haveing trouble. when i print out the values to make sure they read properly i get the lest entered value for with drawll and deposit stored there and garbage for the rest of the account nums

and this is a run of my program

Please enter the fully qualified name of
the input text file, including the path: accounts.txt


Please enter the fully qualified name of
the input text file, including the path: transactions.txt

Please enter the name of your output file: test.txt

Test1
value d45.1value d50.45value d198.56 value w45.67 value w34value d4.56 value w45
.13value d23.23 value w23.12value d1000.34value d1234.56 value w34.33 value w345
.87 value w22.13value d345.67value d123.67value d45.99value d45.99 value w34.77v
alue d66.6value d666.66value d6.66value d66.6value d6666.66 temp_acct_num123
costumer deposit: 6666.66 costumer withdrawl : -34.77
temp_acct_num123
costumer deposit: -9.25596e+061 costumer withdrawl : -9.25596e+061
temp_acct_num123
costumer deposit: -9.25596e+061 costumer withdrawl : -9.25596e+061
temp_acct_num123
costumer deposit: -9.25596e+061 costumer withdrawl : -9.25596e+061
temp_acct_num123
costumer deposit: -9.25596e+061 costumer withdrawl : -9.25596e+061
temp_acct_num123
costumer deposit: -9.25596e+061 costumer withdrawl : -9.25596e+061
Test2
123daffy34.676666.66-34.7711345goofy123.89-9.25596e+061-9.25596e+061-858993460-8
58993460639sneezy1945.76-9.25596e+061-9.25596e+061-858993460-858993460890dopey1.
23457e+007-9.25596e+061-9.25596e+061-858993460-858993460666grumpy666.66-9.25596e
+061-9.25596e+061-858993460-858993460Press any key to continue . . .

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

	char tranType;
	float amount;
	int accountno;
	int i;


	while(!inFileT.eof())
	{
		inFileT >> accountno; 
		for ( i = 0 ; i < 6 ; i++)
		{
			
			if ( accountno == customerArray[i].acct_num)
			{
				while(inFileT >> tranType >> amount)  
				{     
					switch(tranType)    

					{       
					case 'd': // deposit   
						customerArray[i].deposit = 0;
						customerArray[i].deposit += amount;
						customerArray[i].timesD = 0;
						customerArray[i].timesD +=1;
						cout << "value d" << amount ;

						break;    




					case 'w': // withdrawal    

						customerArray[i].withdrawl = 0;
						customerArray[i].withdrawl -=	amount;
						customerArray[i].timesW = 0;
						customerArray[i].timesW +=1;
						cout << " value w"<< amount;
						break;    
					}   
				}
				inFileT >> accountno; 
				
			}

		}

	}

this is is main

for ( i=0 ; i<5 ;i++)
cout << customerArray[i].acct_num << customerArray[i].name << customerArray[i].acct_bal << customerArray[i].deposit << customerArray[i].withdrawl << customerArray[i].timesD << customerArray[i].timesW;
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.