Console Closing Down

Pages: 12345... 7
@Duoas

Intersesting post.
But it does not quite answer my question.

I am using MS C++ Visual Studio 2008.
When I follow your link to Microsoft, they tell me to use

 
system("cls")


to clear the screen.

But my question was, what should I use since system("cls") seems to be bad practice?

And one thing I do not quite grasp is that you are always talking of taking away the control from the user, but I only use "cls" after the user has done something to trigger it. Say, I ask "Do you want to do soemthign again (y(n)?" and then, if it is a yes, I usually use system("cls") to reset the screen. So, the software is controlled by the user.

So far I have not programmed anything (in c+) that was not supposed to be controlled by a human.

regards

int main
Last edited on
This is going to sound harsh, but I don't mean it badly.

You have not made sufficient effort to understand the underlying concepts I have tried to teach you.

In technical and industrial programming places it is not uncommon for an employee to be given a 500 page technical manual and be expected to return to work the next day familiar with its contents.

The same holds true for other disciplines. If you want to be a doctor, you spend a lot of time reading huge manuals on every kind of disease and function of the body -- and you are expected to remember it. If you want to be a lawer you spend four years studying and preparing for the bar exam. A lot of people don't pass it.


The link I gave you has, in a big grey box, a complete, 40-line function that you can cut and paste directly into your program to clear the screen.


Everything the computer does is ultimately controlled by a human. The issue isn't whether humans exist -- it is what options the human has in manipulating the program.

I am aware that you have only written very simple programs so far, where you expect someone to be sitting there typing 'y' or 'n' to questions and the like. It is time to move beyond that constricted point of view and learn how to make your programs more intelligent, safer, and more usable in a powerful, industry standard way.


If this were just a normal thread I might have just given you a link to the MS site and nothing else. But since this is stickied you get the lecture as a bonus. I don't give it because I think you are stupid. I give it because I think you are smart, and capable of learning how to make better software. Now go, young grasshopper, and exhibit some kung fu.
Last edited on
closed account (z05DSL3A)
int main wrote:
class="quote">
class="qd">But my question was, what should I use since system("cls") seems to be bad practice?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <windows.h>
#include <iostream>

int main(int argc, TCHAR argv[])
{

    HANDLE hStdout;

    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

    cls(hStdout);

}

void cls( HANDLE hConsole )
{
   COORD coordScreen = { 0, 0 };    // home for the cursor 
   DWORD cCharsWritten;
   CONSOLE_SCREEN_BUFFER_INFO csbi; 
   DWORD dwConSize;

    // Get the number of character cells in the current buffer. 

   if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
      return;
   dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

   // Fill the entire screen with blanks.

   if( !FillConsoleOutputCharacter( hConsole, (TCHAR) ' ',
      dwConSize, coordScreen, &cCharsWritten ))
      return;

   // Get the current text attribute.

   if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
      return;

   // Set the buffer's attributes accordingly.

   if( !FillConsoleOutputAttribute( hConsole, csbi.wAttributes,
      dwConSize, coordScreen, &cCharsWritten ))
      return;

   // Put the cursor at its home coordinates.

   SetConsoleCursorPosition( hConsole, coordScreen );
}
Last edited on
who would have thought that a beginner's forum would turn into a massive debate.
instead of using windows to program like the losers you all apparently are, use linux, and even if your ide doesn't say to keep the terminal open, it keeps it open anyways.
who would have thought that a beginner's forum would turn into a massive debate.
instead of using windows to program like the losers you all apparently are, use linux, and even if your ide doesn't say to keep the terminal open, it keeps it open anyways.


Who would've thought a newbie 15 year old would turn it into a "linux is awesome, windows sucks because I said so" thread.
closed account (z05DSL3A)
[Deleted] off topic posting
Last edited on
whoa...
what i meant to say was that
for programming, and everything in general really, but especially programming, linux is the proven choice
it's not like i don't know what i'm saying, i programmed for windows for a long time, even video games. i programmed a lot of windows programs. i know what i'm saying when i say that linux has a better programming environment than windows because i have used both. and if you guys were on linux, you wouldn't be arguing about this because there is no need for pausing the terminal

just because i'm fifteen, doesn't mean that i don't know jack squat about c++ programming.

now i admit, calling you guys losers for programming on windows went a little bit to far and i'm sorry.

and a couple of you guys have helped me out and i'm greatful for that.
what i was trying to point out in the initial comment was that
point a: a beginner was asking a simple question about keeping the console window open, and that turned into a massive debate, which by no means was meant to be so, seeing as how the initial poster has not gotten involved in the discussion.

point b: linux has a better programming environment and is better in general, and you would gain a great benifit by using it, seeing as it would make your life easier in programming, particularly in C++.
Metalmop, please stop hijacking this thread. It was stickied because this issue is a common one, whether reasonable or not, and it does not help beginners to have to wade through someone else's issues. The OP got his answer and more.

If you want to propound the virtues of Linux please start a new thread in the Lounge, and I'm sure you'll get a lively discussion going.
Dont use "system pause" because it sucks, you can google it... you best use "cin.get();" wich plain waits for a key to be pressed thank you.
because it sucks
is hardly a valid reason.
Hello all, and I'm a completely new to all this C++ and am having a go at it, In the first page what does Ropez mean by IDE? and does the bloodshed Dev-C++
have the option to keep the cmd open that you mentioned? Sorry if these are answered elsewhere but I'm a bit stumped, and to metalmop, this is a BEGINNER thread, and windows is a normal friendly environment for most computer users, if you get into heavy programming, by all means, use linux, but I don't think most of the people looking through here for help are ready for the hard yards yet.
IDE http://en.wikipedia.org/wiki/Integrated_development_environment
Dev-C++ does not.
Please don't beat up on anyone for past stuff.

Personally, I use the command window all the time, so I've got it set up to position itself real purty-like and automatically load all my custom commands and directories.

I've even got little batch scripts to load an environment based upon the programming tool in use. For example, in my D:\bin directory, I've got
g++.bat
1
2
@mpath a 0 C:\PROGRA~1\MinGW\bin
@"g++" %*

(The mpath program is one I wrote to manipulate the %PATH% variable.) So if MinGW's g++.exe is not in the path, it (a)dds it to the head of path and starts it normally. Thereafter the batch file is skipped entirely, since it finds the g++.exe first.

;-)
there is nothing wrong with using system("pause") if you remembered to include <stdlib.h>
I've used the system() functions in early programs, and I didn't need the cstdlib header file.

That isn't to say that other people won't need the header.

My 2 cents...
wow you should really read the other posts

system() is a function that calls on the Operating System
which we would expect, because of its name.

There are many more than just one operating system, of which would interpret the string passed to the function differently. This makes your code less portable, which was why C/C++ was made and people use assembly less.

Example:
system("pause");

will tell the system to do the "pause" command.
In Windows, this creates the "Press Any Key To Continue...".

However, in Linux, or Unix, or Macintosh, or most other operating systems, this will create a runtime error at best, and compile error at worst.

In Linux I could do
system("ls");

which wouldn't do anything in Windows, but in Linux is a very common command (equivalent to DOS's dir command).

So unless you want to be stuck with Windows in this era of what seems like the death of Microsoft, it is much better to learn, and get in the habit of using other techniques--portable techniques.
Last edited on
Although I shouldn't speak for him, I believe he likes it for the same reason I do: while it does use system(), it does so in a clever way; by taking advantage of the fact that static objects are automatically deleted. Kudos to you, ropez!

Another version I've seen (implemented in Code::Blocks) uses a program to call the program being tested (through the use of system(), I presume) and after the latter returns control to the former, which then proceeds to use some method to capture input (might be system("pause"). I can't remember right now). This has the advantage of not having to add anything to the program.
...which is the correct way for it to be done. I am always surprised that IDE's so often lack this feature. The code to do it is trivial:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <algorithm>
#include <string>
#include <windows.h>
using namespace std;

int main( int argc, char **argv )
  {
  TCHAR*              exepath;
  TCHAR*              cmdline;
  STARTUPINFO         startup_info;
  PROCESS_INFORMATION process_info;

  if (argc < 2) return 1;

  cmdline = GetCommandLine();
  #ifdef _UNICODE
    TCHAR** wargv = CommandLineToArgW( cmdline, &argc );
    exepath = wargv[ 1 ];
    LocalFree( wargv );
  #else
    exepath =  argv[ 1 ];
  #endif
  cmdline = search(
              cmdline, cmdline +lstrlen( cmdline ),
              exepath, exepath +lstrlen( exepath )
              );

  fill_n( (char*)&process_info, sizeof( PROCESS_INFORMATION ), 0 );
  fill_n( (char*)&startup_info, sizeof( STARTUPINFO ),         0 );
  startup_info.cb = sizeof( STARTUPINFO );

  AllocConsole();  // make sure there is a console associated with this process

  if (!CreateProcess(
         exepath,
         cmdline,
         NULL,
         NULL,
         TRUE,
         NORMAL_PRIORITY_CLASS,
         NULL,
         NULL,
         &startup_info,
         &process_info
         ))
    return 2;

  CloseHandle( process_info.hThread );

  // wait for the child program to terminate
  WaitForSingleObject( process_info.hProcess, INFINITE );

  // Wait for the user
  const TCHAR* prompt = TEXT( "Press any key to continue..." );
  DWORD count;
  WriteConsole(
    GetStdHandle( STD_OUTPUT_HANDLE ),
    prompt,
    lstrlen( prompt ),
    &count,
    NULL
    );
  WaitForSingleObject(
    GetStdHandle( STD_INPUT_HANDLE ),
    INFINITE
    );

  return 0;
  }

Alas.
OSHI-
I'm so embarrassed! I thought I was looking at the last page when I was actually looking at the first.
I hope i ask this correctly as to not look like a total idiot. I have, at this point, read all the post in this thread and still have a burning question, and I will ask it in an example.

EXAMPLE:

I am writing a console Tic-Tac-Toe game. When the X player enters his move (2,3) I need to draw the tic tac toe board to reflect the change. I don't want to keep the old one there, and just redraw the new one below the old, so in my mind, I would want to clear the screen.

Would this be an acceptable place to add a system("CLS") , or any other method of clearing the screen? I understand the point of not using the system() function due to operating system, and accept it. so could I use Grey_Wolfs post?

If not, how would I reach the desired effect?
I hope i ask this correctly as to not look like a total idiot. I have, at this point, read all the post in this thread and still have a burning question, and I will ask it in an example.

EXAMPLE:

I am writing a console Tic-Tac-Toe game. When the X player enters his move (2,3) I need to draw the tic tac toe board to reflect the change. I don't want to keep the old one there, and just redraw the new one below the old, so in my mind, I would want to clear the screen.

Would this be an acceptable place to add a system("CLS") , or any other method of clearing the screen? I understand the point of not using the system() function due to operating system, and accept it. so could I use Grey_Wolfs post?

If not, how would I reach the desired effect?
Pages: 12345... 7