Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
i urgently need help with this assignment im doing..i have a perfectly working code.. but when i ask the program to read from text file and display the bank account details, it doesn't display neatly. i want it to display just the way its written into the text file.. please help me..Thanks!
Check the last bit of the code..
C++
// interbanking.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <string>
#include<fstream>
#include<iomanip>
using namespace std;
struct BankAccount
{
       int acc_Num;
       double acc_Balance;//starting balance
       double final_Balance;
       string cust_Name;
       int pin;
       double interest_Earned;
       double ann_InterestRate;

};

int main()
{

    BankAccount accountOne, accountTwo;
    string username;
    int adminPin;
    string reply1;
	int select,term, year, month,tries,tries1;
	double depositAmount, withdrawAmount;


	cout<< "------------**Interbanking Application**-------------" << endl << endl;

	cout << "-------Registerd Users Only--------" << endl << endl;
	cout <<  "Enter username" << endl;
	cin>> username;
	 for(tries = 0;tries<2;tries++)
    {
          if(username == "safra" || username== "SAFRA" || username=="Safra")
          {
                      break;
          }
          else{
	        cout<<"Invalid username. note: you have a total of two tries."<<endl<<"Please re-enter the username:";
	        cin>>username;

         }


    }
    cout << "Enter four digit PIN" << endl;
	cin>> adminPin;
	for(tries1 = 0;tries1<2;tries1++)
	     {
          if(adminPin == 1234)
          {
                break;}
          else{
	        cout<<"Invalid Admin PIN. note: you have a total of two tries."<<endl<<"Please re-enter the PIN:";
	        cin>>adminPin;
         }

    }


		cout << "-----Create new account-----"<< endl;
	    cout << "Enter bank account number of Account One: "<<endl;
	    cin >> accountOne.acc_Num;
	    while(accountOne.acc_Num<1000 || accountOne.acc_Num>9999)//validation
	{
		cout<<"Invalid Account Number";
		return 0;
	}

	cout << "Enter bank account number of Account Two: "<<endl;
	    cin >> accountTwo.acc_Num;
	    while(accountTwo.acc_Num<1000 || accountTwo.acc_Num>9999)//validation
	{
		cout<<"Invalid Account Number";
		return 0;
	}

		while(accountOne.acc_Num==accountTwo.acc_Num)//validation
	{
		cout<<"Account numbers cannot be the same ";

		    return 0;
		}


	    cout << "Enter bank account balance of Account One: $"<<endl;
        cin >> accountOne.acc_Balance;
         while(accountOne.acc_Balance<=0) //checking the balance for negative values
	{
		cout<<"Account balance cannot be a negative!"<<endl;
		return 0;
    }

	  cout << "Enter bank account balance of Account Two: $"<<endl;
        cin >> accountTwo.acc_Balance;
        	while(accountTwo.acc_Balance<0)
        	{
		cout<<"Account balance cannot be a negative !"<<endl;
		return 0;
	}
		cout<<"Enter the annual interest rate for Account One: "<<endl;
	cin>>accountOne.ann_InterestRate;
	while(accountOne.ann_InterestRate<0 || accountOne.ann_InterestRate>0.15)
	{
		cout<<"The annual interest rate must be between 0 and 0.15"<<endl;
		cout<<"Enter interest rate again: "<<endl;
		cin>>accountOne.ann_InterestRate;
	}

	cout<<"Enter the annual interest rate for the second account: "<<endl;
	cin>>accountTwo.ann_InterestRate;
	while(accountTwo.ann_InterestRate<0 || accountTwo.ann_InterestRate>0.15)
	{
		cout<<"The annual interest rate must be between 0 and 0.15"<<endl;
		cout<<"Enter interest rate again: "<<endl;
		cin>>accountTwo.ann_InterestRate;
	}

	//prompt to enter a term for the cash to be held
	cout<<"Enter the term the accounts will be held: "<<endl;
	cin>>term;
	while(term<1 || term>10)
	{
		cout<<"The term has to be between 1 to 10 years"<<endl;
		cout<<"Re-enter the term: ";
		cin>>term;
	}

	//prompt the user for an automatic deposit amount
	cout<<"Enter a deposit amount per month: $"<<endl;
	cin>>depositAmount;

	//prompt the user for a withdrawal amount
	cout<<"Enter a withdrawal amount per month: $"<<endl;
	cin>>withdrawAmount;
	//------------------Display------------------------------
    fstream myFile;
	myFile.open("details.txt", ios::out); //open for writing
	if(myFile.good())
		cout << "All details written to file!" << endl;
	else
		cout << "Details could not be written!" << endl;

      myFile<<"Account Numbers:    "<<accountOne.acc_Num<<"                    "<<accountTwo.acc_Num<<endl;

	myFile<<"Year   Month    Start       End		 Start       End"<<endl;

	for(year=1; year<=term; year++)
	{
		for(month=1; month<=12; month++)
		{
			//calculating interest earned
			accountOne.interest_Earned = (accountOne.acc_Balance*accountOne.ann_InterestRate)*1/12;

			accountTwo.interest_Earned = (accountTwo.acc_Balance*accountTwo.ann_InterestRate)*1/12;

			accountOne.acc_Balance = accountOne.acc_Balance + accountOne.interest_Earned;

			accountTwo.acc_Balance = accountTwo.acc_Balance + accountTwo.interest_Earned;

			accountOne.final_Balance = (accountOne.acc_Balance + accountOne.interest_Earned) + depositAmount - withdrawAmount;

			accountTwo.final_Balance = accountTwo.acc_Balance + accountTwo.interest_Earned + depositAmount - withdrawAmount;

			myFile<<" "<<year<<"      "<<month<<"      "<<accountOne.acc_Balance<<"      "<<accountOne.final_Balance<<"      "
            <<accountTwo.acc_Balance<<"      "<<accountTwo.final_Balance<<endl;
		}
	}
	myFile.close();
	cout<<"Do you want to view the details? Enter Y to view."<<endl;
	cin>>reply1;
//	do
//	{
	string myLine;
	myFile.open("details.txt", ios::in); //open for reading
	if(myFile.good())
		cout << "File opened!" << endl;
	else
		cout << "File could not be opened" << endl;

	//reading data from file
	while(myFile >> myLine){
		cout << myLine;
		cout<<endl;
	}
	cout << endl;
	//close the file
	myFile.close();
//}
//while ( reply1 == "Y" || reply1 == "y");
  system("PAUSE");
	 }
Posted
Updated 16-Sep-12 7:55am
v2
Comments
[no name] 16-Sep-12 13:21pm    
You are joking right? First of all it's not at all urgent. Secondly, so? Format your output to look like whatever your file looks like. How on earth are we supposed to know what your file looks like? Or what output you want it to look like?
Alisha92 17-Sep-12 15:06pm    
i have no idea how the strike through appeared..it wasn't there when i typed it..i assumed you guys will run the code to check.. i'm sorry..i didn't know..
Richard MacCutchan 16-Sep-12 13:55pm    
You say "i have a perfectly working code", so what exactly is your question about?
Alisha92 17-Sep-12 15:07pm    
i meant there wasn't any bugs
Richard MacCutchan 17-Sep-12 15:29pm    
You still have not explained your problem.

Hi Friend,

See i am not sure what exactly do you want, but as you are saying that you want all the file data to be displayed simply same as it is in the file, I will suggest you to step down to low level file I/O.
Simply open the file and start reading each single byte (Every ASCII code is 1 byte long) and put that character on the screen using putch() (I guess this is the name). Now this will allow every single space, newline or tab to be printed exactly as same as it was in the file.

See, how simple it was.
Now it is up to you how will you be implementing it.

1. Just open the file as you do.
2. start reading the file, character by character (1 byte at a time) using a loop.
3. Print each single byte on the screen using putch() function on the screen while inside the loop.

Hope that it can help you. :-)

Regards
Tushar Srivastava :-)
 
Share this answer
 
Comments
Alisha92 17-Sep-12 15:11pm    
i have no idea how to use putch(). i googled for examples..and it turned out that there is something called putchar() is that the one you suggest i use?
Er. Tushar Srivastava 17-Sep-12 15:41pm    
Yes, that is the one.... Actually I haven't worked with C++ for long so I forget it, but yes it is the one I am talking about... :-) Thanks For Pointing it out.
Hope your Project successes
Regards
Tushar Srivastava
Alisha92 20-Sep-12 9:28am    
Thanks aloot! :)
Alisha92 20-Sep-12 11:52am    
so i went through a few examples and apparently i'm supposed to use getc() .. i replaced
string myLine;
myFile.open("details.txt", ios::in); //open for reading
if(myFile.good())
cout << "File opened!" << endl;
else
cout << "File could not be opened" << endl;

//reading data from file
while(myFile >> myLine){
cout << myLine;
cout<<endl;
}
-------------
with this code :
FILE * pFile;
int c;
char n;
pFile=fopen ("details.txt","r");
if (pFile==NULL) perror ("Error opening file");
else
{
do {
c = getc (pFile);
if (c == '1')
n++;
} while (c != EOF);
fclose (pFile);
cout<<n;
------------
but it doesn't work..any idea how to read a file that displays..


Account Numbers: 2222 3333
Year Month Start End Start End
1 1 55.0917 73.1833 44.0733 62.1467
1 2 55.1835 73.2753 44.1468 62.2202
1 3 55.2755 73.3674 44.2204 62.2939
1 4 55.3676 73.4597 44.2941 62.3678
1 5 55.4599 73.5521 44.3679 62.4417
1 6 55.5523 73.6447 44.4418 62.5158
1 7 55.6449 73.7375 44.5159 62.59
1 8 55.7376 73.8304 44.5901 62.6643
1 9 55.8305 73.9234 44.6644 62.7387
1 10 55.9236 74.0166 44.7389 62.8133
1 11 56.0168 74.11 44.8134 62.888
1 12 56.1101 74.2035 44.8881 62.9628
Er. Tushar Srivastava 21-Sep-12 8:22am    
My friend, the program that you have made here seems to me as a counting program that only counts the number of 1's in the file but nothing else. ok I will be posting a working code soon...... :-) Happy Programming :-)
Hi Friend, As i had told you, here is a general code, that can display a ASCII format Text file perfectly as same as it can be viewed in any formatted text editor like wordpad.

C++
#include<stdio.h>    // For FILE operations
#include<conio.h>    // For putchar() function
void main()
{
 FILE *pFile;
 int c;
 pFile = fopen("test.txt","r");
 if(pFile == NULL)
 {
  printf("Error opening file");
  exit(1);
 }
 while(c != EOF)
 {
  c = fgetc(pFile);
  putchar(c);
 }
 getch();
}
</conio.h></stdio.h>


See, it wasn't difficult at all, now the only thing you need to remember before getting perfectly fine display is:
Maximum Columns in a single Row must to 80, since in dos text mode, only 80 columns are there.

Hope it will help you. :-)
Regards
Tushar Srivastava :-)
 
Share this answer
 
Comments
Richard MacCutchan 21-Sep-12 11:20am    
Maximum Columns in a single Row must to 80, since in dos text mode, only 80 columns are there.This restriction disappeared last century.
Er. Tushar Srivastava 21-Sep-12 11:22am    
Please Explain....
Er. Tushar Srivastava 21-Sep-12 11:28am    
Ok... got your point, but what about windows console screen?
Richard MacCutchan 21-Sep-12 11:36am    
You can display as many characters as you like in any screen, it's what the horizontal scrollbar was invented for.
Er. Tushar Srivastava 21-Sep-12 11:39am    
Sir, Please Help me implement this in this program in DOS TEXT MODE using C++ I will be very thankful to you. It's a kind request :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900