Lonely
Lonely

Reputation: 11

sleep or time delay for C++ Program

So I'm writing this spinoff game of Paperboy as a class project. If I wanted to I could say I'm finished and turn it in but I want it to have a professional touch to it. My game consists of different modes: easy and hard. However I have not implemented the hard mode yet.

Anyway, here is my code

void easyMode() {

    string playerName;
    int numNewspapers, numDelivered = 0, numMissed = 0, score = 0;

    cout << "Enter Your Player Name: ";
    cin >> playerName;

    cout << "\nEnter How Many Newspapers That Need To Be Delivered: ";
    cin >> numNewspapers;

    cout << "\n\nYou have " << numNewspapers << " newspapers to deliver!\n\n";
    cout << "Time To Deliver !!\n\n";
    cout << "*===================================*\n\n";

    //cout << string(50, '\n');

    while (numDelivered < numNewspapers) {

        int outcome = RandomNumberEasy();

        cout << "*===================================*\n\n";
        cout << "Delivering Newspaper...\n\n";


        //           Game Sequence
        //*===================================*

        //      Delivered Successfully
        //*===================================*
        if (outcome <= 3 || outcome > 7) {

            cout << "You Successfully Delivered The Newspaper.\n\n";

            numDelivered++;

            score = score + 15;

            cout << "Your score is " << score << " points!\n\n";
        }

        //          Delivery Failed
        //*===================================*
        else {

            cout << "The Neighbor's Dog Chased You. Delivered Paper Didn't Quite Land On Step\n\n";

            numDelivered++;

            numMissed++;

            score = score + 5;

            cout << "Your score is " << score << " points!\n";
        }

        cout << "\n";
        sleep(1);
    }

        //              END GAME
        //*===================================*
        if (numDelivered == numNewspapers) {

            int SuccDeliver = numDelivered - numMissed;

            cout << "*===================================*\n\n";
            cout << "Congratulations, " << playerName << "!\n\n"; 
            cout << "Your Final Score Is: " << score; 
            cout << "\n\nYou missed " << numMissed << " Newspapers And Delivered " << SuccDeliver << " Newspapers\n\n";

        }

}

As you can see I do have the sleep function in there, but when I run my program, it waits a long time and the outputs every iteration all at once. I want it to wait in between each iteration but I can't seem to get it to work.

Any help is appreciated!

Upvotes: 1

Views: 86

Answers (1)

Alejandro De Cicco
Alejandro De Cicco

Reputation: 1236

The problem seems to be that the output buffer is not being flushed. A way to do it is to use cout << endl instead of cout << "\n". Mainly, this part:

cout << "\n";
sleep(1);

Should be like this:

cout << endl;
sleep(1);

And that should fix it!

Upvotes: 1

Related Questions