需要帮助来读取文件并整齐显示. [英] need help to read from a file and display neatly..
本文介绍了需要帮助来读取文件并整齐显示.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我紧急在执行此任务时需要帮助.我有一个完美的代码..但是当我要求程序从文本文件中读取并显示银行帐户详细信息时,它没有不能整齐地显示.我希望它以显示其写入文本文件的方式显示..请帮助我..谢谢!
检查代码的最后一位..
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");
}
推荐答案
"<< endl; cin>> accountOne.acc_Balance; 同时(accountOne.acc_Balance< = 0 )// 检查余额是否为负值 { cout<< " << endl; 返回 0 ; } cout<< "
"<<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; 同时(accountTwo.acc_Balance< 0) { cout<< " << endl; 返回 0 ; } cout<< " << endl; cin>> accountOne.ann_InterestRate; 同时(accountOne.ann_InterestRate< 0 || accountOne.ann_InterestRate> 0. 15 ) { cout<< " << endl; cout<< " << endl; cin>> accountOne.ann_InterestRate; } cout<< " << endl; cin>> accountTwo.ann_InterestRate; 同时(accountTwo.ann_InterestRate< 0 || accountTwo.ann_InterestRate> 0. 15 ) { cout<< " << endl; cout<< " << endl; cin>> accountTwo.ann_InterestRate; } // 提示输入要保留的现金的期限 cout<< " << endl; cin> term; 同时(term< 1 || term> 10) { cout<< " << endl; cout<< " ; cin> term; } // 提示用户输入自动存款金额 cout<< "
"<<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; // 提示用户提款金额 cout<< "
"<<endl; cin>>depositAmount; //prompt the user for a withdrawal amount cout<<"Enter a withdrawal amount per month:
这篇关于需要帮助来读取文件并整齐显示.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文