LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-01-2002, 08:18 AM   #1
OrganicX
Member
 
Registered: Aug 2002
Posts: 105

Rep: Reputation: 15
Question Clear the Screen in C++


Can anyone tell me how to clear the screen in C++ under Linux.

thanks
 
Old 11-02-2002, 06:28 AM   #2
M3xican
LQ Newbie
 
Registered: Nov 2002
Location: Naples, Italy
Distribution: Slackware
Posts: 14

Rep: Reputation: 0
clear

U have to use the clear() function.
It should required the conio.h library.
 
Old 11-02-2002, 10:59 PM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
conio.h? Don't have that on my linux box.

Take a look at this thread that was around for a while. C++ provides nothing special to do this. The C code in the examples will work.

http://www.linuxquestions.org/questi...threadid=11857
 
Old 11-03-2002, 06:47 PM   #4
OrganicX
Member
 
Registered: Aug 2002
Posts: 105

Original Poster
Rep: Reputation: 15
#include<iomanip.h>
#include<unistd.h>

void main()
{
.
.
.
system("tput clear");
.
.
.
}



The function system("tput clear"); and the #include<unistd.h> library clear the screen, for anyone else interested.

Last edited by OrganicX; 11-03-2002 at 06:48 PM.
 
Old 11-03-2002, 10:11 PM   #5
gdrobson
Member
 
Registered: Jun 2002
Location: Ontario, Canada
Distribution: RH8.0
Posts: 65

Rep: Reputation: 15
Code:
#include <stdio.h>

// f fullscreen
// r replace line

int main(){
   printf ("\f");
   printf ("asdf\n");
   return 0;
}
here you go
 
Old 11-04-2002, 02:39 AM   #6
rao
LQ Newbie
 
Registered: Jun 2002
Location: Malaysia
Posts: 14

Rep: Reputation: 0
Use clear() function. For this function use header #include<curses.h
 
Old 11-04-2002, 10:48 AM   #7
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
be real careful using something line printf("\f"). If you pipe your console to a file it might not do what you expect.... you have to keep in mind you never know what printf is actually printing too (screen, printer, file, ect).
 
Old 11-04-2002, 04:35 PM   #8
M3xican
LQ Newbie
 
Registered: Nov 2002
Location: Naples, Italy
Distribution: Slackware
Posts: 14

Rep: Reputation: 0
Unhappy Sorry

I'm sorry...
the function is clrscr() and is provided by borland C++ (so the conio.h library).

The best way under Linux is using ncurses or system("clear");.

Mea culpa...
 
Old 12-02-2002, 04:43 AM   #9
Slycordinator
Member
 
Registered: Dec 2002
Location: Washington State
Posts: 30

Rep: Reputation: 15
More portable clear screen fcn

Try this for size-

void clear_screen()
{
for(int i=0;i<23;i++)
{
cout << endl;
}
}

Unlike other clear_screens this is completely portable.
 
Old 12-02-2002, 09:40 AM   #10
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
What if your terminal has more than 24 rows? Then you go up to 50, 80, 100? Not the best solution...
 
Old 12-03-2002, 09:49 AM   #11
Admiral Yoshi
Member
 
Registered: May 2002
Location: Edmonton
Distribution: Slackware 9.1
Posts: 31

Rep: Reputation: 15
it will start at the bottom too...
 
Old 12-03-2002, 01:35 PM   #12
Slycordinator
Member
 
Registered: Dec 2002
Location: Washington State
Posts: 30

Rep: Reputation: 15
I think this should work, not sure though

void clear_screen()
{
#if defined(WIN_32)
system("cls")
#endif
#if defined(LINUX)
system("clear")
#endif
}

Should work I think; I'll try it and see.
 
Old 12-03-2002, 02:10 PM   #13
Slycordinator
Member
 
Registered: Dec 2002
Location: Washington State
Posts: 30

Rep: Reputation: 15
I made a mistake, corrected

void clear()
{
#if defined(_WIN32)
system("cls");
#endif

#if defined(_linux)
system("clear");
#endif
}

does the same system call for clearing screen in linux also work in macs now that OS X is based on Unix? Because I do remember older macs having a different clear screen system call before.
 
Old 07-22-2003, 10:37 AM   #14
priggle
LQ Newbie
 
Registered: Jul 2003
Location: Indiana
Posts: 2

Rep: Reputation: 0
You can clear the screen in C++ using the following code:

#include <iostream>

using namespace std;

int main(void)
{
cout << "\x1B[2J"; // Clear the screen
cout << "\x1B[0;0H"; // place cursor at home position
return 0;
}

Last edited by priggle; 07-22-2003 at 10:56 AM.
 
Old 07-22-2003, 10:45 AM   #15
DrOzz
Senior Member
 
Registered: May 2003
Location: Sydney, Nova Scotia, Canada
Distribution: slackware
Posts: 4,185

Rep: Reputation: 60
just as easy to make a system call to the clear command..and make a function for it where all you would have to type is something like clrscr(); wherever you wanted to clear screen instead of typing it all over again.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how do i replace the clear "clear screen" with the cls command thefedexguy SUSE / openSUSE 2 12-02-2005 05:02 PM
how to clear screen in C ? indian Programming 3 10-01-2004 10:58 PM
chroot not able to clear screen Archite Linux - Security 6 07-13-2004 10:41 AM
clear the screen!!! raven Programming 18 11-24-2003 07:27 PM
How to clear screen? alexzander Programming 1 01-23-2003 08:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration