Answer to Question #149009 in C++ for Ali Mohammed

Question #149009
You are tasked with writing a program which will write a monthly account summary of customers' transactions at the bank at which you are employed. The monthly transactions are stored in a file called transactions.txt in the follow form:
123 d45.10 d50.45 d198.56 w45.67
1
Expert's answer
2020-12-06T10:22:55-0500
#include <iostream>
#include <stdlib.h>
#include <fstream> // this library is used to reading a text file
#include <string.h> // this library is used to calling string functions


using namespace std;
int main () {
	// declare a input file stream
	ifstream inFile;
	// open a file stream
	inFile.open("transactions.txt"); // note the .txt file must be in the .cpp file location
	
	// the proccess is checking the was file opened or wasn't
	if (!inFile) {
    	cerr << "Unable to open file datafile.txt";
    	exit(1);   // call system to stop
	}
	
	
	// we declare a string for placing the elements in .txt file
	string summary = "";
	string x;
	while (inFile >> x) {
		summary += x + " ";
	}
	
	// we initialize all informations to Summary string
	int n = 0;
	int number_of_deposits = 0;
	
	// this is convert string to char
	char cstr[summary.size() + 1];
    strcpy(cstr, summary.c_str());
    
	string account_number = "";
	for (int i = 0; i < 3; i++) {
		account_number += cstr[i];
	} 
	cout << "Account number is that : " << account_number << endl;
	
	for (int i = 0; i < summary.size() + 1; i++) {
		if (cstr[i] == 'd') {
			string deposit = "";
			for (int j = i + 1; ; j++) {
				deposit += cstr[j];
				if (cstr[j] == ' ')
					break;
			}
			n++;
			cout << n << " - deposit balance is " << deposit << endl;
			number_of_deposits++;
		}
		if (cstr[i] == 'w') {
			string withdrawn_deposit = "";
			for (int j = i + 1; j <= i + 5; j++) {
				withdrawn_deposit += cstr[j];
			}
			cout << "Amount of withdrawn is " << withdrawn_deposit << endl;
		}
	}
	cout << "The number of Deposits: " << number_of_deposits;
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog