Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Visual C++

Posted on 1998-05-27
15
Medium Priority
?
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 400 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

688 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