Загрузка...

Exceptions in C++. Highlight non-abundances?

Thread in C/C++ created by ICH42 Apr 12, 2022. 160 views

  1. ICH42
    ICH42 Topic starter Apr 12, 2022 Banned 0 Mar 24, 2022
    #include <iostream>

    bool division(int a, int b, int &res) {
    if (b != 0) {
    res = a / b;
    return true;
    } else {
    return false;
    }
    }

    int main() {
    int a, b;
    std::cin >> a >> b;

    int res;
    if (division(a, b, res)) {
    std::cout << res;
    } else {
    std::cerr << "b must not be equal to 0" << std::endl;
    }

    return 0;
    }
     
  2. andrey_bruter
    andrey_bruter Apr 12, 2022 Banned 37 Sep 20, 2019
    что сделать?
     
  3. vtlstolyarov
    Не очень понятно при чём тут исключения и как выглядит задача которую надо решить. Если надо просто придраться к коду, то я бы оформил как-то так (не меняю оформление ввода и выводв потому что не знаю условия задачи, но возвращаю из программы ненулевой ResultCode при делении на ноль):
    C
    #include <iostream>

    bool TryGetQuotient(int numerator, int denominator, int &qotient) {
    if (denominator != 0) {
    qotient = numerator / denominator;
    return true;
    } else {
    return false;
    }
    }

    int main()
    {
    int a, b;
    std::cin >> a >> b;

    int result;
    if (TryGetQuotient(a, b, result)) {
    std::cout << result;
    return 0;
    } else {
    std::cerr << "b must not be equal to 0" << std::endl;
    return -1;
    }
    }
     
Top
Loading...