gpt4 book ai didi

c++ - 使用 fstream 将文本文件导入变量?

转载 作者:行者123 更新时间:2023-11-28 00:25:37 25 4
gpt4 key购买 nike

我正在尝试创建一个简单的程序,它像 ATM 一样用于学习目的。到目前为止,我有一个工作程序,但现在我想“将其提升到一个新的水平”,如果您愿意,并从文本/csv 文件导入我的银行余额,然后分配给一个变量,如果对余额进行任何更改我还想将这些更改写入所述文本文件。到目前为止,我已经在网上进行了搜索,但未能找到很多似乎与我的问题特别相关的信息。

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

//calculates money withdrawal.
int main(){
double currentBalance = -250.63;
double prevBalance = currentBalance;
double withdrawal = 0;
double deposit = 0;
double deficit = currentBalance;
double updatedWithdrawal = 0; //(currentBalance - withdrawal)
double updatedDeposit = 0; //(currentBalance + deposit)
string answer = "blah";

cout << "Welcome to bank bro's banking services.\nWhat would you like to do? Withdrawal, Deposit, or View Account Status(VAS): ";
cin >> answer; // asks the user if they intend to withdrawal, or deposit.

if (answer == "VAS"){
if (currentBalance > 0)
cout << "\n Your account is in good standing." << endl;
else{
cout << "\n You currently owe: $" << deficit << endl;
}
cout << "\n Your current balance is: $" << currentBalance << endl;
}
else if (answer == "vas"){
cout << "\n Your current balance is: " << currentBalance << endl;
if (currentBalance > 0)
cout << "\nYour account is in good standing." << endl;
else{
cout << "You currently owe: $" << deficit << endl;
}
cout << "\nYour urrent balance is: $" << currentBalance << endl;
}
else if (answer == "Withdrawal"){
cout << "\nHow much would you like to take out? ";
cin >> withdrawal;
if (withdrawal > currentBalance)
cout << "\nYou don't have sufficient funds." << endl;
else{
updatedWithdrawal = (currentBalance - withdrawal);
cout << "You have $" << updatedWithdrawal << " left, cash is dispensed below." << endl;
cout << "\n\n\n Thank you, come again!" << endl;
}
}
else if (answer == "withdrawal"){
cout << "\nHow much would you like to take out? ";
cin >> withdrawal;
if (withdrawal > currentBalance)
cout << "\nYou don't have sufficient funds." << endl;
else{
updatedWithdrawal = (currentBalance - withdrawal);
cout << "\nYou have $" << updatedWithdrawal << " left, cash is dispensed below." << endl;
cout << "\n\n\n Thank you, come again!" << endl;
}
}
else if (answer == "Deposit"){
cout << "\nHow much would you like to deposit? ";
cin >> deposit;
updatedDeposit = (currentBalance + deposit);
cout << "\nYour previous balance of $" << prevBalance << " \nhas been updated and $" << deposit <<
" \nhas been added to your account, bringing the total available balance to $" << updatedDeposit << endl;
cout << "\n\nThank you come again!" << endl;
}
else if (answer == "deposit"){
cout << "\nHow much would you like to deposit? ";
cin >> deposit;
updatedDeposit = (currentBalance + deposit);
cout << "\nYour previous balance of $" << prevBalance << " \nhas been updated and $" << deposit <<
" \nhas been added to your account, bringing the total available balance to $" << updatedDeposit << endl;
cout << "\n\nThank you come again!" << endl;
}
else{
cout << "I don't recognize that command, restart and try again." << endl;
}
return 0;
}

非常感谢任何帮助! :)

最佳答案

为了在 C++ 中使用文件,您需要一个 fstream。您应该查看文档 http://en.cppreference.com/w/cpp/io/basic_fstream/basic_fstream哪里正确描述了如何在 C++ 中与文件交互。

您需要一个以写入模式打开的fstream,以便以您选择的格式保存所有数据。之后,您需要以读取模式打开一个 fstream 以便再次从中读取数据。

文件格式由您决定。您可以以任何您想要的格式序列化数据。您需要记住,在序列化过程之后,必须准确读取所有数据的写入方式。

关于c++ - 使用 fstream 将文本文件导入变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25271563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com