vendredi 28 août 2020

try and except, but in c++

Suppose I want to compute the binary value of a series of numbers entered by a user but I don't want to set a limit to how many numbers the user can enter, so I do this in Python 3:

try:
    lst = list()
    while True:
        n = int(input("Enter the number: "))
        b = binary_of(n)                      // Function to find binary value
        lst.append(b)
except:
    print("The respective binary values are", '\n')
    print(lst)

In above code, the 'try' stops executing when the user enters a non-integer value and the program moves to 'except' to print the list and this is exactly what I want.

Can I do something similar in C++? Is there a 'try and except' version of C++ too??

Aucun commentaire:

Enregistrer un commentaire