从文件中减去,输出计算(c++)
假设我有一个文本文件,其中包含值1000,用户输入他们想要从1000中减去多少,可以任意多次。有没有办法输出实际的计算结果从文件中减去,输出计算(c++),c++,file,C++,File,假设我有一个文本文件,其中包含值1000,用户输入他们想要从1000中减去多少,可以任意多次。有没有办法输出实际的计算结果 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 numb
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;
int amount = 0;
do {
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;
cout << "" << endl;
cin >> selection;
int amount = 0;
switch(selection)
{
case 1:
cout << "You have choosen to transfer an amount" << endl;
cout << "How much do you wish to transfer from the shop account?"<<endl;
cout << "transfer funds" << endl;
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;
cout << "" << endl;
cout << "" << endl;
break;
case 3:
cout << "The account names is:" << endl;
cout << "The account number is:" << endl;
std::cout << "The current account balance is " << read_balance() << std::endl;
break;
case 4:
return 0;
break;
default:
cout << "Ooops, invalid selection!" << endl;
break;
}
}while(selection != 4);
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();
}
好的,基本上说,用户把它放在40,然后只有1000-40或类似的影响。1000是银行账户中的钱,用户插入要提取的金额,我想输出实际的交易是有意义的。这是我的代码。这是菜单上的第二个选项,我一直坚持着。我想输出最近的五笔交易