vendredi 21 septembre 2018

How do i improve my algorithms to avoid failure in c++?

I am a new beginner with programming and c++

i have a question about improving my code to handle failures in the proper way.

so i have something like this:

    double i;

while(true){
        cout << "enter number:" << "\n";
        cin >> i;
        cout << "you entered:" << i+2;
}

i do have an if statement that stops the loop if i enter the word "exit"

when cin takes a string instead of an int, it breaks of course. what do i do to get an error message and start the loop from the beginning?

this is just an example, so any general cases would be appriciated.

Aucun commentaire:

Enregistrer un commentaire