Solved

Visual C++

Posted on 1998-05-27
15
244 Views
Last Modified: 2013-12-14
What is the command for clearing the screen in Microsoft's visual C++ and what library does it use?  (I know how to add new lines - I want my cursor to end up at the top of the cleared screen)  By the way - Borland's clearscreen command does not work for me with Visual - ??
0
Comment
Question by:Reid
  • 6
  • 3
  • 2
  • +2
15 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 1164837
#include <stdlib.h>

system("cls");
0
 

Expert Comment

by:zaphord
ID: 1164838
zaphord,

I am sure Reid is talking about a Console application, not a Windows GUI application.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1164839
chensu,

In that case your sollution is correct.
And I did preface my sollution with:
"If the problem is in a client window"
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Expert Comment

by:zaphord
ID: 1164840
Although system("cls") will work it does have some drawbacks.

"The system function passes command to the command interpreter, which executes the string as an operating-system command.  You must explicitly flush (using fflush or _flushall) or close any stream before calling system"

A native way of clearing the console in a Win32 application is presented in a KnowledgeBase article Q99261 (accessible from the online help).

A DOS application (MSVC 1.5x only) can use the clrscr() function.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1164841
I am working on a console application - I am obviously still new at this...

chensu's answer compiled, etc, but didn't work.  I tried adding fflush and got an
"undeclared identifier" error - do I need to add another library for it?

zaphord's answer is way beyond me...
0
 

Author Comment

by:Reid
ID: 1164842
I've tried _flushall and fflush(stdin) with system("cls"), it still doesn't work.
0
 

Author Comment

by:Reid
ID: 1164843
This comment did not seem to be added -

I've flushed the buffers using fflush and _flushall - system("cls") still does not work.
0
 

Author Comment

by:Reid
ID: 1164844
If it's a real console application (using the console API under NT) then you can use the WriteConsole() function to write 80x25 characters (or whatever size your console is) to the screen. Otherwise, use printf() or puts() in a loop to clear the screen.
0
 
LVL 1

Expert Comment

by:bsimser
ID: 1164845
If I use printf or puts, my cursor still ends up at the bottom of the screen.  Can you clarify how to use the WriteConsole function?  Will my cursor end up at 1X1?
0
 

Author Comment

by:Reid
ID: 1164846
Ok, here's the deal - chensu's answer was exactly what I was looking for - except that I can't get it to work.  I am very new at this - consider my knowledge base to be minimal.  (I couldn't figure out how to find the material referenced in alexo's note).  I flush everything using _flushall(); but system("cls") doesn't do anything.  I have looked at WriteConsole, but can't figure out how to use it so my cursor is at the top.  

Thanks a lot...
0
 

Author Comment

by:Reid
ID: 1164847
This should work for you:

#include <windows.h>
#include <stdio.h>
#include <conio.h>

HANDLE hConsole;
COORD pos;

void cls()
{
    DWORD chwrote;

    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    FillConsoleOutputCharacter(hConsole, ' ',
            80*25, pos, &chwrote);
    SetConsoleCursorPosition(hConsole, pos);
}

void main(void)
{
    pos.X = 0;
    pos.Y = 0;

    AllocConsole();
    printf("This is where your app starts...");
    getch();
    cls();
    printf("This is 0, 0");
}

0
 
LVL 1

Accepted Solution

by:
bsimser earned 100 total points
ID: 1164848
I used system("cls") method before and it worked.

The KB article alexo mentioned is at http://support.microsoft.com/support/kb/articles/q99/2/61.asp.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1164849
It works!  Thanks!  Now I need to go and figure out what you did....
0
 

Author Comment

by:Reid
ID: 1164850
The accepted answer assumes the console is 80x25 charactes, which is wrong.
A Win95 console can also be 80x43 or 80x50.  An NT console can be almost any size.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1164851
Well, the console I get is 80 x 25 characters..... so it works well for me.  I think that the reason system("cls") doesn't work is that this is a university owned Visual C++, and they've made some alterations so that we can not customize it to our own needs.  Now that I understand what was done, I can change the console size easily if I need to.  Thanks everyone!

Reid
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question