Hi guys, I am suppose to write a program that accepts an account number, d or c standing for debit or credit, and a floating number representing the amount of the transaction, and then does 5 things.

1)Display the sum-total amount of all transactions for a particular account.

2)Display the sum-total amount of all transactions for all accounts in ascending order of account number.
3)Display the total of all debits for the day.
4)Display the total of all credits for the day.
5) Print out a list of transactions grouped by account number in ascending order

Example of input file

11232345 d 123.33
12323534 c 122.10
12312312 d 133.11
12312414 c 444.10

For example, when I try to execute my function to find the sum-total amount of all transactions for a specific account, I get the result sometimes of whatever it is going to the last line, which then gets doubled as it is either subtracted or added, depending on whether the last line is associated with d or c.

Also, I have no idea how i can make a list of accounts by eliminating the extra account numbers, and consolidating all the account numbers in one account number.

I would appreciate any help, this has been driving me nuts!

#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;

int accountNumber;
char dOrC;
char dOrCArray[1000];
int accountNum[1000];
float transactionAmount[1000];
float sumOfTransactions;
float transaction;
fstream accountInfo;
int choice;

void sumTotalAccount(int choice)
{
	float tempSumTotal = 0;
	for(int i=0; i<1000;i++)
	{
	
		char tempChar = dOrCArray[i];
		float tempTransaction = transactionAmount[i];
	
	
	
		if(accountNum[i] == choice)
		{
			if( tempChar == 'd')
			{	
				tempSumTotal -= tempTransaction;
			}
			if( tempChar == 'c')
			{
				tempSumTotal += tempTransaction;
			}
		}
	}
	cout << "The sum total of the transactions for this account is: " 
	     << tempSumTotal 
	     << "dollars." <<endl;
}

void allAccounts()
{
	bool swapped;
	do
	{
		swapped = false;
		for (int i= 0; i<999; i++)
		{
			if(accountNum[i] > accountNum[i+1])
			{
				swap(accountNum[i], accountNum[i+1]);
				swap(dOrCArray[i], dOrCArray[i+1]);
				swap(transactionAmount[i], transactionAmount[i+1]);
				swapped = true;
			}
		}
	}
	while (swapped);
	
	for(int i=0; i< 1000; i++)
	{
		if( accountNum[i] != 0)
		{	
			cout << accountNum[i] << " "
			     << dOrCArray[i] << " "
			     << transactionAmount[i]
			     <<  endl;
		}
	}
	
}

void allDebits()
{
	float temp = 0;
	for(int i = 0; i<1000;i++)
	{
		if(dOrCArray[i] == 'd')
		{
			temp += transactionAmount[i];
		}
	}
	cout << temp;
}

void allCredits()
{
	float temp =0;
	for(int i = 0; i<1000;i++)
	{
		if(dOrCArray[i] == 'c')
		{
			temp += transactionAmount[i];
		}
	}
	cout << temp;
}

void sortAccountNumbers()
{
	for(int i =0; i <1000; i++)
	{
		cout << accountNum[i] << " "
		     << dOrCArray[i] << " "
		     << transactionAmount[i]
		     << endl;
	}
} 
int main()
{	
	int count =0;
	cout << "Please enter the name of the file"<< endl;
	accountInfo.open("test.txt");
	
	if(!accountInfo)
	{
		cout << "Unable to open file";
		return 1;
	}
	while(!accountInfo.eof())
	{	
		accountInfo >> accountNumber  >> dOrC >> transaction;
	        accountNum[count] = accountNumber;
		dOrCArray[count] = dOrC;
	        transactionAmount[count] = transaction;
		count++;
	}
	accountInfo.close();
	cout << "Welcome to the End-Of-Day  BANC menu. Please enter the appropriate digit "
	     << "based upon what you would like." << endl
	     << "1) Display the sum-total amount of all transactions for a "
	     <<	"particular account." <<endl
	     << "2) Display the sum-total amount of all transactions for "
	     << "all account in ascending order of account number."
	     << endl << "3) Display the total of all debits for the day."
	     << endl << "4) Display the total of all credits for the day."
	     << endl << "5) Print out a list of transactions grouped by account "
	     << "in ascending order." << endl << "6) Quit program." << endl;
	cin >> choice;
	switch(choice)
	{
		case 1:
		cout << "Please enter the account number.";
		cin >> choice;
		sumTotalAccount(choice);
		break;
	
		case 2:
		sortAccountNumbers();
		break;
		
		case 3:
		allDebits();
		break;

		case 4:
		allCredits();
		break;
		
		case 5:
		allAccounts();
		break;
	
		default:
		exit(1);
		break;
	}
	
}

Recommended Answers

All 6 Replies

The loop at line 123 is incorrect. Should be coded like this because eof() doesn't work the way you think it does.

while(accountInfo >> accountNumber  >> dOrC >> transaction)
{
     accountNum[count] = accountNumber;		
     dOrCArray[count] = dOrC;	        
     transactionAmount[count] = transaction;		
     count++;	
}

Hm, my first function in case 1 doesnt work anyone after making that change. Have I made some kind of mistake in my logic?

don't know -- post your changes.

Alright, I got rid of my problem with the doubling problem. However, I am trying to get rid of my global variables and pass by reference. However, I have no idea even where to start. Anyone know a site that could guide me by chance? Thanks, any help is appreciated

some of those globals may not need to be passed to other functions. For example fstream accountInfo. Just declare it in the function in which it is used. You need to evaluate each global variable and determine which ones can be declared locally in the function in which it is used and which one you have to declare in main() then pass it by reference to the function(s) that use it.

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.