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
Solved

Visual C++

Posted on 1998-05-27
15
245 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

792 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