首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从文本文件中的值减去,然后输出计算(C++)

从文本文件中的值减去,然后输出计算(C++)
EN

Stack Overflow用户
提问于 2014-01-20 00:41:06
回答 1查看 828关注 0票数 0

嗨,我正在处理一份工资申请,有四种选择,如下:

1.从商店银行帐户中减去一笔款项(该帐户是文本文件" shop ")。您不需要将相同的金额添加到另一个帐户中,但是您应该在一个单独的文件中使用时间戳记录事务。应用程序应防止帐户余额透支。

2.列出屏幕上最近的五项交易。如果还没有五笔交易,那就把它们都列出来。

3.将帐户名称、号码和当前余额打印到屏幕上。

4.退出程序。

我已经完成了1,3和4,但我完全不知道该怎么做,我希望有人能帮我解决这个问题。

代码语言:javascript
复制
int read_balance(void);
void write_balance(int balance);
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
    int selection;
    int total;
    int attempts = 0;
    string name;
    string number;
    cout << "Correct login details entered!"
         << "" << endl;
    cout << "1. Transfer an amount" << endl;
    cout << "2. List recent transactions" << endl;
    cout << "3. Display account details and current balance" << endl;
    cout << "4.Quit" << endl;
    cout << "Please enter menu number" << endl;
    cin >> selection;
    switch (selection)
    {
        case 1:
            cout << "How much do you wish to transfer?" << endl;
            int amount = 0;
            if (std::cin >> amount)
            {
                std::cout << "Transferred Amount:" << amount << "\n";
                int balance = read_balance();
                if (amount <= 0)
                {
                    std::cout << "Amount must be positive\n";
                }
                else if (balance < amount)
                {
                    std::cout << "Insufficient funds\n";
                }
                else
                {
                    int new_balance = balance - amount;
                    write_balance(new_balance);
                    std::cout << "New account balance: " << new_balance
                              << std::endl;
                    fstream infile("time.txt", ios::app);
                    std::time_t result = std::time(nullptr);
                    std::string timeresult = std::ctime(&result);
                    infile << amount << std::endl;
                    infile << timeresult << std::endl;
                }
            }
            break;
        case 2:
            cout << "Here are you're recent transactions" << endl;
            break;
        case 3:
            cout << "The account names is:" << name << endl;
            cout << "The account number is:" << number << endl;
            std::cout << "The current account balance is " << read_balance()
                      << std::endl;
            break;
        case 4:
            system("pause");
            return 0;
        default:
            cout << "Ooops, invalid selection!" << endl;
            break;
    }
    system("pause");
    return 0;
}
int read_balance(void)
{
    std::ifstream f;
    f.exceptions(std::ios::failbit | std::ios::badbit);
    f.open("shop.txt");
    int balance;
    f >> balance;
    f.close();
    return balance;
}
void write_balance(int balance)
{
    std::ofstream f;
    f.exceptions(std::ios::failbit | std::ios::badbit);
    f.open("shop.txt");
    f << balance;
    f.close();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-20 01:32:45

在你的第二个开关箱里应该这样做:

代码语言:javascript
复制
case 2:
{
    std::cout << "Here are your recent transactions" << '\n';

    for (int i = 0, size = v.size(); i < (size <= 5 ? size : 5); ++i)
    {
        std::cout << i << ". " << v.at(i) << '\n';
    }

    break;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21224575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档