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

0

あなたの開始時間と終了時間はどちらも時間(0)であり、問​​題になる可能性があります。

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

@JoshGreiferが言ったように、最初timeLeft != cdtimeLeft > cd(または>=、私は前者に行きますが)に変更する必要があります。

さらに、あなたのバグに対処する前に、私はあなたのプログラムに問題があり、ユーザーが長い時間をかけdiff > cdtimeLeft. if(guessWord.compare(originalWord) != 0)条件がまだ満たされているため、ユーザーは別の推測を行うことができます。

while ループでは、推測が行われる前にチェックguessWord.compare(originalWord) == 0しています (ただし、null であるため問題ありません)。timeLeft != cdまた、初期化される前に確認しtimeLeftています。

どこで初期化しているのかわかりませんoriginalWord。関連するコードを投稿してください。グローバル変数ですか?その場合、これがエラーの原因である可能性があります。

于 2013-07-15T15:34:21.083 に答える
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 に答える