endl, \n, clrscr()

Hi

I have seen someone using "clrscr". What's its function? I guess it stands for 'clear screen'. But which screen it want to clear. And what header file to include to make it functional? Please tell me.

1
2
3
4
5
6
7
8
9
#include <iostream>

void main()

{

  clrscr();
  ...
}



I have been told "\n" and "endl" are almost the same but there are some technical differences between the two. Is there also something such as "newl" which is also used for new line?

When I want to use "endl" I do it the following way and it works.

Using "endl":

This will work:
 
cout << "Yes, I'm fine" << endl;


This won't work:
 
cout << "Yes, I'm fine. \endl";


Using "\n":

This will work:
1
2
cout << "Yes, I'm fine.\n";
cout << "What about you?\n";


This will not work:
1
2
cout << "Yes, I'm fine." \n;
cout << "What about you? " \n;


Is my observation correct that you have to enclose "\n" within the quotation but "endl" is to be kept outside the quotation marks?

Please help me with the above queries. It would be kind of you. Thanks a lot.
Last edited on
1) clrscr() is a non-standard function, don't use it
2) std::endl something you send to a stream that outputs a '\n' character then flushes the buffer
3) '\n' is a special character, different from std::endl, it represents a "newline" (although what that means might be different on your OS)

1
2
3
std::cout<<"Stuff"<<std::endl;
//is the same as
std::cout<<"Stuff\n"<<std::flush;
Hi firedraco

Thanks for the help.

Okay, I won't use "clrscr()" but can you tell what its not-standard function is?

And what do you really mean by "non-standard" above? Does it have something to do with C++ and "Standard C++" (although I don't know the distinction between the two).

Best wishes
Jackson
Help, please.
By non-standart function you can understand that it is not included in C++ libraries. For example the functions you create by yourself are non-standart functions ;)
Thanks, ZephyrTR.

I also use function "system()" such as "system("pause")", is it also non-standard. And it works without including any header file - although I was told I would need to include "<windows.h>". I'm using Dev-C++. Please guide me on this. Thanks.
It is also non-standard due to the fact that it is OS dependent.

It would be best to use

 
cin.get();

This simply waits for the user to press the 'Enter' key.

And in some cases where the stream is not cleared, the following will ignore and then wait for the user to press 'Enter' Key.

1
2
cin.ignore();
cin.get();
jackson-

System("command"); is a function call that will make use of any of the commands available in your windows command prompt. Go to StartMenu->Run and type in cmd and hit enter, that will bring up a command prompt. If you type cls into the command prompt and hit enter it will clear the screen of the console. Similarly when you use a System("cls"); call in your code it will clear the console screen at run time.

-ceruleus

edit: if it is of any help the code for clrscr() would probably be something like this:

1
2
3
4
void clrscr( )
{
 system("cls");
}
Last edited on
"The C++ standard" refers to the international ISO C++ standard. If a function/expression/marcro/whatever is in the standard, then it will. But for those not in the standard, it is not guaranteed that they will remain useful (e.g. understandable by new compilers) in, who knows, 10 years time. It is an evolving language.
BTW, system() is a standard function. You find it by #including <cstdlib>.
The problems are that it opens security vulnerabilities, and that it is inherently OS-dependant. For example

system("cls");

will only work on Windows. On Unix, you would have to say

system("clear");

and hope it works.
Topic archived. No new replies allowed.