954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

I Summon Clear Screen

Sorry to be such a gimmy gimmy... but I couldn't find any threads on some code that clears the screen, just like DOS's 'CLS' and Bash's 'Clear'...I need a function that does the same thing as both CLS and Clear. Thanks for reading and or helping out.

Geek-Master
Junior Poster
158 posts since Dec 2004
Reputation Points: 12
Solved Threads: 7
 

Hmm.... 'clrscr()' from 'conio.h' should serve your purpose

jeanatkin
Newbie Poster
10 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

You don't want to do that. It's considered rude in command line programs and in all others building up your own screen will replace what's underneath it anyway (at least while your application is running).

There's no platform independent way to achieve it btw (probably because of that) so if you insist you'll have to plow through your compiler documentation to see if there's a function for it and how to call it.
You could of course always figure out the memory location of your screenbuffer and manually poke zeros into each position and hope.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

>I need a function that does the same thing as both CLS and Clear.

#include <stdlib.h>
#include "system.h"

void clear()
{
#if defined(DOS_BASE)
  system("CLS");
#elif defined(UNIX_BASE)
  system("clear");
#else
  #error Unsupported system
#endif
}

Will it work? Probably, provided you define either DOS_BASE or UNIX_BASE in system.h. Is it advisable? No, not really. But I've described why elsewhere, and it's really your decision.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Just to be sure that we are on the same page... the function should only clear the 'output' on the screen... not do anything with the actual program or command prompt. I want to create 'animation' in the command prompt, I have done this before with Batch files by echoing text and then 'cls' and echoing the text in another spot on the line. I want to be able to do the same, by just clearing whats on there and then creating the illusion of animation.

Geek-Master
Junior Poster
158 posts since Dec 2004
Reputation Points: 12
Solved Threads: 7
 
Just to be sure that we are on the same page... the function should only clear the 'output' on the screen... not do anything with the actual program or command prompt. I want to create 'animation' in the command prompt, I have done this before with Batch files by echoing text and then 'cls' and echoing the text in another spot on the line. I want to be able to do the same, by just clearing whats on there and then creating the illusion of animation.


I'm having trouble deciding if you're stupid or just can't read well. We understand exactly what you want. You've had three useful replies and two viable solutions. What need is there to be on the same page if you already have a workable answer?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I'm just stupid, and lovin every minute of it

Geek-Master
Junior Poster
158 posts since Dec 2004
Reputation Points: 12
Solved Threads: 7
 

At least you can admit it. Kudos.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

LOL, sorry to be such an 4$$ but I'm still a new guy in programming. Just trying to learn a lot at one time. Thanks for the replys.

Geek-Master
Junior Poster
158 posts since Dec 2004
Reputation Points: 12
Solved Threads: 7
 

if you want to clear selective positions, you'll need to write the text that's there in the background colour to those positions.

This is an age old technique employed by terminal applications on mainframes and it works well.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

cls and clrscr() work the same i thought..... :s ?

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 

when I use clrscr(); it gives me a linker error that it's undefined. Could this be an issue with my library? I'm using Dev-C++ 4.

Geek-Master
Junior Poster
158 posts since Dec 2004
Reputation Points: 12
Solved Threads: 7
 

>when I use clrscr(); it gives me a linker error that it's undefined.
clrscr is pretty much restricted to Borland compilers. Use another method. I'm sure someone will be happy to share their lame, stupid way of clearing the screen with you.

What exactly are you trying to do and on what system with what compiler? Since this is an implementation-dependent question, it's helpful to know what implementation you're using. Otherwise there's now way I can give you a decent solution.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

To Narue: in your own opinion, what would be a good compiler to use?

I'm just in a learning phaze. I'm not making anything grand right now, but I'm just making simple things and learning from the experience. I'm just curious own how it all works, that's all. I'll see if I can search harder in google or my local libraries "book libraries :cheesy: ."

Thanks bunches Narue ^^

Running on Windows XP, Bloodshed Dev-C++ 4, for what reason...to learn.

Geek-Master
Junior Poster
158 posts since Dec 2004
Reputation Points: 12
Solved Threads: 7
 

>To Narue: in your own opinion, what would be a good compiler to use?
Dev-C++ is a good one for Windows, but just about any modern compiler is workable if you know your way around the language and the system.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

what exactly is the error? the exact words

odee
Light Poster
27 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

untitled1.o.(.text+0xc):untitled1.cpp: undefined reference to `clrscr'

Am I supposed to alter the header file, I guess to define it?

Geek-Master
Junior Poster
158 posts since Dec 2004
Reputation Points: 12
Solved Threads: 7
 

have you got the main libraries attached to the project? an undefined linker error usually is when th library is not in the project, no matter what headers are included (this always happens when people learn DirectX!!!!)

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 

did you include conio.h?

odee
Light Poster
27 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

yes, yes i did

I've tried it on both Dev-C++ 4 Bloodshed, and Quincy 2002's compilers. Both give me the same exact errors.

Geek-Master
Junior Poster
158 posts since Dec 2004
Reputation Points: 12
Solved Threads: 7
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You