I have a countdown timer for a game i'm making. There is a section that asks the player (using getline
) if they want to dodge a shot from an enemy except that they only have 3 seconds to decide otherwise the shot will hit them. The problem is that if the player does decide to dodge the shot then whenever I use another getline
after that, there will be the number 1 already typed. Is there any way I can clear the number 1 and make it so there is nothing already typed?
This shows the output with a pre-typed number 1
#include <string>
#include <iostream>
#include <thread>
#include <Windows.h>
using namespace std;
string input;
void attack()
{
getline(cin, input);
}
int main()
{
srand(time(NULL));
int randomIfDodged = ((rand() % 4) + 1);
ULARGE_INTEGER initialTime;
ULARGE_INTEGER currentTime;
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
initialTime.LowPart = ft.dwLowDateTime;
initialTime.HighPart = ft.dwHighDateTime;
LONGLONG countdownStartTime = 30000000;
LONGLONG displayedNumber = 4;
while (true)
{
GetSystemTimeAsFileTime(&ft);
currentTime.LowPart = ft.dwLowDateTime;
currentTime.HighPart = ft.dwHighDateTime;
bool dodge = false;
SHORT key = GetKeyState('1');
if (key & 0x8000)
{
dodge = true;
}
LONGLONG elapsedTime = currentTime.QuadPart - initialTime.QuadPart;
LONGLONG currentNumber_100ns = countdownStartTime - elapsedTime;
if (currentNumber_100ns <= 0)
{
system("cls");
cout << "You took too long, the guard's shot hit you." << endl;
break;
}
if (dodge)
{
system("cls");
if (randomIfDodged <= 2)
{
cout << "You dodged the guard's shot!" << endl;
cout << "What would you like to do now?" << endl;
attack();
exit(0);
}
else
{
cout << "You failed to dodge the guard's shot" << endl;
attack();
exit(0);
}
}
LONGLONG currentNumber_s = currentNumber_100ns / 10000000 + 1;
if (currentNumber_s != displayedNumber)
{
system("cls");
cout << "The guard shoots at you." << endl;
cout << "'1' To dodge the guards shot" << endl;
cout << "You have " << currentNumber_s << " seconds to dodge the guard's shot" << endl;
displayedNumber = currentNumber_s;
}
}
system("pause");
}