我正在尝试创建一个while循环,当满足字符串条件或计时器条件时,程序将跳出循环并打印出所需的输出。并且在打印出所需的输出时,输出将包含比较答案前后的时间差。
但是循环并没有按照我期望的方式运行。那么谁能帮我弄清楚这段代码的问题在哪里?
下面是我的代码:
void startGame(time_t cd,int gl){
string guessWord;
time_t start, end, diff,timeLeft;
cout << "Scrambled word is " << randomizeWord(gl) << endl;
while (timeLeft != cd || guessWord.compare(originalWord) == 0)
{
start = time(0);
cout << "You have " << cd << " seconds to guess." << endl;
cout << "Enter guess : ";
cin >> guessWord;
end = time(0);
diff = end - start;
//total_time = total_time + diff;
timeLeft = cd - diff;
if(guessWord.compare(originalWord) != 0)
{
cout << "WRONG! Attempt ... You have " << timeLeft << "seconds left... Try Again" << endl;
cout << "Enter guess : ";
cin >> guessWord;
}
else
{
cout << "You are CORRECT! "<< timeLeft <<" seconds left. Your timing is "<< diff <<" seconds." << endl;
break;
}
}
}