0

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Look at this https://stackoverflow.com/a/9747668/14755898 – AlexM28 Feb 09 '22 at 15:32
  • How about system("sleep 5") ? – U. W. Feb 09 '22 at 15:36
  • 1
    If you use `windows.h`, you should use [`Sleep`](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleep), not `sleep`. Note that C++ is case-sensitive. However, using the ISO C++ function [`std::this_thread::sleep_for`](https://en.cppreference.com/w/cpp/thread/sleep_for) is probably better in your case. – Andreas Wenzel Feb 09 '22 at 15:57

1 Answers1

1
#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);

}
Boris Hu
  • 202
  • 1
  • 6
  • Simply including `Windows.h` wont make `sleep(3);` (lowercase `s`) work, it needs to be `Sleep(3);` (uppercase `S`) instead. There is a reason why cross-platform functions like `std::this_thread::sleep_for()` exist, to hide these details. – Remy Lebeau Feb 09 '22 at 17:37
  • good point. updated the solution. – Boris Hu Feb 09 '22 at 17:44