Clearing the consule window in C++

Discussion in 'General Chat' started by amazingtrade, Oct 30, 2003.

  1. amazingtrade

    amazingtrade Mad Madchestoh fan

    Joined:
    Jun 19, 2003
    Messages:
    5,139
    Likes Received:
    0
    Location:
    Manchester
    I want to tidy up my UI for my program by clearing the screen to make it look more tidy on the consule mode in C++. I am using the IOSTREAM libary. Does anybody know how you clear the consule screen in C?

    I want to the CLS thing in BASIC.

    Thanks for any help, I have tried google and books but found nothing.
     
    amazingtrade, Oct 30, 2003
    #1
  2. amazingtrade

    penance Arrogant Cock

    Joined:
    Jun 30, 2003
    Messages:
    6,004
    Likes Received:
    2
    Location:
    Bristol - armpit of the west.
    what compiler are you useing?

    if it supports conion.h (console input output)

    Code:
    #include <conio.h>
    
    int main()
    {
    
               printf("arse hats");
               getchar();
               clrscr();
    
               getchar();
               return;
    }
    
    ish

    clears the console screen and returns the cursor to top left.
    the library also contains gotoxy, usefull for makeing a tidy UI


    conio.h is not ANSI C so be careful if portability is an issue
     
    Last edited by a moderator: Oct 30, 2003
    penance, Oct 30, 2003
    #2
  3. amazingtrade

    michaelab desafinado

    Joined:
    Jun 19, 2003
    Messages:
    6,403
    Likes Received:
    1
    Location:
    Lisbon, Portugal
    Microsoft Visual C++ (all 32 bit editions) don't support clrscr() in its conio.h. You have to implement it yourself. See below for someone who has done it:

    http://cs.colgate.edu/APCS/C++/VC++ClearScr.htm

    btw AT it's spelled console ;)

    Michael.
     
    michaelab, Oct 30, 2003
    #3
  4. amazingtrade

    amazingtrade Mad Madchestoh fan

    Joined:
    Jun 19, 2003
    Messages:
    5,139
    Likes Received:
    0
    Location:
    Manchester
    Thanks I will have a look at that. I'm using C++ NET at uni but Bloodshed DEV C++ at home so portability is a major issue.
     
    amazingtrade, Oct 30, 2003
    #4
  5. amazingtrade

    Sgt Rock

    Joined:
    Jun 19, 2003
    Messages:
    873
    Likes Received:
    0
    Sgt Rock, Oct 30, 2003
    #5
  6. amazingtrade

    michaelab desafinado

    Joined:
    Jun 19, 2003
    Messages:
    6,403
    Likes Received:
    1
    Location:
    Lisbon, Portugal
    If you're coding for the Windows environment then the code in the link I gave you will be fine but it's strictly Windows environment.

    Michael.
     
    michaelab, Oct 30, 2003
    #6
  7. amazingtrade

    julian2002 Muper Soderator

    Joined:
    Jun 19, 2003
    Messages:
    5,094
    Likes Received:
    1
    Location:
    Bedfordshire
    why not just print lots of \n's the scroll everything off the top of the console? should port to other platforms well enough?
    int scrLines = 30;
    for (int i = 0; i < scrLines; i++) cout << "\n";
    should do it (for a 30 line console) or is my console c++ that rusty?

    cheers


    julian
     
    julian2002, Oct 30, 2003
    #7
  8. amazingtrade

    amazingtrade Mad Madchestoh fan

    Joined:
    Jun 19, 2003
    Messages:
    5,139
    Likes Received:
    0
    Location:
    Manchester
    I thought of doing something like that but not shure how that would work in a windows enviroment as it /n makes it scroll down rather than up.

    That will just add a long scrollbar to the screen and leave lots of white space I think.

    I did so somthing like that in Java in college but I was working in a DOS environment then.

    I wlll try it later though when I am on my dekstop, I am in bed atm on my ancient P233 laptop.
     
    amazingtrade, Oct 30, 2003
    #8
  9. amazingtrade

    penance Arrogant Cock

    Joined:
    Jun 30, 2003
    Messages:
    6,004
    Likes Received:
    2
    Location:
    Bristol - armpit of the west.
    Julians idea would clear the screen, but would leave the curser at the bottom i think
     
    penance, Oct 30, 2003
    #9
  10. amazingtrade

    amazingtrade Mad Madchestoh fan

    Joined:
    Jun 19, 2003
    Messages:
    5,139
    Likes Received:
    0
    Location:
    Manchester
    I try it, I am sure its pretty easy to move the postion of the cursor using x,y commands but then I might as well use Micheal's suggestion as that involves using other libaries too.

    I will have a play.
     
    amazingtrade, Oct 30, 2003
    #10
  11. amazingtrade

    Paul Ranson

    Joined:
    Sep 4, 2003
    Messages:
    1,602
    Likes Received:
    0
    Location:
    An octopus's garden.
    You could read the Windows documentation, this is immediately relevant. http://msdn.microsoft.com/library/en-us/dllproc/base/clearing_the_screen.asp

    If you're writing Windows apps I think you could find a free 'curses' implementation for Windows. This would give you Unix source compatibility but is a whole new thing to learn.

    But if all you want to do is clear the screen when your program starts/exits then the 'system ("cls");' approach rules. If you have compilation problems then just use conditional compilation to remove the call from the Bloodhound build.

    Paul
     
    Paul Ranson, Oct 31, 2003
    #11
  12. amazingtrade

    amazingtrade Mad Madchestoh fan

    Joined:
    Jun 19, 2003
    Messages:
    5,139
    Likes Received:
    0
    Location:
    Manchester
    Thanks, that method worked

    {
    system("cls");
    }

    I just need to check it works on the .NET compiler though.
     
    amazingtrade, Oct 31, 2003
    #12
  13. amazingtrade

    SteveC PrimaLuna is not cheese

    Joined:
    Aug 31, 2003
    Messages:
    854
    Likes Received:
    0
    Location:
    SE Norway
    Do you have back problems or something?

    SteveC ;)
     
    SteveC, Oct 31, 2003
    #13
  14. amazingtrade

    amazingtrade Mad Madchestoh fan

    Joined:
    Jun 19, 2003
    Messages:
    5,139
    Likes Received:
    0
    Location:
    Manchester
    Lol I was just tired and my laptop is silent unlike my noisy desktop.
     
    amazingtrade, Oct 31, 2003
    #14
  15. amazingtrade

    themadhippy seen it done it smokin it

    Joined:
    Jun 19, 2003
    Messages:
    1,118
    Likes Received:
    1
    Location:
    by the cross
    have you tried
    [​IMG]
     
    themadhippy, Oct 31, 2003
    #15
  16. amazingtrade

    amazingtrade Mad Madchestoh fan

    Joined:
    Jun 19, 2003
    Messages:
    5,139
    Likes Received:
    0
    Location:
    Manchester
    If I wasn't feeling so down (because of a couple of problems at uni and my social life crisis) I would actually find that quite funny:)
     
    amazingtrade, Oct 31, 2003
    #16
  17. amazingtrade

    Sgt Rock

    Joined:
    Jun 19, 2003
    Messages:
    873
    Likes Received:
    0
    LOL Les that's a classic :D :D
     
    Sgt Rock, Oct 31, 2003
    #17
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.