问题陈述:读取客户的支票账户信息的C ++程序计算其账户余额。应开发基于菜单的应用程序以迭代执行以下功能,直到用户请求退出程序为止:1.显示,2。存款,3。提款,4。退出。
到目前为止,这就是我所拥有的。
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
//Main function
int main()
{
//Identify type variables
int x=0;
float amountinput,famount,sum;
string firstname, lastname,date,filename,input,ID,amount;
fstream file;
//Program loop
while(true)
{
//Main menu loop
do
{
cout<<"Enter the number of the option you would like carried out.\n";
cout<<"1. Summary\n";
cout<<"2. Deposit\n";
cout<<"3. Withdraw\n";
cout<<"4. Quit\n";
cin>>x;
}
while(!x);
//When Quit is input, break
if(x==4)
{
break;
}
//Summary display
if (x==1)
{
cout<<"You have selected option number 1. Summary.\n"<<endl;
cout<<"Enter your Cust_ID: ";
cin>>ID;
file.open("C:\\Users\\Raggulddon\\Desktop\\C++ supplement
\\Cust_"+ID+".dat", ios::in|ios::out|ios::app);
//IF not found error.
if(!file)
{
cout<<"Sorry your account could not be found\n";
}
//Else display all content.
else
{
cout<<endl<<file.rdbuf()<<"\n";
file.close();
}
}
//Deposit
else if(x==2)
{
cout<<"You have selected option number 2. Deposit.\n";
cout<<"Please enter you account ID: ";
cin>>ID;
file.open("C:\\Users\\Raggulddon\\Desktop\\C++ supplement
\\Cust_"+ID+".dat", ios::in|ios::out|ios::app);
if(!file)
{
cout<<"Sorry the requested account could not be located.\n";
}
else
{
file>>firstname>>lastname;
cout<<endl<<firstname<<" "<<lastname<<endl;
while(!file.eof())
{
file>>date>>amount;
//
%%%%%%%%%%%%%%%%%%%//This is mainly where I am missing the lines of code..
//
float atof(string& amount);
cout<<date<<"\t\t"<<amount<<endl;
}
cin.get();cin.get();
cout<<"How much would you like to deposit today.";
cin>>amountinput;
cout<<endl;
file.close();
}
}
else if(x==3);
else if(x==4);
}
cin.get();cin.get(); //or system("PAUSE");
return 0;
}
示例文本文件看起来像这样,但是具有不同的行数。
James Bond
01/01/12 200010
03/30/12 -40000
04/30/12 -40000
05/30/12 -40000
06/30/12 -40000
07/30/12 -40000
我设置了数量=字符串,然后我希望每个字符串彼此独立(以便我可以添加它们),然后将它们转换为float或double。
基本上是这样的:
fstream file;
do
file>>date>>amount++; //I know this doesn't work, what will..
while(!file.eof());
float deposit;
finalAmount= deposit+amount;
C ++输入运算符的好处>>
是它可以接受不同类型的参数,例如a double
:
std::string date;
double amount;
file >> date >> amount;
但是,我建议使用std::getline
和std::istringstream
解析它:
std::string line;
std::getline(file, line);
std::istringstream iss(line)
iss >> date >> amount;
在有些相关的注释上,请勿使用while (!file.eof())
。在eof
实际输入操作失败之前,将不会设置该标志,因此,除非您在循环内检查错误,否则将尝试一次读取多次。
相反,例如
while (std::getline(file, line))
{
...
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句