ctime - Can't time input: C++ -
i trying make game test c++ skills, , in game created class called player method function definition attack(). prints random string based on player method variable, , asks player input string in little time possible:
//definitions player int player::attack() { std::cout << "you attacking. \n"; std::cout << "you must enter string in little time possible:"; std::string r = randstr(wordc); std::cout << r << "\n"; std::string attack; double seconds_since_start; time_t start = time(0); while (true) { std::cin >> attack; if (attack == r) {break;} seconds_since_start = difftime(time(0), start); } std::cout << "you typed word in " << seconds_since_start << "seconds\n"; }
it doesn't work, , have looked everywhere answer. returns random numbers don't make sense. when see people using difftime() function, convert tm structure time_t variable , put second argument. need use this? type of data difftime() function return? doing wrong? compiler? appreciate help.
just place time measurement in if
block before break;
, delay correctly computed. but, next try when attack != r
, have restart counter (if needed).
double seconds_since_start; time_t start = time(0); while (true) { std::cin >> attack; if (attack == r) { // stop counter , measure delay seconds_since_start = difftime(time(0), start); break; } // restart counter (if needed) start = time(0); } std::cout << "you typed word in " << seconds_since_start << "seconds\n";
Comments
Post a Comment