您好我正在处理简单的工资申请。以下代码包含使用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();
}
答案 0 :(得分:3)
与其他人一样,您没有在正确的范围(功能级别范围,在本例中)中声明int balance
。
事实上,您似乎完全忘记了致电read_balance
,因此您在balance
上的计算使用了不确定值,即Undefined Behaviour。
接下来:声明你的变量在哪里被使用,这样你就可以在决定将代码片段提取到子函数时防止这种情况,并且更容易看到变量的使用位置。因此,更容易看出代码是否正确。
下一篇:错误处理。如果你没有,你的程序就毫无价值。事实上,即使在解决上述问题时,
只需输入无效输入,就会运行一个循环,只是从帐户余额中扣除不确定值并将错误的值写入磁盘。那可能不是你想要的。
使shop-account.txt
只读是足以愚弄程序
这是一个清理版本,可以进行最少量的检查,并添加一个选项来“检查帐户余额”。看似有用。
我希望其中一些有帮助。首先,我希望你的老师提到大部分内容:/
int read_balance(void);
void write_balance(int balance);
#include <iostream>
#include <limits>
int main()
{
while(std::cin.good())
{
std::cout << "0. Request balance" << std::endl;
std::cout << "1. Transfer an amount" << std::endl;
std::cout << "2. Quit" << std::endl;
std::cout << "Please enter menu number" << std::endl;
int selection = 2;
if(std::cin >> selection)
{
std::cout << "DEBUG: selection:" << selection << "\n";
switch(selection)
{
case 0:
std::cout << "The current account balance is " << read_balance() << std::endl;
break;
case 1:
{
std::cout << "You have choosen to transfer an amount" << std::endl;
std::cout << "How much do you wish to transfer from the shop account?"<<std::endl;
int amount = 0;
if (std::cin >> amount)
{
std::cout << "DEBUG: 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;
}
} else
{
// bad input cleared outside the switch
}
}
break;
case 2:
return 0;
break;
default:
std::cout << "Ooops, invalid selection!" << std::endl;
break;
}
}
if(std::cin.eof())
{
std::cout << "Bye\n";
return 0;
}
if(std::cin.fail())
{
std::cin.clear();
std::cin.ignore(99999, '\n');
std::cout << "Invalid input\n";
// note eof() can be true here
}
}
}
#include <fstream>
int read_balance(void)
{
std::ifstream f;
f.exceptions(std::ios::failbit | std::ios::badbit);
f.open("shop-account.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-account.txt");
f << balance;
f.close();
}
答案 1 :(得分:1)
你的功能没有平衡变量声明!!你必须在你的函数中添加一个平衡声明
int read_balance(void)
{
fstream f;
f.open("shop-account.txt");
int balance;
f >> balance; //now it's defined
f.close();
return balance;
}