I am quite new to c++ and learning it following a tutorial. But when it comes to debug, the debugger says ctime is unsafe. It says : C4996 'ctime': This function or variable may be unsafe. Consider using ctime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. what does this mean? The same code works perfect in codeblock 17.12. How can I fix it in order to work in visual studio 2017?
#include<iostream>
#include<string>
#include<ctime>
#include<stdexcept>
using namespace std;
class FactorialException :public exception {
private: int n;
char *dt;
public:
FactorialException(int n) {
this->n = n;
time_t now = time(0);
dt = ctime(&now);
}
const char * what() const throw () {
return "Unable to calculate factorial of negative number";
}
int getValue() {
return this->n;
}
char* getTime() {
return dt;
}
};
int factorial(int n) {
int fact = 1;
if (n < 0) {
/*string strError = "Unable to calculate factorial or negative number";
throw strError;*/
FactorialException ex(n);
throw ex;
}
for (int i = 1; i < n; ++i) {
fact *= i;
}
return fact;
}
int main() {
int n;
bool error = false;
do {
try {
error = false;
cout << "Enter a Number: ";
cin >> n;
cout << factorial(n) << endl;
}
catch (FactorialException& ex) {
cout << ex.what() << ", n = " << ex.getValue() << ", at time: " << ex.getTime() << endl;
error = true;
}
} while(error);
return 0;
}