Compare commits
5 Commits
a6524954d2
...
7cc78abab6
Author | SHA1 | Date |
---|---|---|
Emojigit | 7cc78abab6 | |
Emojigit | 009eab6195 | |
Emojigit | 2f48026b5e | |
Emojigit | aaa75156d8 | |
Emojigit | 750f183366 |
|
@ -0,0 +1,32 @@
|
|||
# Prerequisites
|
||||
*.d
|
||||
|
||||
# Compiled Object files
|
||||
*.slo
|
||||
*.lo
|
||||
*.o
|
||||
*.obj
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Compiled Dynamic libraries
|
||||
*.so
|
||||
*.dylib
|
||||
*.dll
|
||||
|
||||
# Fortran module files
|
||||
*.mod
|
||||
*.smod
|
||||
|
||||
# Compiled Static libraries
|
||||
*.lai
|
||||
*.la
|
||||
*.a
|
||||
*.lib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
20
main.cpp
20
main.cpp
|
@ -3,10 +3,13 @@
|
|||
#include <cstdlib>
|
||||
#include <signal.h>
|
||||
#include <ctime>
|
||||
#include <sys/time.h>
|
||||
using namespace std;
|
||||
|
||||
int random_num(int lo,int hi) { // contributed by a_k_n, https://www.cplusplus.com/forum/general/107917/
|
||||
srand(time(NULL));
|
||||
struct timeval time_now{};
|
||||
gettimeofday(&time_now, nullptr);
|
||||
srand(time_now.tv_usec);
|
||||
return rand()%(hi-lo) + lo;
|
||||
}
|
||||
|
||||
|
@ -23,11 +26,22 @@ int main() {
|
|||
cout << "Guess a 2 digit number!" << endl;
|
||||
signal(SIGINT, sigint_handler);
|
||||
while(1) {
|
||||
cout << guess_attempt++ << " guess attempt: ";
|
||||
cout << guess_attempt+1 << " guess attempt: ";
|
||||
int x;
|
||||
cin >> x;
|
||||
++guess_attempt;
|
||||
if ( x == rand_guess_int ) {
|
||||
if (cin.fail()) {
|
||||
cout << "INVALID NUMBER, NOT AN INT!" << endl;
|
||||
std::cin.clear();
|
||||
std::cin.ignore(256,'\n');
|
||||
--guess_attempt;
|
||||
} else if ( x < 10 ) {
|
||||
cout << "INVALID NUMBER! numbers should bigger then 9" << endl;
|
||||
--guess_attempt;
|
||||
} else if ( x > 99 ) {
|
||||
cout << "INVALID NUMBER! numbers should smaller then 100" << endl;
|
||||
--guess_attempt;
|
||||
} else if ( x == rand_guess_int ) {
|
||||
cout << "YAY! The secret number is " << rand_guess_int << ", you success within " << guess_attempt << " attempts!" << endl;
|
||||
return 0;
|
||||
} else if ( x < rand_guess_int ) {
|
||||
|
|
Loading…
Reference in New Issue