0

问题陈述:读取客户支票账户信息的 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;
}

示例文本文件如下所示。

占士邦

01/01/12 200010

2012 年 3 月 30 日 -40000

2012 年 4 月 30 日 -40000

2012 年 5 月 30 日 -40000

2012 年 6 月 30 日 -40000

2012 年 7 月 30 日 -40000

我已将日期和金额转换为字符串,并尝试将其转换为浮点数和双精度数,然后将它们相加。我曾想过分开金额,但我也无法做到。我还尝试将数量设为 char 类型。

任何能引导我走上正确道路的建议都将不胜感激。

4

1 回答 1

0

虽然它不会尝试成为您程序的完整版本,但这里有一些代码来读取提供的文本文件,使用第一项(名称之后)作为初始余额,其余作为存款(如果他们' re positive)或提款(如果它们是负数),报告每笔交易的金额,以及应用后的余额。

#include <iostream>
#include <string>
#include <algorithm>

struct transaction {
    std::string date;
    double value;

    friend std::istream &operator>>(std::istream &is, transaction &t) {
        is >> t.date >> t.value;
        return is;
    }
    operator double() { return value; }
};

int main(){
    double value;
    transaction t;

    std::string name;
    std::getline(std::cin, name);

    std::cin >> t;
    value = t;

    while (std::cin >> t) {
        std::cout << "Transaction: " << t.value;
        value += t;
        std::cout << "\tBalance: " << value << "\n";
    }

    std::cout << value;
}
于 2013-10-30T05:59:15.727 回答