DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 12 of 12

Thread: How Do I Clear a Screen?

  1. #1
    Jeffrey Guest

    How Do I Clear a Screen?


    I am just a beginner to the C++ language, and am currently writing programs
    that just run at the DOS prompt. I haven't moved on to writing Window applications
    yet...

    Anyway, with applications that run in MS-DOS, I know how to get input from
    the user, and send output onto the screen. Problem is this, I want to be
    able to send output to the screen, wait for a user response, then clear the
    screen and send a whole new set of output. The problem is that I don't know
    what command (if there is one) that clears a screen? I mean, in MS-DOS,
    if you type CLS at the prompt, that clears the screen. But how would I clear
    the screen in C++ language?

    For example, here's some code:

    #include <iostream.h>
    #include <iomanip.h>

    int main() {
    char userInput[2];

    cout << "Here is the first screen of data.\n"
    << "Type Y and press Enter to move on." << endl;
    cin >> setw(2) >> userInput;

    if (userInput[1] == "Y" || userInput[1] == "y") {
    // I want to clear the screen here.
    cout << "Here is the second screen of data." << endl;
    }

    return 0;
    }

  2. #2
    john b Guest

    Re: How Do I Clear a Screen?


    if you are using something like turbo c++ (I am), you need to include conio.h
    with your headers and to clear the screen you use the function call clrscr().
    Note that this may not be portable with some other compilers or operating
    systems, so it is probably a good idea to check your compilers documentation.
    8-)

  3. #3
    Frank Guest

    Re: How Do I Clear a Screen?


    Use the clrscr() function with the conio.h header file



    "Jeffrey" <kaiser@webspan.net> wrote:
    >
    >I am just a beginner to the C++ language, and am currently writing programs
    >that just run at the DOS prompt. I haven't moved on to writing Window applications
    >yet...
    >
    >Anyway, with applications that run in MS-DOS, I know how to get input from
    >the user, and send output onto the screen. Problem is this, I want to be
    >able to send output to the screen, wait for a user response, then clear

    the
    >screen and send a whole new set of output. The problem is that I don't

    know
    >what command (if there is one) that clears a screen? I mean, in MS-DOS,
    >if you type CLS at the prompt, that clears the screen. But how would I

    clear
    >the screen in C++ language?
    >
    >For example, here's some code:
    >

    #include <conio.h>
    >#include <iostream.h>
    >#include <iomanip.h>
    >
    >int main() {
    > char userInput[2];
    >
    > cout << "Here is the first screen of data.\n"
    > << "Type Y and press Enter to move on." << endl;
    > cin >> setw(2) >> userInput;

    clrscr(); //clears the screen
    >
    > if (userInput[1] == "Y" || userInput[1] == "y") {
    > // I want to clear the screen here.
    > cout << "Here is the second screen of data." << endl;
    > }
    >
    > return 0;
    >}



  4. #4
    nomorsht Guest

    Re: How Do I Clear a Screen?



    Have you tried WINDEX glass cleaner?
    All kidding aside, you can use one of the samples below.

    Borland C++

    #include <conio.h>
    void main()
    {
    clrscr()
    }


    Visual C++

    #include <stdlib.h>

    void main()
    {
    system("cls");
    }

    See yah!
    nomorsht
    "Jeffrey" <kaiser@webspan.net> wrote:
    >
    >I am just a beginner to the C++ language, and am currently writing programs
    >that just run at the DOS prompt. I haven't moved on to writing Window applications
    >yet...
    >
    >Anyway, with applications that run in MS-DOS, I know how to get input from
    >the user, and send output onto the screen. Problem is this, I want to be
    >able to send output to the screen, wait for a user response, then clear

    the
    >screen and send a whole new set of output. The problem is that I don't

    know
    >what command (if there is one) that clears a screen? I mean, in MS-DOS,
    >if you type CLS at the prompt, that clears the screen. But how would I

    clear
    >the screen in C++ language?
    >
    >For example, here's some code:
    >
    >#include <iostream.h>
    >#include <iomanip.h>
    >
    >int main() {
    > char userInput[2];
    >
    > cout << "Here is the first screen of data.\n"
    > << "Type Y and press Enter to move on." << endl;
    > cin >> setw(2) >> userInput;
    >
    > if (userInput[1] == "Y" || userInput[1] == "y") {
    > // I want to clear the screen here.
    > cout << "Here is the second screen of data." << endl;
    > }
    >
    > return 0;
    >}



  5. #5
    anand Guest

    Re: How Do I Clear a Screen?


    what's the problem, just put the clrscr() function inside the if loop, I think
    this is self-evident. was that really your problem?.

    Regards
    Anand

    >>
    >>I am just a beginner to the C++ language, and am currently writing programs
    >>that just run at the DOS prompt. I haven't moved on to writing Window

    applications
    >>yet...
    >>
    >>Anyway, with applications that run in MS-DOS, I know how to get input from
    >>the user, and send output onto the screen. Problem is this, I want to

    be
    >>able to send output to the screen, wait for a user response, then clear

    >the
    >>screen and send a whole new set of output. The problem is that I don't

    >know
    >>what command (if there is one) that clears a screen? I mean, in MS-DOS,
    >>if you type CLS at the prompt, that clears the screen. But how would I

    >clear
    >>the screen in C++ language?
    >>
    >>For example, here's some code:
    >>

    >#include <conio.h>
    >>#include <iostream.h>
    >>#include <iomanip.h>
    >>
    >>int main() {
    >> char userInput[2];
    >>
    >> cout << "Here is the first screen of data.\n"
    >> << "Type Y and press Enter to move on." << endl;
    >> cin >> setw(2) >> userInput;

    > clrscr(); //clears the screen
    >>
    >> if (userInput[1] == "Y" || userInput[1] == "y") {
    >> // I want to clear the screen here.
    >> cout << "Here is the second screen of data." << endl;
    >> }
    >>
    >> return 0;
    >>}

    >



  6. #6
    jDGFdjdfg Guest

    Re: How Do I Clear a Screen?


    #include <cstdlib>

    main()
    {
    std::system("CLS");
    }

    anything you can do in DOS you can do with system,
    but system() fails alot, so you can also use this

    #include <windows.h>
    #include <cstdio>

    void goto_xy(int, int);

    const HANDLE hConOut = GetStdHandle(STD_OUTPUT_HANDLE);

    main()
    {
    getData(myVar, myVar2);

    for(int i = 0; i < 26; i++)
    puts("\n");
    goto_xy(0, 0);

    }

    void goto_xy(int x, int y)
    {
    COORD screen_pos;

    screen_pos.X = x;
    screen_pos.Y = y;

    SetConsoleCursorPosition(hConOut, screen_pos);

    }




    >"anand " <anand_hiyer@usa.net> wrote:
    >
    >what's the problem, just put the clrscr() function inside the if loop, I

    think
    >this is self-evident. was that really your problem?.
    >
    >Regards
    >Anand
    >
    >>>
    >>>I am just a beginner to the C++ language, and am currently writing programs
    >>>that just run at the DOS prompt. I haven't moved on to writing Window

    >applications
    >>>yet...
    >>>
    >>>Anyway, with applications that run in MS-DOS, I know how to get input

    from
    >>>the user, and send output onto the screen. Problem is this, I want to

    >be
    >>>able to send output to the screen, wait for a user response, then clear

    >>the
    >>>screen and send a whole new set of output. The problem is that I don't

    >>know
    >>>what command (if there is one) that clears a screen? I mean, in MS-DOS,
    >>>if you type CLS at the prompt, that clears the screen. But how would

    I
    >>clear
    >>>the screen in C++ language?
    >>>
    >>>For example, here's some code:
    >>>

    >>#include <conio.h>
    >>>#include <iostream.h>
    >>>#include <iomanip.h>
    >>>
    >>>int main() {
    >>> char userInput[2];
    >>>
    >>> cout << "Here is the first screen of data.\n"
    >>> << "Type Y and press Enter to move on." << endl;
    >>> cin >> setw(2) >> userInput;

    >> clrscr(); //clears the screen
    >>>
    >>> if (userInput[1] == "Y" || userInput[1] == "y") {
    >>> // I want to clear the screen here.
    >>> cout << "Here is the second screen of data." << endl;
    >>> }
    >>>
    >>> return 0;
    >>>}

    >>

    >



  7. #7
    cal Guest

    Re: How Do I Clear a Screen?


    The visual c++ version works, however i get a O on the next line. How can
    I get rid of that mark?

    cal

    "nomorsht" <nomorsht@hotmail.com> wrote:
    >
    >
    > Have you tried WINDEX glass cleaner?
    >All kidding aside, you can use one of the samples below.
    >
    >Borland C++
    >
    >#include <conio.h>
    >void main()
    >{
    > clrscr()
    >}
    >
    >
    >Visual C++
    >
    >#include <stdlib.h>
    >
    >void main()
    >{
    > system("cls");
    >}
    >
    >See yah!
    >nomorsht
    >"Jeffrey" <kaiser@webspan.net> wrote:
    >>
    >>I am just a beginner to the C++ language, and am currently writing programs
    >>that just run at the DOS prompt. I haven't moved on to writing Window

    applications
    >>yet...
    >>
    >>Anyway, with applications that run in MS-DOS, I know how to get input from
    >>the user, and send output onto the screen. Problem is this, I want to

    be
    >>able to send output to the screen, wait for a user response, then clear

    >the
    >>screen and send a whole new set of output. The problem is that I don't

    >know
    >>what command (if there is one) that clears a screen? I mean, in MS-DOS,
    >>if you type CLS at the prompt, that clears the screen. But how would I

    >clear
    >>the screen in C++ language?
    >>
    >>For example, here's some code:
    >>
    >>#include <iostream.h>
    >>#include <iomanip.h>
    >>
    >>int main() {
    >> char userInput[2];
    >>
    >> cout << "Here is the first screen of data.\n"
    >> << "Type Y and press Enter to move on." << endl;
    >> cin >> setw(2) >> userInput;
    >>
    >> if (userInput[1] == "Y" || userInput[1] == "y") {
    >> // I want to clear the screen here.
    >> cout << "Here is the second screen of data." << endl;
    >> }
    >>
    >> return 0;
    >>}

    >



  8. #8
    nomorsht Guest

    Re: How Do I Clear a Screen?


    "cal" <sticks2@earthlink.net> wrote:
    >
    >The visual c++ version works, however i get a O on the next line. How can
    >I get rid of that mark?
    >
    >cal
    >



    Please post your code so we can help you further.

    nomorsht

  9. #9
    cal Guest

    Re: How Do I Clear a Screen?



    ok here it is----if you run it on the line under the dollar amount you will
    see a 0

    thanks for the help

    cal


    #include <iostream.h>
    #include <math.h>
    #include <iomanip.h>
    #include <ctype.h>
    #include <stdlib.h>

    void main()
    {

    // this portion of the program will initialize all of the countries currencies

    float usDollars = (float) 0.0;
    float austDollars = (float) 0.0;
    float frenFrancs = (float) 0.0;
    float nethGuilder = (float) 0.0;
    float koreanWon = (float) 0.0;
    char currency = ' ';

    // this portion will display what the program is about and will also display

    // the conversion factors that will be used for each country
    // the next portion also defines the country by the letter

    // the while statement is for a program loop that will continue until a
    "X" is entered.

    while (currency != 'X')
    {
    cout << "Enter the currency you want to convert to:" << endl << endl;
    cout << "Enter --A-- for Australian Dollars" << endl << endl;
    cout << "Enter --D-- for Danish Kroner" << endl << endl;
    cout << "Enter --F-- for French Francs" << endl << endl;
    cout << "Enter --N-- for Netherland Guilders" << endl << endl;
    cout << "Enter --S-- for South Korean Won" << endl << endl;
    cout << "Enter --X-- to end program" <<endl << endl;
    cout << "Enter letter choice now and press ENTER: " << endl;
    cin >> currency;
    currency = toupper (currency);

    if (currency == 'X') // ****if currency = x the program will end
    cout << "The program will end now! Thanks for shopping at K-Mart " << endl;

    else
    {

    cout << "Enter the US dollar amount you want to convert: "<< endl
    << endl;

    cout << setprecision(2) << setiosflags(ios::fixed | ios::showpoint);

    switch (currency)
    {
    case 'A':
    cin >> usDollars;
    cout << "The amount in Australian Dollars is = " << (usDollars) *
    1.7483 << endl << system("cls") << endl;
    break;

    case 'D':
    cin >> usDollars;
    cout << "The amount in Danish Kroner is = " << (usDollars) * 8.3500
    << endl << system("cls") << '\n';
    break;

    case 'F':
    cin >> usDollars;
    cout << "The amount in French Francs is = " << (usDollars) * 7.3505
    << endl << system("cls") << '\n';
    break;

    case 'N':
    cin >> usDollars;
    cout << "The amount in Netherland Guilders is = " << (usDollars) *
    2.4694 << endl << system("cls") << '\n';
    break;

    case 'S':
    cin >> usDollars;
    cout << "The amount in South Korean Won is = " << (usDollars) * 1108.6000
    << endl << system("cls") << '\n';
    break;

    default: cout << "Please enter a valid currency letter!" << endl <<
    system("cls") << '\n';
    } //end switch


    }//end if

    }//end while
    }//end of main function

  10. #10
    nomorsht Guest

    Re: How Do I Clear a Screen?


    Cal,
    The problem is that you are trying to use the function system() with the
    cout << operator.
    The line "cout << "The amount in Australian Dollars is = " << (usDollars)
    *
    1.7483 << endl << system("cls") << endl;"
    should be...
    cout << "The amount in Australian Dollars is = "
    << (usDollars) * 1.7483
    << endl;
    system("cls");
    cout << endl;

    Can you see the problem now?

    nomorsht


  11. #11
    nomorsht Guest

    Re: How Do I Clear a Screen?



    I ran the your program after the earlier post and I notice that the screen
    cleared before you could see the answer.
    Here is the correct solution to your problem.

    system("cls");
    cout << "The amount in Australian Dollars is = "
    << (usDollars) * 1.7483
    << endl;
    cout << endl;

    It should work..
    nomorsht


  12. #12
    cal Guest

    Re: How Do I Clear a Screen?



    Thanks ---it does work!!!!


    cal








Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center