Replacement For Form Feed?

Pages: 12
It appears that '\f' Form Feed no longer clears the screen, at least on the bash terminal type (unless someone thinks otherwise, in which case I will supply the sample code I tested things out on). So I was wondering if there is another way to easily clear the screen. I know the bash command 'clear' will clear the terminal screen, but I don't know how to get a terminal-run program to use a terminal command during the run-time of said program. Any thoughts?
1
2
3
4
5
6
7
8
#include <iostream>
using namespace std ; 

int main()
{
   cout <<"\n This is going to be cleared ";
     clear();
}
@bluecoder: Do you mean system("clear"); ?
yes system("clear");
This is the code I'm using just to test the different methods before I implement it into my actual program. However, trying this below, and trying the ClearScreen() method, both wind up not outputting anything to the screen. It just stays blank. Is there something I'm doing wrong here? It almost seems like the buffer isn't keeping up...not sure.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
  int i=1;
  while (true)
  {
    system("clear");
    cout<<i;
    i++;
    sleep(1);
  }  
  return 0;
}
sleep(1) pauses your program only for one milisecond. Try sleep(1000);
Thanks, that will probably save me a lot of confusion for this and later problems, but something else is still preventing anything from being output to the screen. I don't see any reason for this.
@Guruwar

If you're wanting to use system("clear");, you have to insert the command correctly. It's written as system("cls");
Since I'm new to system commands, I used the example provided by the article that Null pointed to.
It still gives no output. (And in case you meant just replace the system("clear") with system("cls"), I tried that and when it runs gives a "cls not found" error, so I assume that's not what you meant).
The thing is, it's not just the system() commands, it's also not outputting anything for form feed, more than 10 newlines at one time, etc. Is maybe the sleep() getting priority over the buffer and things don't have a chance to go? I'll experiment a bit, but this is really creating a lot of problems.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
  int i=1;
  while (true)
  {
    if (system("CLS"))
      system("clear");
    cout<<i;
    i++;
    sleep(1000);
  }
  return 0;
}
Guruwar wrote:
I used the example provided by the article that Null pointed to.
Out of an entire article on the various ways to clear the terminal, you pick the one that is plainly marked as a Bad Thing.

I'm not so sure I want to tell you why you aren't seeing anything on your display. It is fairly obvious... but it isn't a syntax problem.
No, I only used the system() commands because I was told to by commentors. I saw that it was marked as the wrong way to do it. I'm just running out of options here. I was hoping someone else would plug this code in, to maybe see if it was just my terminal or something that was different. I guess I'll just drop it for now, and live without. Whatever is the issue, if it's not a syntax problem, then it's not really under my control. Thanks for the help, everyone.
@Guruwar

I inserted your program into Microsoft Visual C++ 2008 Express, added #include <windows.h> to use the Sleep(); command, and it worked perfectly. Without 'Sleep' starting with a capital 'S', I would get an error that sleep couldn't be found.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Form Feed.cpp : Defines the entry point for the console application.
//
#include "stdafx.h" // Needed because of the compiler I'm using.
#include <iostream>
#include <cstdlib>
#include <windows.h>

using namespace std;

int main()
{
  int i=1;
  while (true)
  {
    system("cls");
    cout<<i;
    i++;
    Sleep(1000);
  }  
  return 0;
}
No, I only used the system() commands because I was told to by commentors. I saw that it was marked as the wrong way to do it. I'm just running out of options here.

Fair enough.

Make sure to flush your output to screen.
cout << i << flush;

If that isn't the problem then something else is going on.

Another thing you could do (instead of clearing the entire screen) would be to simply output a '\r' in front of your integer (but don't forget to flush still).

Let me know how it goes...
$ whatis sleep
sleep (3)            - Sleep for the specified number of seconds
$ whatis clear
clear (1)            - clear the terminal screen
$ g++ whitenite1.cpp
fatal error: windows.h: No such file or directory
¿Where is Waldo? ;)
Case sensitive? It's Windows.h with a capital W. And I have never seen sleep (lowercase) or clear defined/declared in any header :/ Oh, Linux. I forgot.
Last edited on
And I have never seen sleep (lowercase) or clear defined/declared in any header :/
He's using Linux http://pubs.opengroup.org/onlinepubs/009604599/functions/sleep.html


sleep(1) pauses your program only for one milisecond. Try sleep(1000);

I was wrong here. Linux's sleep() pauses program for specified number of seconds while on Windows Sleep() pasues program for a specified number of miliseconds
Last edited on
sleep(1) is a shell command, meaning you'd have to use system() to invoke it... which would make it near worthless.

Try this instead:
http://www.cplusplus.com/forum/unices/10491/#msg49054
sleep(3) is a library call, meaning you don't have to use system()... which would make it quite useful.
;)
Pages: 12