【问题标题】:subtracting from value in a text file, then outputting the calculation (C++)从文本文件中的值中减去,然后输出计算(C++)
【发布时间】:2014-01-20 00:41:06
【问题描述】:

您好,我正在处理一个工资申请,有以下四个选项:

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

2. 在屏幕上列出最近的五笔交易。如果还没有五笔交易,请列出所有交易。

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

4.退出程序。

我已经完成了 1,3 和 4,但我完全不知道如何去做 2。我希望有人能够帮助我解决这个问题。

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();
}

【问题讨论】:

  • 你的目标平台和你使用的编译器是什么?
  • 我正在使用 Microsoft Visual Studio。而且我什至不确定您所说的目标平台是什么意思,对此非常陌生
  • 您可能必须将事务存储在内存中。你在课堂上学习过容器类吗?
  • 不,我们还没有介绍。我想我现在必须研究容器类

标签: c++ file function


【解决方案1】:

这是您的第二个开关盒中应该包含的内容:

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;
}

【讨论】:

  • 当我尝试这样做时出现此错误:错误 C2360:'in' 的初始化被 'case' 标签跳过
  • @user3057816 我没有收到此错误。你介意在Coliru上给我一个例子吗?
  • 甚至不确定我是否理解这个问题。一个例子究竟是什么?抱歉,我讨厌需要为我说明的所有内容
  • 其实我犯了一个愚蠢的错误。您的代码现在正在运行,但输出只是“商店”文件中当前的余额,与实际交易相反
  • @user3057816 您将交易存储在哪里?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多