Solved

Visual C++

Posted on 1998-05-27
15
243 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
Comment Utility
#include <stdlib.h>

system("cls");
0
 

Expert Comment

by:zaphord
Comment Utility
zaphord,

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

Expert Comment

by:chensu
Comment Utility
chensu,

In that case your sollution is correct.
And I did preface my sollution with:
"If the problem is in a client window"
0
 

Expert Comment

by:zaphord
Comment Utility
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
Comment Utility
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
Comment Utility
I've tried _flushall and fflush(stdin) with system("cls"), it still doesn't work.
0
 

Author Comment

by:Reid
Comment Utility
This comment did not seem to be added -

I've flushed the buffers using fflush and _flushall - system("cls") still does not work.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:Reid
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
It works!  Thanks!  Now I need to go and figure out what you did....
0
 

Author Comment

by:Reid
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now