-1

I'm trying to clear my screen in C++, but I can't compile this code...

#include<iostream>
using namespace std;
#include<conio.h>

#include<stdio.h>

class cpuschedule
{
    int n, bu[20];

    float twt, awt, wt[20], tat[20];

public:

    void Getdata();

    void fcfs();

    void sjf();

    void roundrobin();

};

//Getting no of processes and Burst time

void cpuschedule::Getdata()
{
    int i;

    cout << "Enter the no of processes : ";

    cin >> n;

    for (i = 1; i <= n; i++)
    {
        cout << "\nEnter The BurstTime for Process p"<< i << " = ";

        cin >> bu[i];
    }

}

//First come First served Algorithm

void cpuschedule::fcfs()
{
    int i, b[10];

    float sum = 0.0;

    twt = 0.0;

    for (i = 1; i <= n; i++)
    {
        b[i] = bu[i];

        cout << "\nBurst time for process p"<< i << " = ";

        cout << b[i];
    }

    wt[1] = 0;

    for (i = 2; i <= n; i++)
    {
        wt[i] = b[i - 1] + wt[i - 1];
    }

    for (i = 1; i <= n; i++)
    {
        twt = twt + wt[i];

        tat[i] = b[i] + wt[i];

        sum += tat[i];
    }

    awt = twt / n;
    sum = sum / n;

    cout << "\nTotal Waiting Time = " << twt;

    cout << "\nAverage Waiting Time = " << awt;

    cout << "\nAverage Turnaround time = " << sum;

}

//Shortest job First Algorithm

void cpuschedule::sjf()
{

    int i, j, temp, b[10];

    float sum = 0.0;

    twt = 0.0;

    for (i = 1; i <= n; i++)
    {
        b[i] = bu[i];

        cout << "\nBurst time for process p" << i << " = ";

        cout << b[i];
    }

    for (i = n; i >= 1; i-)
    {
        for (j = 2; j <= n; j++)
        {
            if (b[j - 1]>b[j])
            {
                temp = b[j - 1];

                b[j - 1] = b[j];

                b[j] = temp;
            }
        }
    }

    wt[1] = 0;

    for (i = 2; i <= n; i++)
    {
        wt[i] = b[i - 1] + wt[i - 1];
    }

    for (i = 1; i <= n; i++)
    {
        twt = twt + wt[i];

        tat[i] = b[i] + wt[i];

        sum += tat[i];
    }

    awt = twt / n;
    sum = sum / n;

    cout << "\nTotal Waiting Time = " << twt;

    cout << "\nAverage Waiting Time = " << awt;

    cout << "\nAverage turnaround time = " << sum;
}

//Round Robin Algorithm

void cpuschedule::roundrobin()
{
    int i, j, tq, k, b[10], Rrobin[10][10], count[10];

    int max = 0;

    int m;

    float sum = 0.0;

    twt = 0.0;

    for (i = 1; i <= n; i++)
    {
        b[i] = bu[i];

        cout << "\nBurst time for process p" << i << " = ";

        cout << b[i];

        if (max<b[i])

            max = b[i];

        wt[i] = 0;
    }

    cout << "\nEnter the Time Quantum = ";

    cin >> tq;

    //TO find the dimension of the Round robin array

    m = max / tq + 1;

    //initializing Round robin array

    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= m; j++)
        {
            Rrobin[i][j] = 0;
        }
    }

    //placing value in the Rrobin array

    i = 1;

    while (i <= n)
    {
        j = 1;

        while (b[i]>0)
        {
            if (b[i] >= tq)
            {
                b[i] = b[i] - tq;

                Rrobin[i][j] = tq;

                j++;
            }
            else
            {
                Rrobin[i][j] = b[i];

                b[i] = 0;

                j++;
            }
        }

        count[i] = j - 1;

        i++;
    }

    cout <<" Display";

    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= m; j++)
        {
            cout << "\nRr[" << i << ", " << j << "] = " << Rrobin[i][j];

            cout << "     ";
        }
        cout << "\ncount = " << count[i];
    }

    for (j = 1; j <= n; j++)
    {
        for (i = 1; i <= count[j]; i++)
        {
            if (i == count[j])
            {
                for (k = 1; k<j; k++)
                {
                    if (k != j)

                        wt[j] += Rrobin[k][i];
                }
            }

            else

            for (k = 1; k <= n; k++)
            {

                if (k != j)

                    wt[j] += Rrobin[k][i];
            }
        }
    }

    for (i = 1; i <= n; i++)
        cout << "\nWaiting Time for process P" << i << " = " << wt[i];

    //calculating Average Weighting Time

    for (i = 1; i <= n; i++)
    {
        twt = twt + wt[i];

        tat[i] = b[i] + wt[i];

        sum += tat[i];
    }

    awt = twt / n;
    sum = sum / n;

    cout << "\nTotal Waiting Time = " << twt;

    cout << "\nAverage Waiting Time = " << awt;

    cout << "\nAverage turnaround time = " << sum;

}

void main()
{
    int ch = 0, cho;

    cpuschedule c;

    clrscr();

    do
    {
        switch (ch)
        {

        case 0:

            cout << "\n0.MENU";

            cout << "\n1.Getting BurstTime";

            cout << "\n2.FirstComeFirstServed";

            cout << "\n3.ShortestJobFirst";

            cout << "\n4.RoundRobin";

            cout << "\n5.EXIT";

            break;

        case 1:
            c.Getdata();
            break;

        case 2:
            cout << "FIRST COME FIRST SERVED SCHEDULING";

            c.fcfs();
            break;

        case 3:
            cout << "SHORTEST JOB FIRST SCHEDULING";
            c.sjf();
            break;

        case 4:
            cout << "ROUND ROBIN SCHEDULING";
            c.roundrobin();
            break;

        case 5:
            break;

        }

        cout << "\nEnter your choice : ";

        cin >> ch;

        getch();

    } while (ch<5);
}

Here is the compiler error:

clrscr is undefined

byxor
  • 5,930
  • 4
  • 27
  • 44
Gab
  • 45
  • 2
  • 7
  • 7
    `clrscr()` isn't a standard c++ function. – πάντα ῥεῖ Dec 14 '16 at 08:11
  • What other function can I use? – Gab Dec 14 '16 at 08:19
  • 3
    There is no standard way of clearing the screen. It depends on which development environment, libraries etc you have. You need to figure something out. If all else fails, you can do lots of `printf("\n\n\n\n\n\n\n\n\n\n");` to sort of "clear" the screen. – Prof. Falken Dec 14 '16 at 08:23
  • Please read how to create a [mcve] - the code you have posted is far from minimal, and most of it is irrelevant to the problem you're describing. It's much easier to offer assistance if you present a targeted sample code. – Toby Speight Dec 14 '16 at 10:15
  • Multiple duplicates, e.g. http://stackoverflow.com/questions/21750450/undefined-refrence-to-clrscr and http://stackoverflow.com/questions/3226329/function-clrscr-in-c-and-c – Paul R Dec 14 '16 at 10:17
  • 1
    As an aside, why do you think that a program needs to clear the screen? Assuming that you have a screen, it's quite rude for a program to unilaterally overwrite the output of previous commands, that might still be relevant. And there are many situations in which you won't have a screen, depending on how your i/o is connected (e.g. to pipe or to file). – Toby Speight Dec 14 '16 at 10:18

1 Answers1

1

I coded a little example for you. I am assuming you're running on windows.

#include <iostream>
#include <string>
using namespace std;

#include <stdio.h> // include this for the clear

int main()
{
  string usersInput;
  cout <<"Enter string: ";
  getline(cin,usersInput);

  cout << "The string is: " << usersInput << endl;

  cout << "Press Enter to clear screen.";

  cin.ignore();
  system("CLS"); // this clears the screen

  cin.ignore();
  return 0;
}
asmcriminal
  • 93
  • 12
  • _Note_: On _Ux_, instead of [CLS](http://ss64.com/nt/cls.html) you could run [clear](https://linux.die.net/man/1/clear) (there are other ways or commands, but this seems the simplest one). – CristiFati Dec 14 '16 at 08:57
  • @CristiFati I never coded C++ on Unix/Linux. Do you need a header file for clear()? – asmcriminal Dec 14 '16 at 09:05
  • _clear_ is _CLS_'s counterpart: it's a shell command, so you call it similarly from _C_ / _C++_ (like you did for _CLS_: via `system`). – CristiFati Dec 14 '16 at 10:30