0

How to use 'try' and 'catch' keywords for exception handling in c++?

An example will be appreciated.

18th Aug 2017, 4:17 AM
Chitransh Vishwakarma
Chitransh Vishwakarma - avatar
5 Respostas
+ 5
tnx Oscar
18th Aug 2017, 7:57 AM
Million Gashawbeza
Million Gashawbeza - avatar
+ 2
What is the idea of saying srand(time(0))
18th Aug 2017, 5:24 AM
Million Gashawbeza
Million Gashawbeza - avatar
+ 1
#include <iostream> #include <stdlib.h> #include <time.h> using namespace std; int main() { srand(time(0)); bool areYouGay = rand()%2; try{ throw areYouGay; } catch(bool yes) { if (yes) cout << "You're gay"; else cout << "You're not gay"; } return 0; } You can see more here http://www.cplusplus.com/doc/tutorial/exceptions/ Regards!
18th Aug 2017, 4:45 AM
Oscar Albornoz
Oscar Albornoz - avatar
+ 1
You're welcome :)
21st Aug 2017, 6:55 AM
Oscar Albornoz
Oscar Albornoz - avatar
0
A random number generator requires a number(it is called seed) to generate random numbers . If the random number generator is given the same seed then every time it will generate the same sequence of random numbers . For example :- If you run the program and it is generating random sequence 2,78,45,60 . If second time you run the program you will again get the same sequence 2,78,45,60. srand function is used to change the seed of the random number generator.By setting srand(time(0)) , you are setting the seed of the random number generator to the current time.By doing this every time you run the program you will get different random sequences :- For example for the first run if you are getting 2,78,45,60 . Next time you might get 5,3,6,80 (depending on the current time,as seed has been changed since the time has changed since the last run) for more info refer these :- http://www.cplusplus.com/reference/clibrary/cstdlib/rand/ http://www.cplusplus.com/reference/clibrary/cstdlib/srand/ http://www.cplusplus.com/reference/clibrary/ctime/time/ From Stackoverflow.
18th Aug 2017, 5:32 AM
Oscar Albornoz
Oscar Albornoz - avatar