嗨我正在一个简单的工资申请。以下代码包含使用switch语句的两个选项菜单。该代码还链接到名为“shop-account”的文本文件。该文件仅包含值100.使用功能读取/写入文件
对于选项1,用户应该能够从文件中转移金额。用户应该能够进行尽可能多的转帐,而不用透支帐户。代码应该能够输出当前余额。我相信我假设要使用void函数,但我以前从未使用它,并且真的很挣扎。我希望有人可以看看代码,并告诉我哪里出错了。谢谢
int read_balance (void);
void write_balance (int balance);
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int selection;
int total;
int balance;
int NewAmount;
do {
cout << "1. Transfer an amount" <<endl;
cout << "2. Quit" << endl;
cout << "Please enter menu number"<<endl;
cin >> selection;
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;
cin >> NewAmount;
balance -= NewAmount;
write_balance (balance);
cout << balance << endl;
break;
case 2:
return 0;
break;
default:
cout << "Ooops, invalid selection!" << endl;
break;
}
}while(selection != 2);
system("pause");
return 0;
}
int read_balance (void)
{
fstream f;
f.open("shop-account.txt");
f >> balance; //error: "balance" is unidentified
f.close();
return balance;
}
void write_balance (int balance)
{
fstream f;
f.open("shop-account.txt");
f << balance;
f.close();
}
我希望你能看看代码,并告诉我们你认为你出错的地方。由于 –
也是唯一一块代码,我正在寻找帮助是如何从文件中实际转移一笔金额,一旦我找到了这一点,我会继续尝试,并自己做其余的 – user3057816
缺少缩进和它不是一个事实可编辑的例子帐户为“你要去哪里错”? - 在你的'read_balance'里,'balance'在哪里?没有地方。将'int balance;'添加到函数中。 – ShinTakezou