Member Avatar for Geek-Master

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.

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

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.

>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.

Member Avatar for Geek-Master

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.

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?

Member Avatar for Geek-Master

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

commented: Merry Christmas -Narue +1

At least you can admit it. Kudos.

Member Avatar for Geek-Master

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.

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.

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

Member Avatar for Geek-Master

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.

>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.

Member Avatar for Geek-Master

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.

>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.

what exactly is the error? the exact words

Member Avatar for Geek-Master

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

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

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!!!!)

did you include conio.h?

Member Avatar for Geek-Master

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.

did you include conio.h?

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.

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

For those that aren't aware, the linker does its thing after the compiler. It apparently compiled okay. But it will not link because the code written to do what you want is missing. Generally this is in a library that is also supported by a compiler.

It's tantamount to me saying, "Here's a check for a million dollars". If I'm Bill Gates, I could do that. But I'm not. And nothing that I can forsee is going to give me access to his bank accounts.

But I said, "here's a million dollars" -- why can't it be so? It just is. Such is the way of headers and libraries. The header says, "Here's how you can have a million dollars". It's up to the library to come through and look for Bill Gates. Without the library, your code may simply be making promises your linker can't keep.

[edit]So Borland library code in a non-Borland system may lead you here: http://www.eskimo.com/~scs/C-faq/q10.11.html.

Member Avatar for Geek-Master

For those that aren't aware, the linker does its thing after the compiler. It apparently compiled okay. But it will not link because the code written to do what you want is missing. Generally this is in a library that is also supported by a compiler.

It's tantamount to me saying, "Here's a check for a million dollars". If I'm Bill Gates, I could do that. But I'm not. And nothing that I can forsee is going to give me access to his bank accounts.

But I said, "here's a million dollars" -- why can't it be so? It just is. Such is the way of headers and libraries. The header says, "Here's how you can have a million dollars". It's up to the library to come through and look for Bill Gates. Without the library, your code may simply be making promises your linker can't keep.

Thanks, I've been wondering about the header files, "where's the code for the things I'm calling", if there was more to it. I'll be checking if "Bill" is even there.

Why can't I use conio.h functions like clrsrc()?

Because conio.h is not part of the C standard. It is a Borland extension, and works only with Borland compilers (and perhaps some other commercial compilers). Dev-C++ uses GCC, the GNU Compiler Collection, as it's compiler. GCC is originally a UNIX compiler, and aims for portability and standards-compliance.

If really can't live without them, you can use Borland functions this way:
Include conio.h to your source, and add C:\Dev-C++\Lib\conio.o to "Linker Options" in Project Options (where C:\Dev-C++ is where you installed Dev-C++).
Please note that conio support is far from perfect. I only wrote it very quickly.

http://www.bloodshed.net/dev/faq.html#conio

Member Avatar for Geek-Master

Thanks for the Hyperlink, this will reduce future trouble I might have with my compiler.

if you want to clear the screen try this one:

#include<stdlib.h>

int main() {
system("cls");
return 0;
}

if you want to clear the screen try this one:

#include<stdlib.h>

int main() {
system("cls");
return 0;
}

::smacks forehead::

Narue: leave bruises.

hehehe... forgot to read the previous ones...

Member Avatar for Geek-Master

I feel horrible about how long this "tutorial" on cls, went. I want to thank all of you who helped me. I learned more than just being able to clear the output on the screen. Narue's example was the one that works perfectly with my compiler, its just the "clrscr()" method that gave me strife. I have a few friends that are using the same compiler I am, so I'll have an answer when they try to use conio.h and clrscr(). :mrgreen:

I guess this can be closed, unless others are having trouble. ;)

>I feel horrible about how long this "tutorial" on cls, went.
Hey, no biggie. We're happy to continue helping until you feel comfortable with the answers. It's a waste of time for all parties if you walk away without an understanding of the solution. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.