【发布时间】:2018-07-18 20:45:13
【问题描述】:
我目前正在学习 c++ 并尝试制作自动售货机!我知道我的代码真的很糟糕,对此我很抱歉。 我正在尝试实施一家银行并让用户从中获得贷款,唯一的问题是银行无法向用户充值。这是我的代码。
void Bank::askLoan() {
//ColaMachine object
ColaMachine cola;
bool loanGranted = false;
cout << string(100, '\n');
cout << "You do not have enough money!\n\n";
cout << "Would you like to take a loan?\n\n(1)-Yes\n(2)-No\n\n\n";
int choice;
cin >> choice;
switch (choice) {
case 1:
//Print the bank menu!
printBank();
while (loanGranted != true) {
cout << "Enter the amount to lend: $";
cin >> _loanValue;
//Test if _loanValue is less than or = to bankmoney, so they would scam the bank.
if (_loanValue <= _bankMoney) {
//Supposed to add money to the user.
cola.addMoney(_loanValue);
break;
}
else {
cout << "You entered too much! Try again..." << endl;
}
}
break;
case 2:
//User does not want to take a loan! Quit the game!
//Not implemented, yet.
break;
default:
cout << "Bad input! Please retry..." << endl;
}
}
如果输入的金额在正确的范围内,它会调用 ColaMachine 类的 addMoney() Func。
void ColaMachine::addMoney(int money) {
//This part doesnt seem to modify the actual value
//Whenever It goes back to the main game loop it doesnt change.
_money += money;
}
据我了解 += 与 _money = _money + money 相同;
我在这里做错了什么? GitHub上的完整源代码- https://github.com/Rohukas/-LearningCPP
【问题讨论】:
-
似乎没有修改这是什么意思?你怎么知道的?
-
From what I understand += is the same as _money = _money + money;正确。但听起来你应该学习使用调试器。 -
阅读您最喜欢的 C++ 书籍中的局部变量,并思考
cola是什么。 (养成将用户交互与数据处理分开的习惯会很有帮助。) -
顺便说一句:银行贷款和可乐机到底有什么关系?