Abdulhayee Baig
Abdulhayee Baig

Reputation: 1

How to add delay to my execution of program

I want to make a simple program, but I want to add some delay() before the program clears the screen.

I have tried the sleep() and delay() functions, but they don't work!

Can anyone help out?

#include <iostream>
#include <string> 
#include <cstring>
#include <fstream>

using namespace std;

class div
{
private:
    string mon= "IICT lab at 8:30AM, PF lab at 10:10AM, PF lecture at 1:50PM";
    string tue= "Maths at 8:30AM, Physics lab at 10:10AM";
    string wed= "English at 8:30AM, IICT lecture at 12:10PM";
    string thurs= "Physics lecture at 8:30AM, English at 10:10AM, PAkSTD at 1:50PM";
    string fri= "Maths at 8:30AM, PF lecture at 10:10AM, QT at 2:50PM";
public:

    timetable(int choice)
    {
        switch(choice)
        {
            case 1:
                cout<<mon<<endl;
                break;
            case 2:
                cout<<tue<<endl;
                break;
            case 3:
                cout<<wed<<endl;
                break;
            case 4:
                cout<<thurs<<endl;
                break;
            case 5:
                cout<<fri<<endl;
                break;
        }
    }

    mainwin()
    {
        cout<<"-------------------------------------------------Time Table-----------------------------------------------"<<endl;
        cout<<"||                                           1.Monday                                                   ||"<<endl;
        cout<<"||                                           2.Tuesday                                                  ||"<<endl;
        cout<<"||                                           3.Wednesday                                                ||"<<endl;
        cout<<"||                                           4.Thursday                                                 ||"<<endl;
        cout<<"||                                           5.Friday                                                   ||"<<endl;
        cout<<"||                                           0.EXIT                                                     || "<<endl;
        cout<<"=========================================================================================================="<<endl;
    }
};

int main()
{
    div t;
    int choice1;
    do{
        t.mainwin();
        cout<< "Enter Your Choice: ";
        cin>>choice1;
        t.timetable(choice1);
        system("cls");
    }while(choice1 != 0);
}

This doesn't work. I use the windows.h header file but it doesn't work and gives an error that a ; is missing before t.

Upvotes: 0

Views: 484

Answers (1)

Boris Hu
Boris Hu

Reputation: 200

#include <thread>
#include <chrono>

#include <iostream>
#include <string>
#include <cstring>
#include <fstream>

using namespace std::chrono_literals;
using namespace std;

class div
{
private:
    string mon= "IICT lab at 8:30AM, PF lab at 10:10AM, PF lecture at 1:50PM";
    string tue= "Maths at 8:30AM, Physics lab at 10:10AM";
    string wed= "English at 8:30AM, IICT lecture at 12:10PM";
    string thurs= "Physics lecture at 8:30AM, English at 10:10AM, PAkSTD at 1:50PM";
    string fri= "Maths at 8:30AM, PF lecture at 10:10AM, QT at 2:50PM";
public:
    void timetable(int choice)
    {
        switch(choice)
        {
            case 1:
                cout<<mon<<endl;
                break;
            case 2:
                cout<<tue<<endl;
                break;
            case 3:
                cout<<wed<<endl;
                break;
            case 4:
                cout<<thurs<<endl;
                break;
            case 5:
                cout<<fri<<endl;
                break;
        }
    }
    void mainwin()
    {
        cout<<"-------------------------------------------------Time Table-----------------------------------------------"<<endl;
        cout<<"||                                           1.Monday                                                   ||"<<endl;
        cout<<"||                                           2.Tuesday                                                  ||"<<endl;
        cout<<"||                                           3.Wednesday                                                ||"<<endl;
        cout<<"||                                           4.Thursday                                                 ||"<<endl;
        cout<<"||                                           5.Friday                                                   ||"<<endl;
        cout<<"||                                           0.EXIT                                                     || "<<endl;
        cout<<"=========================================================================================================="<<endl;
    }
};

int main()
{
class div t;
int choice1;
do{
    t.mainwin();
    cout<< "Enter Your Choice: ";
    cin>>choice1;
    t.timetable(choice1);
    std::this_thread::sleep_for(2000ms);
} while(choice1 != 0);

}

Upvotes: 1

Related Questions