Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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..
// 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 16-Sep-12 8:08am
Edited 16-Sep-12 8:55am
v2
Comments
Wes Aday at 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 at 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 at 16-Sep-12 13:55pm
   
You say "i have a perfectly working code", so what exactly is your question about?
Alisha92 at 17-Sep-12 15:07pm
   
i meant there wasn't any bugs
Richard MacCutchan at 17-Sep-12 15:29pm
   
You still have not explained your problem.
Alisha92 at 20-Sep-12 11:47am
   
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;
 

 
i managed to look at a few examples and type the above code. 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
Richard MacCutchan at 21-Sep-12 12:52pm
   
You need to either read each line of the file and print it exactly as you read it, or read each field from the file and format your output by using the formatting functions that specify field widths, and separate fields with spaces. The best way to figure this out is to write the various data items on a piece of paper and count how many characters wide each field needs to be and how many spaces between fields.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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. Smile | :)
 
Regards
Tushar Srivastava Smile | :)
  Permalink  
Comments
Alisha92 at 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?
Tushar Sriv at 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 at 20-Sep-12 9:28am
   
Thanks aloot! :)
Alisha92 at 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
Tushar Sriv at 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 :-)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
 
#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. Smile | :)
Regards
Tushar Srivastava Smile | :)
  Permalink  
Comments
Richard MacCutchan at 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.
Tushar Sriv at 21-Sep-12 11:22am
   
Please Explain....
Tushar Sriv at 21-Sep-12 11:28am
   
Ok... got your point, but what about windows console screen?
Richard MacCutchan at 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.
Tushar Sriv at 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 :-)
Richard MacCutchan at 21-Sep-12 12:47pm
   
Sorry, what program are you talking about? You already offered the OP a solution.
Tushar Sriv at 21-Sep-12 12:49pm
   
I am talking about creating a horizontal scrollbar in dos text mode ... :-) Please it's a request
Richard MacCutchan at 21-Sep-12 13:02pm
   
I don't know what you mean by DOS Text mode - DOS disappeared years ago. If you mean running a program in a command prompt window, you can adjust the size by modifying the properties of the window. Just click on the icon in the top left corner of the window and select "Properties".
Tushar Sriv at 21-Sep-12 13:04pm
   
Thank you for this quick tip :-) I really didn't knew about this one :-) Thanks a lot :-)

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

  Print Answers RSS
0 Kornfeld Eliyahu Peter 265
1 Sergey Alexandrovich Kryukov 202
2 George Jonsson 185
3 King Fisher 185
4 CPallini 155
0 OriginalGriff 6,223
1 DamithSL 4,724
2 Maciej Los 4,196
3 Kornfeld Eliyahu Peter 3,894
4 Sergey Alexandrovich Kryukov 3,496


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 21 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100