divisor;
std::cout << "Enter two integers: ";
input:
if (std::cin >> dividend >> divisor) {
try {
if (divisor == 0)
throw std::domain_error("Divisor cannot be zero.");
std::cout << dividend << " / " << divisor << " = " << dividend / divisor << std::endl;
} catch (std::domain_error err) {
std::cout << err.what() << "\nWant to try again? (enter y or n): ";
char c;
std::cin >> c;
if (std::cin && c == 'y')
goto input;
}
}
return 0;
}
is there a way to avoid goto?
this is what i can think of #include <iostream> #include <stdexcept> int main() { int dividend, divisor; std::cout << "Enter two integers: "; while (std::cin >> dividend >> divisor) { try { if (divisor == 0) throw std::domain_error("Divisor cannot be zero."); std::cout << dividend << " / " << divisor << " = " << dividend / divisor << std::endl; break; } catch (std::domain_error err) { std::cout << err.what() << "\nWant to try again? (enter y or n): "; char c; std::cin >> c; if (!std::cin || c != 'y') break; } } return 0; }
Обсуждают сегодня