【发布时间】:2015-10-12 09:47:05
【问题描述】:
#include <iostream>
using namespace std;
void check_positive () {
cout << "Number given must be positive. Please try again." << endl;
}
int main() {
int pesos, interest, compound, year;
do {
cout << "How many pesos did you deposit? ";
cin >> pesos;
if (pesos <= 0) {
check_positive();
}
} while (pesos <= 0);
do {
cout << "What is the interest? ";
cin >> interest;
if (interest <= 0) {
check_positive();
}
} while (interest <= 0);
}
每当我运行此代码并在第一个循环期间输入“9+”作为输入时,第一个循环结束,但在第二个循环开始后立即进入无限循环。为什么会这样?
【问题讨论】:
-
因为你从不检查你的输入是否成功。
-
您预计会发生什么?尽可能具体。你认为什么代码可以处理这种情况,你认为它是如何处理的?
标签: c++