hi ...

i am making a program which can handle bank acount system.

In which User can MakeDeposite,WithDraw money and CheckRemainingBalance.

when user MakeDeposite then a certain amount should be saved and as how much times user

MakeDeposit, it should be added in balance amount and as how many times user

WithDraw, it should also be stored and maintain

Remaining Balance (thorugh subtracting that amount)

i have written some code, please see it and correct it :

#include <iostream.h>
#include <fstream.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

int WithDraw(char filePath[], int openMode);
int MakeDeposit(int amount,char filePath[], int openMode);

int main()
{
	int amount=0;
	clrscr ();

	char filePath[] = "anyFile.txt";
	int openMode  = ios::out | ios::app;
  	cout<<"Enter Amount to deposit in your account:";
	cin>>amount;
	if (MakeDeposit(amount,filePath,openMode) != 1)
	{
		cout << "\n Program was unable to write to file. ";
	}
	openMode = ios::in;
	if (WithDraw(filePath,openMode) != 1)
	{
		cout << "\n Program was unable to read from file. ";
	}
       getch();
	return 0;
}
int MakeDeposit(int amount,char filePath[], int openMode)
{
       ofstream fout(filePath, openMode);

	if (!fout)
	{
		cout << "\n Unable to open file for writing. Now exiting...";
		return -1;
	}
	char str[25];
	itoa(amount, str, 10);

	fout.write(str, strlen(str));

      /*	int n;
	char *str = "12345.67";
	n = atoi(str);
	printf("string = %s integer = %d\n", str, n);  */


	fout.close();
	return 1;
}
int WithDraw(char filePath[], int openMode)
{
	ifstream fin(filePath, openMode);

	if (!fin)
	{
		cout << "\n Unable to open file for reading. Now exiting...";
		return -1;
	}

	char strTemp[100] = "";
    
	fin.getline(strTemp,100, '\n');
	cout << "\n WithDraw amount:" << strTemp;

	fin.close();
	return 1;
}

Recommended Answers

All 2 Replies

Your description of the problem is somewhat unclear. What does "and as how many times user" mean?

>>i have written some code, please see it and correct it :

What is wrong with it? Are you getting compiler errors? If yes, then what errors? And what compiler?

i mean to say that user can add amount multiple times, not just one time.
i am using turbo c3.0 compiler.
i hav made it. but one error is still not handled.
when i do Makedeposite more than once, it replaces the amount of file but it should be added in that amount.
.

#include <iostream.h>
#include <fstream.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

int MakeDeposit(int amount,char filePath[], int openMode);
int WithDraw(int amount,char filePath[], int openMode);
int CheckRemaingBalace(char filePath[], int openMode);
int tempStore=0;
int main()
{
	int depositedAmount=0,withDrawAmount=0,tempForAmount=0;
	clrscr ();

	char filePath[] = "f:\\anyFile.txt",temp;
      //	int openMode  = ios::out | ios::app;
      while(1)
      {
	clrscr();
	cout<<"Press 1 to Deposite amount:";
	cout<<"\nPress 2 to withdraw:";
	cout<<"\nPress 3 to Check Balance:";
	cout<<"\nPress e or q to exit the program:";
	cout<<"\nPress any key to clear:";
	cout<<"\nWrite your Optione:";
	temp = getche();
	switch(temp)
	{
		case '1':
		 cout<<"\nEnter Amount to deposit in your account:";
		 cin>>depositedAmount;
		 tempForAmount +=depositedAmount;
		 int openMode  = ios::out;
		 if (MakeDeposit(tempForAmount,filePath,openMode) != 1)
		 {
			cout << "\n Program was unable to write to file. ";
		 }
		break;
		case '2':
		cout<<"\nEnter Amount to WithDraw:\n";
		 cin>>withDrawAmount;
		 openMode  = ios::out;
		 if ( WithDraw(withDrawAmount,filePath,openMode) != 1)
		 {
			cout << "\n Program was unable to write to file. ";
		 }
		break;
		case '3':
		openMode = ios::in;
		if (CheckRemaingBalace(filePath,openMode) != 1)
		{
			cout << "\n Program was unable to read from file. ";
		}
		break;
		case 'e':
		case 'q':
		exit(0);
		break;
		default:
		cout<<"\n Option invalid!";
		break;
	}

	 getch();
      }

	return 0;
}
int MakeDeposit(int amount,char filePath[], int openMode)
{
       ofstream fout(filePath, openMode);

	if (!fout)
	{
		cout << "\n Unable to open file for writing. Now exiting...";
		return -1;
	}
	int openMode2 = ios::in;
	ifstream fin(filePath, openMode2);

	if (!fin)
	{
		cout << "\n Unable to open file for reading. Now exiting...";
		return -1;
	}

	char strTemp[100] = "";
	fin.getline(strTemp,100, '\n');

	tempStore = atoi(strTemp);
	if(strTemp=="")
	{
	   itoa(amount, strTemp, 10);
	   cout<<"\n\nSuccessfully stored !";
	   fout.write(strTemp, strlen(strTemp));
	}
	else if(strTemp!="")
	{
		tempStore+=amount;
		itoa(tempStore, strTemp, 10);
		cout<<"\n\nSuccessfully stored !";
		fout.write(strTemp, strlen(strTemp));
	}

	fin.close();
	fout.close();
	return 1;
}
int WithDraw(int amount,char filePath[], int openMode)
{
	ifstream fin(filePath, openMode);

	if (!fin)
	{
		cout << "\n Unable to open file for reading. Now exiting...";
		return -1;
	}

	char strTemp[100] = "";
	fin.getline(strTemp,100, '\n');
	int n=0;
	n = atoi(strTemp);
	n = n - amount;
	cout<<"\nSuccessfully WithDraw amount:"<<amount;
	int openMode2  = ios::out;
	if (MakeDeposit(n,filePath,openMode2) != 1)
	 {
		cout << "\n Program was unable to write to file. ";
	 }

	fin.close();
	return 1;
}
int CheckRemaingBalace(char filePath[], int openMode)
{
	ifstream fin(filePath, openMode);

	if (!fin)
	{
		cout << "\n Unable to open file for reading. Now exiting...";
		return -1;
	}

	char strTemp[100] = "";
  
	fin.getline(strTemp,100, '\n');
	cout << "\n CheckRemaingBalace amount:" << strTemp;

	fin.close();
	return 1;
}
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.