1
do
{
cout << "Enter the numerator and denominator of the first fraction: ";
cin >> a >> b;
cout << endl;
cout << "Enter the numerator and denominator of the second fraction: ";
cin >> c >> d;
cout << endl;
} while (!validNum(a, b, c, d));
...
bool validNum(int num1, int num2, int num3, int num4)
{
if (cin.fail() || num2 == 0 || num4 == 0)
{
if (num2 == 0 || num4 == 0)
{
cout << "Invalid Denominator. Cannot divide by 0" << endl;
cout << "try again: " << endl;
return false;
}
else
{
cout << "Did not enter a proper number" << endl;
cout << "try again: " << endl;
return false;
}
}
else
return true;
}
내가하려는 것은 분모가 0이 아니며 숫자 만 입력해야합니다. 제로 코드에 의한 나누기는 잘 작동하지만 char 값을 입력하면 무한 루프가 발생하고 이유는 알 수 없습니다. 어떤 아이디어? 당신이 (즉 char
) 유효하지 않은 값을 입력하면무효 루프에서 int 입력이 유효하지 않음
굉장 완벽 작동합니다. 호기심에서 numeric_limits :: max()가 어떻게 작동하고 무엇을 하는가? –
@JuanSierra ['numeric_limits :: max'] (http://en.cppreference.com/w/cpp/types/numeric_limits/max)는 주어진 유형에 대해 가장 큰 값을 반환합니다. – 0x499602D2