问题陈述:读取客户支票账户信息的 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
我设置了数量 = 字符串,然后我希望每个字符串彼此独立(以便我可以添加它们)并将它们转换为浮点数或双精度数,无论哪个。
基本上,类似:
fstream file;
do
file>>date>>amount++; //I know this doesn't work, what will..
while(!file.eof());
float deposit;
finalAmount= deposit+amount;