Clearing The Screen

This is a discussion on Clearing The Screen within the C++ Programming forums, part of the General Programming Boards category; I have written code for a game --- when the users say they want to play again, i want the ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    Question Clearing The Screen

    I have written code for a game --- when the users say they want to play again, i want the screen to clear -- any help wold be appreciated..thanx in adv

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    How have you coded it....is it for a char based console (In which case go to lightatdawn's board faqs.....is it for DirectX, OpenGL, GDI or what???

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    Question Clearing The Screen

    It is for char here is the code:

    //This program will play the Rock Paper Scissors Game

    #include <iostream.h>
    #include <ctype.h>

    int main()
    {
    char user_one, user_two, response = 'Y';

    do
    {
    cout << "Welcome To Rock, Paper, Scissors.\n";
    cout << "A Fun Game For All Ages !!!!\n\n\n";
    cout << " The game is simple, just enter P for Paper, R for Rock, and S for Scissors\n\n\n";


    cout << "Player 1, Please enter R, P, or S: ";
    cin >> user_one;

    user_one = toupper(user_one);

    cout << "Player 2, Please enter R, P, or S: ";
    cin >> user_two;

    user_two = toupper(user_two);


    if (user_one == 'P' && user_two == 'P')
    {
    cout << "Nobody Wins, Sorry\n\n\n\n";
    }

    else if (user_one == 'P' && user_two == 'R')
    {
    cout << "The Winner is: Player 1, Paper wraps Rock\n\n\n\n";
    }

    else if (user_one == 'P' && user_two == 'S')
    {
    cout << "The Winner is: Player 2, Scissors cut Paper\n\n\n\n";
    }

    else if (user_one == 'R' && user_two == 'R')
    {
    cout << "Nobody Wins, Sorry\n\n\n\n";
    }

    else if (user_one == 'R' && user_two == 'P')
    {
    cout << "The Winner is: Player 2, Paper wraps Rock\n\n\n\n";
    }

    else if (user_one == 'R' && user_two == 'S')
    {
    cout << "The Winner is: Player 1, Rock breaks Scissors\n\n\n\n";
    }

    else if (user_one == 'S' && user_two == 'S')
    {
    cout << "Nobody Wins, Sorry\n\n\n\n";
    }

    else if (user_one == 'S' && user_two == 'R')
    {
    cout << "The Winner is: Player 2, Rock breaks Scissors\n\n\n\n";
    }

    else if (user_one == 'S' && user_two == 'P')
    {
    cout << "The Winner is: Player 1, Scissors cut Paper\n\n\n\n";
    }




    cout << "Would you like to play another game (y/n): ";
    cin >> response;

    response = toupper(response);
    cin.clear();


    }while (response == 'Y');

    return 0;
    }

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Try here

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    Cool Clearing The Screen

    Thank you....

  6. #6
    Registered User Robert_Ingleby's Avatar
    Join Date
    Oct 2001
    Posts
    57

    Clearing the Screen

    clrscr();

    If you put this line anywhere you want the screen to clear in your code making sure to remember the #include <conio.h> library file, the screen will clear.

    It is used in my compiler which is Borland Turbo C++ V3.1

    Hope this helps.

    Rob.

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    Clearing The Screen

    One other question and my program qill be complete.....Suppose I wanted to have it test to see if the user put in something other than the R P or S ---- I've tried a few things but it keeps giving me errors..any ideas on that one..??? Thanks again to the replyersof this request

  8. #8
    Registered User Robert_Ingleby's Avatar
    Join Date
    Oct 2001
    Posts
    57
    mmmmmmmmm, well I personally would have used a case, switch, break style of code for this program.

    Im not knocking it though, your code is good, Im sure there is a way you can do this using your existing work, but take a look at switch, case, and break in the help files of your compiler, it may lead you to the answer.

    Good Luck,

    Rob.

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Or a much easier way;

    Code:
    cin.get(user_one);  
    if(user_one != 'P' &&
       user_one != 'R' &&
       user_one != 'S'){
    	cout << "Illegal move\n";
    
    	return 0;
    }

  10. #10
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    Thumbs up Clearing The Screen

    Thanks Fordy...

  11. #11
    Unregistered
    Guest

    Re: Clearing the Screen

    Originally posted by Robert_Ingleby
    clrscr();

    If you put this line anywhere you want the screen to clear in your code making sure to remember the #include <conio.h> library file, the screen will clear.

    It is used in my compiler which is Borland Turbo C++ V3.1

    Hope this helps.

    Rob.
    This funtion is not supported in Microsoft visual C++.

  12. #12
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    Clearing The Screen

    Ive added the conio.h lib and the clrscr(); still doesnt seem to want to work....ive put in several places in the code (???)



    Also::::a huge thanks goes out to Fordy... your code worked...

  13. #13
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    CLearing The Screen Revision

    Since MS C++ doesnt support that lib file...are there any other suggestions to clearing the screen..???

  14. #14
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    >>Since MS C++ doesnt support that lib file...are there any other suggestions to clearing the screen..???

    WINDOWS OPTION: (Credit: Sunlight)

    Just call the function clrscr(). Simple as that.
    #include <windows.h>

    void clrscr()
    {
    COORD coordScreen = { 0, 0 };
    DWORD cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD dwConSize;
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    GetConsoleScreenBufferInfo(hConsole, &csbi);
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
    SetConsoleCursorPosition(hConsole, coordScreen);
    }
    Its in the faqs that I linked you to earlier

  15. #15
    Registered User
    Join Date
    Nov 2001
    Posts
    4

    what about this

    #include <windows.h>

    system("cls");

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Feedback: Functional Specification Wording
    By Ragsdale85 in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2006, 04:56 PM
  2. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  3. clearing the screen
    By ssharish in forum C Programming
    Replies: 2
    Last Post: 02-01-2005, 09:00 PM
  4. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 03:41 AM
  5. Clearing the screen
    By Shadow in forum C Programming
    Replies: 4
    Last Post: 05-23-2002, 02:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21