Define a function "foo"

Rearrange the code to define a function"foo", wich throws an exception with a value of "100" if its parameter is greater than 999. The "foo" catches its exceptions and prints "error" to the screen.

cout << "error!" << endl;}
try {
}
if (arg> 999) throw -100; }
catch (int x) {
void foo (int arg) {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# include <iostream>
# include <stdexcept>

void foo (int number)
{
    try
    {
        std::cout << "enter number: \n";
        std::cin >> number;
        if (number <= 999)
        {
            std::cout << "the number is: " << number << "\n";
        }
        else
        {
            throw std::out_of_range("100");
            //console prints same for number entered == 100, ...
            // ... so something additioal would be good
        }
    }
    catch (const std::exception& e)
    {
        std::cerr << e.what();
    }
}

int main()
{
    int num1{}, num2{};
    foo(num1);
    foo(num2);
}
anyone who understands the language in which ninja clark writes is welcome to reply though the post seems to be addressed to me, thanks

edit: for the record there was a non-English post above by ninja clark that has been removed
Last edited on
closed account (E0p9LyTq)
@ninja clark,

since you already know the correct answer why do you have to keep spamming it again and again.
did he really just gave a coding puzzle? xD
Topic archived. No new replies allowed.