1

我正在尝试创建一个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;
        }
    }
}
4

4 回答 4

5

条件说明

也改变你的循环条件:

while ((timeLeft <= cd) && (guessWord.compare(originalWord) != 0))

如果其中任何一个条件失败,你就会爆发。这是因为&&如果任一条件失败,则结果为假。因此,当a)您还有时间并且b)您还没有猜到这个词时,这将继续进行。你的使用||将使循环继续,而其中任何一个为真(因此两者都必须失败才能退出循环)。

Note:我个人认为额外的大括号()很好听,可以使表达式更易于阅读。对于没有经验的程序员,他们不需要按优先顺序猜测(查找)

剩余时间计算:

time_t start    = time(0);  // Make the start time absolute and outside the loop.
                            // Note: Use one line per variable declaration and initialize.
int    timeLeft = cd;       // timeLeft is a relative value that can be negative => `int`

// Notice I adjust timeLeft because of new definition.
while ((timeLeft > 0) && (guessWord.compare(originalWord) != 0))
{
    time_t end       = time(0);
    time_t timeTaken = end-start;        // Total time taken so Far.
    timeLeft         = cd - timeTaken;   // Time left is thus.
于 2013-07-15T15:07:22.257 回答
0

正如@JoshGreifer 所说,您需要先更改timeLeft != cdtimeLeft > cd(或>=,尽管我会选择前者)。

此外,在解决您的错误之前,我可以看到您的程序存在一个问题,即如果用户花费很长时间并且diff > cd, 那么timeLeft将是负面的。它仍然允许用户进行另一个猜测,因为if(guessWord.compare(originalWord) != 0)条件仍然满足。

在您的 while 循环中,您guessWord.compare(originalWord) == 0在进行猜测之前进行检查(但这很好,因为它是空的)。您还在检查timeLeft != cd之前timeLeft是否已初始化。

我看不到您在哪里初始化originalWord,请发布相关代码。是全局变量吗?如果是这样,这可能是您的错误的根源。

于 2013-07-15T15:34:21.083 回答
0

您的开始时间和结束时间都是时间(0),这可能是问题所在。

    start = time(0);
    ..
    end = time(0); 
于 2013-07-15T15:10:34.897 回答
0

嘿,伙计们,在对其进行了一些更改后,它现在可以工作了,所以请看看我的答案,并告诉我是否还有什么需要更改的逻辑或我编写程序的方式。所以我可以在未来以正确的方式学习编写程序。

以下是新代码:

void startGame(time_t countDown,int gl){
//bool succeeded = false;
string guessWord;
time_t start, end, timeTaken;
int timeLeft = countDown;

cout << "Scrambled word is " << randomizeWord(gl) << endl;
start = time(0);//start the time once word is given
//timeLeft refers to the amt of time input by user
cout << "You have " << timeLeft << " seconds to guess." << endl;
cout << "Enter guess : ";
cin >> guessWord;
//the program stopped running after changing '||' to '&&' operator
//so meaning the condition is wrong
//so i changed it back to '||'
while ((timeLeft > 0) || (guessWord.compare(originalWord) == 0))
{
    end = time(0);//takes in the time at the moment user inputs
    timeTaken = end - start;//calculate the amt of time taken by user to input
    timeLeft = countDown - timeTaken;//calculate the amt of time left from the user input of time

    //check whether input by user is the same as the original word from the program
    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 "<< timeTaken <<" seconds." << endl;            
        break;
    }
}

}

于 2013-07-15T15:43:14.833 回答