这是我在 Bjarne Stroustrup 的编程原理和 C++书中为练习编写的解决方案的尝试。不幸的是,我写的从输入的硬币中给出总金额的部分没有按照我的意愿工作!
一个快速的回答,我将非常感激,但如果有人也有时间他们可以帮我介绍一些基本的错误检查吗?
我希望它工作的方式是在需要用户输入之后(例如,你有多少 20p?),检查用户是否输入了int
. 如果没有,请提供一个微妙的错误消息并有机会重复相同的问题,而不是停止程序或从头开始程序!
#include "../../std_lib_facilities.h"
int main() {
int one, ten, twenty, fifty, one_pound, two_pound;
double amount;
amount = (one * 0.01) + (ten * 0.1) + (twenty * 0.2) + (fifty * 0.5) + one_pound + (two_pound * 2);
cout << "Welcome to the change counter app!\nHow many 1p's do you have?\n";
cin >> one;
cout << "How many 10p's do you have?\n";
cin >> ten;
cout << "How many 20p's do you have?\n";
cin >> twenty;
cout << "How many 50p's do you have?\n";
cin >> fifty;
cout << "How many £1 coin's do you have?\n";
cin >> one_pound;
cout << "How many £2 coin's do you have?\n";
cin >> two_pound;
cout << "You have: " << one << " 1p coins!\n"
<< "You have: " << ten << " 2p coins!\n"
<< "You have: " << twenty << " 20p coins!\n"
<< "You have: " << fifty << " 50p coins!\n"
<< "You have: " << one_pound << " £1 coins!\n"
<< "You have: " << two_pound << " £2 coins!\n"
<< "The total amount of money you have is: " << amount << "\n";
}