Solved

windows.h console functions, smitty1276

Posted on 2001-06-05
13
1,115 Views
Last Modified: 2013-12-14
please take a look at the class below and suggest how i can display variables (i.e. integer vars, string vars, etc.) can the print function be overloaded to handle other data types?

// console.h
#ifndef CONSOLE_H
#define CONSOLE_H

#include <windows.h>

class console
{
public:
     console();
     void conInit   ();
     void cls       (void);
     void print     (char *text);
     void gotoxy    (short x, short y);
     void fontColor (WORD attr);
private:
     HANDLE scr;
};

#endif

// console.cpp
#include "console.h"

console::console()
{
     conInit();
}

void console::conInit(void)
{
    scr = CreateConsoleScreenBuffer( GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, CONSOLE_TEXTMODE_BUFFER, 0 );
    SetConsoleActiveScreenBuffer( scr );
}

void console::cls(void)
{
     CloseHandle(scr);
     scr = CreateConsoleScreenBuffer( GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, CONSOLE_TEXTMODE_BUFFER, 0 );
     SetConsoleActiveScreenBuffer( scr );
}

void console::print(char *text)
{
     char buf[1024];
     strcpy(buf, text);
     DWORD num;
     WriteConsole( scr, buf, strlen(text), &num, 0);
}

void console::gotoxy(short x, short y)
{
     COORD pos = { y, x };
     SetConsoleCursorPosition(scr, pos);
}

void console::fontColor(WORD attr)
{
     SetConsoleTextAttribute(scr, attr);
}

// consoleMain.cpp
#include "console.h"

#include <string>
using std::string;

void main()
{
     int num = 5;
     sting name;
     name = "kiphughes";

     console A;
     A.cls();
     A.conInit();
     A.gotoxy(5,5);
     A.fontColor(FOREGROUND_RED);
     A.print("This is a test");
     A.print(name);                    // won't print string
     A.print(num);                    // won't print int
     A.print(num + num);               // won't print calculations
     A.cls();
}
0
Comment
Question by:kiphughes
13 Comments
 
LVL 10

Expert Comment

by:makerp
ID: 6158728

    void print     (int num);

    void print     (char *text);

    void print     (float number);

then just implement them, c++ supports method overloading, the parameters must be different
0
 
LVL 22

Expert Comment

by:nietod
ID: 6159125
Some suggestions...

Your must remember to call close. If you failt to call close you will cause a resource leak.  Dangerious.  Instead give the class a destructor and have it call close..  Pluse the close function "reopens"   I assume that is a mistake.  if not that is another memory leakl.  A good safe  system woudl be

console::console() :
   scr(INVALID_HANDLE_VALUE) // Indicate class is colose
{
   conInit();
}

console::~console()
{
   cls();// Make sure class is closed.
}


void console::conInit(void)
{
   if (!Isclosed())  // If called when open
      cls();             // be sure to close first.
   scr = CreateConsoleScreenBuffer( GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
    0, CONSOLE_TEXTMODE_BUFFER, 0 );
    SetConsoleActiveScreenBuffer( scr );
}

void console::cls(void)
{
   CloseHandle(scr);
   scr = INVALID_HANDLE_VALUE;
}

bool console::IsClosed() const
{
    return scr == INVALID_HANDLE_VALUE;
}

Now it is still "unsafe" to use this class for output when it is closed.  It will simply ignore the requested operation in this case, it will not act unpredictably, so its an improvement over yoru version.


Your print function will fail to print anything longer than 1k.  sometimes that sort of limit is needed, but in this case it was totally arbitrary because you added an unnecessary copying of the data.  Try.

oid console::print(const char *text)
{
   DWORD num;
   WriteConsole( scr, text, strlen(text), &num, 0);
}l

Now you can overload the output functiosn as markerp suggested, but I would recommend using operator << 

0
 
LVL 22

Expert Comment

by:nietod
ID: 6159141
for example,

console &operator << (console &Con,const char *Dat)
{
   con.print(Dat);
   return Con;
};

console &operator << (console &Con,int Dat)
{
   char Str[32];

   sprintf(Str,"%i",Dat);
   con.print(Str);
   return Con;
};

console &operator << (console &Con,double Dat)
{
   char Str[32];

   sprintf(Str,"%e",Dat);
   con.print(Str);
   return Con;
};

console &operator << (console &Con,char Dat)
{
   char Str[2] = "?";

   Str[0] = Dat;;
   con.print(Str);
   return Con;
};


Now you can do things like

console C;

C << "Label:" << ' ' << 123 << ' ' << 123.456;

0
 
LVL 2

Expert Comment

by:smitty1276
ID: 6160895
Actually, that wasn't intended to be a close function.  It was supposed to be a clear screen function (I just stole the old BASIC function name).  

Like I said before... I haven't used ConsoleScreenBuffer objects all that much myself.  When I posted those functions I specified that there was probably a better way to clear the screen.  I just found that closing the handle and reopening it was the quickest to code, and it seemed to do the job.

You bring up a good point though. There needs to be a method that will close the buffer and deallocate the memory.
0
 
LVL 1

Author Comment

by:kiphughes
ID: 6170394
how does this display a string?

example:

console C;
string str = "kiphughes";

C << str;
0
 
LVL 1

Author Comment

by:kiphughes
ID: 6170399
nietod,

also, where am i suppose to put the code u posted? do i put it in the console class? or do i put that in a different class that will use the overloaded operators? either way, what must i put in the .h file? i think i'd rather have this in my console class.

btw, what's the difference here?
console &operator << (console &Con,char Dat)
console &operator << (console &Con,const char *Dat)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:nietod
ID: 6170528
>> lso, where am i suppose to put the code u posted?
the operator << functions are free functions (not members of the console class.  that is why tha don't have "console::" in there names.  The rest is in the console class, that is why they have "Console::" in their names.

>> what must i put in the .h file?
The changes to the console class and the operator << functions, but those go outside of the class.

>> console &operator << (console &Con,char Dat)
>> console &operator << (console &Con,const char *Dat)
the first outputs a character, the 2nd poiputs a NUL termianted strign of charcters, like

SomeConsole << 'A';  // Single charcter.
SomeConsole << "A"; // String of 1 character.

For those two cases the differences doesn't seem important, but there will probably be times when you have only a single character as a varaible and need to ouput it and you don't want to have to form a NUL termianted string from it.
0
 
LVL 1

Author Comment

by:kiphughes
ID: 6170544
what about displaying a string?
0
 
LVL 22

Expert Comment

by:nietod
ID: 6170577
That is what the 2nd one does.   I could be a string of 1 character or a string of 1000.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6170580
Oh you mean a STL string object.

console &operator << (console &Con,const string &Str)
{
   con.print(Str.c_str());
   return Con;
};

i.e. to print anyting, pass it to operator <<.  then convert that thing to a string and pass the converted string to prnt().
0
 
LVL 1

Author Comment

by:kiphughes
ID: 6170586
it won't work for some reason, i get this message:

error C2664: 'print' : cannot convert parameter 1 from 'const char *' to 'char *'
        Conversion loses qualifiers
Error executing cl.exe.

this is how the string is declared:

#include <string>
using std::string;

string str;
str = "kiphughes";
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 6170699
             error C2664: 'print' : cannot convert parameter 1 from 'const char *' to 'char *'
the problem is in print().  It should be declared to take a parameter that is a constant character pointer.  I did that above.  print doesn't change the string so there is no loss n functionallity by making this explicit, by adding the const.  But when you make it constant you increase the usability of the function.  it can be used with non-constant string AND with constant strings.  (your version could only be used with non-constant strings).   This more than doubles the usability of the function since most strings actually will be constant.
0
 
LVL 1

Author Comment

by:kiphughes
ID: 6171512
i got it working, thanx!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

863 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

20 Experts available now in Live!

Get 1:1 Help Now