Answer the question
In order to leave comments, you need to log in
Why does it print "terminate called after throwing an instance of 'errors'" when entering a non-number?
I'm trying to make it so that when entering a non-number, the program asks the user to enter a number again and so on until he enters a number. But it turned out that when you enter not a number, an inscription is displayed
terminate called after throwing an instance of 'errors'
. #include <iostream>
#include "sz.h"
using namespace std;
int main(){
system("chcp 65001 && cls");
printf("введи число:");
int n = sz::in_is_int();
cout << n << endl;
return 0;
}
#include <iostream>
using namespace std;
enum errors{
not_int = 1 // не число
};
namespace sz{
int in_is_int(){
int res = 0;
try{
cin >> res;
if (cin.fail()){
cin.clear();
cin.ignore(32767,'\n');
throw not_int;
}
}
catch (int e){
printf("Це не число. Спробуй ще раз\n");
return in_is_int();
}
return res;
}
}
Answer the question
In order to leave comments, you need to log in
You are not catching an exception, try this
catch (errors){
printf("Це не число. Спробуй ще раз\n");
return in_is_int();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question