conio.h functions in mingw?

This is a discussion on conio.h functions in mingw? within the C++ Programming forums, part of the General Programming Boards category; I need to use a few conio.h functions - namely clrscr(), gotoxy() and most importantly delay() - for a console ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    37

    Question conio.h functions in mingw?

    I need to use a few conio.h functions - namely clrscr(), gotoxy() and most importantly delay() - for a console C++ program...A friend of mine says its only available in Borland Turbo C++ 2.x (the DOS one *gasp cough cough*), while I use Bloodshed Dev C++. Are there any replacement functions for the aforementioned three, or some other way to implement them in Bloodshed Dev C++?
    -regards,
    ultrabot90

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    DevC++ versions 4.80 and 4.992 do define conio.h in the compiler library. Although clrscr() is more of a Borland compiler function.

    You can use system("cls") to clear the screen if you get desperate but thats not really good advise as system commands are bad and unsafe, There are windows funtions that clear the screen... dev does use the windows.h header library so a windows function is a much safer way to do this
    I'm just trying to be a better person - My Name Is Earl

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Here's a few functions that I've cobbled together.
    Code:
    void GotoXY(int x, int y)
    {
      COORD coord;
      coord.X = x;
      coord.Y = y;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    
    void GetXY(int *x, int *y)
    {
      CONSOLE_SCREEN_BUFFER_INFO  csbInfo;
      GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbInfo);
      *x = csbInfo.dwCursorPosition.X;
      *y = csbInfo.dwCursorPosition.Y;
    }
    ...
    void ClrEol() 
    {
    	int i;
    	int x, y;
    	COORD a = { 80, 1 };
    	COORD b;
    	SMALL_RECT r;
    	DWORD len;
    	CHAR_INFO str[81];
    	for(i = 0; i < 80; i++) {
    		str[i].Char.AsciiChar = ' ';
    		str[i].Attributes     = 0x07;
    	}
    	GetXY(&x, &y);
    	len = 80 - x;
    	b.X = 0;
    	b.Y = 0;
    	r.Top = y;
    	r.Left = x;
    	r.Bottom = y+1;
    	r.Right = (SHORT)(x + len);
    	WriteConsoleOutput(GetStdHandle(STD_OUTPUT_HANDLE), str, a, b, &r);
    }
    
    void ClrLine(int y)
    {
    	GotoXY(0, y);
    	ClrEol();
    }
    
    void ClrScr(void)
    {
    	COORD a = {0,0};
    	DWORD nwrite;
    	FillConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x07, 2000, a, &nwrite);
    }
    Not saying that these are the best way to do things, but as far as I can tell, it works.
    Obviously, it's an ugly hack to use constant 2000 in ClrScr.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    37
    Quote Originally Posted by swgh View Post
    DevC++ versions 4.80 and 4.992 do define conio.h in the compiler library. Although clrscr() is more of a Borland compiler function.

    You can use system("cls") to clear the screen if you get desperate but thats not really good advise as system commands are bad and unsafe, There are windows funtions that clear the screen... dev does use the windows.h header library so a windows function is a much safer way to do this
    o_o Thanks a lot, I guess the answer to delay() would also lie in windows.h, then.
    It seems like this is a frequently asked question, seeing that one out of 11 people viewing this thread replied...
    Thanks again.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If I remember right, delay is a "sleep for x ms", which is what Windows Sleep does:
    http://msdn2.microsoft.com/en-us/library/ms686298.aspx

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    37
    Eh, I tried using #include<windows> and used sleep(100), but it says that
    " 'sleep' undeclared (first use this function)"
    Bummer...Now what?
    @matsp - Im quite the C++ noob, didnt get a word of your indegenious functions...
    Last edited by ultrabot90; 09-21-2007 at 06:26 AM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ultrabot90 View Post
    Eh, I tried using #include<windows> and used sleep(100), but it says that
    " 'sleep' undeclared (first use this function)"
    Bummer...Now what?
    @matsp - Im quite the C++ noob, didnt get a word of your indegenious functions...
    Try Sleep instead of sleep - remember, C (and thus C++) is a case-sensitive language, so upper and lower case names aren't the same.

    My "indegenious" functions are just calls to various Windows system calls - you can look up the system calls in MSDN if you like. If there's anything in the calls you find unclear, please ask.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    37
    Errors, but atleast it wasnt with the Sleep()...yeah it works! O_O So does the system("cls") XD Dunno how system() functions are supposed to be bad, I use system("PAUSE") for all the new compilers, coz they close the window as soon as they detect that there are only output lines left - though I could just as well write getch(); -_-
    Thanks a lot for the help ^_^

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    System calls are unsafe and hackable. You can use cin.get() in C++ or in C use getchar()
    I'm just trying to be a better person - My Name Is Earl

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    The main problems with system() are that it is unportable and unsafe.

    It's unportable because you don't know if a given system has the command that you're invoking. For example, there's no such command as PAUSE on UNIX systems, so if a system("PAUSE") call was executed on such a system, at best, nothing would happen.

    It's unsafe because you don't know exactly what it is that you're executing. If you used system("ls"), and someone created an executable called ls in a location in the path before the real ls, their ls would be executed. This ls could do anything. It could create a security hole.

    There are other problems with system(): for example, it uses the user's default shell. If this shell is customized or incompatible with the shell you were expecting, there could be problems.

    It's best to simply avoid system() if possible, and in the case of keeping a Windows console open it's certainly possible.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    It's unsafe because you don't know exactly what it is that you're executing. If you used system("ls"), and someone created an executable called ls in a location in the path before the real ls, their ls would be executed. This ls could do anything. It could create a security hole.
    I think this is rather theoretical though. If the user's system has been compromised so badly, chances are you cannot really make it very much worse. After all, the user himself/herself might have invoked a planted executable on their way to using your program.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Writing safe code is a habit, not an afterthought based on the supposed runtime environment.

    If you avoid risky constructs from the outset, then your code will be better when it matters.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User
    Join Date
    Sep 2007
    Posts
    9
    I've always preferred using this simple function:
    Code:
    void pause()
    {
        std::cout << "Press [Enter] to continue...";
        std::cin.ignore( 256, '\n' );
    }
    as far as clearing the screen...

    I guess whatever OS you're using should decide the best way to handle that.

    If you're looking for a universal solution, one could use PDCurses...

    But if you're writing simple console programs for school or whatever, use system(), who cares, it's not like you're deploying multi-million dollar software.

    If you're using a "pausing" feature to debug a program, (this might be OK for simple tings) but start using gcc/g++ (DevC++ is old and MinGW has its limitations) and its built-in debugging features.

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    37
    Quote Originally Posted by entropyrexor View Post
    If you're looking for a universal solution, one could use PDCurses...
    Whats PDCurses? o.o
    Quote Originally Posted by entropyrexor View Post
    If you're using a "pausing" feature to debug a program, (this might be OK for simple tings) but start using gcc/g++ (DevC++ is old and MinGW has its limitations) and its built-in debugging features.
    And whats gcc/g++?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Whats PDCurses?
    Public Domain Curses

    And whats gcc/g++?
    gcc is a C compiler, g++ is its C++ counterpart, and both are part of GCC. I am not entirely sure what entropyrexor means though, since on Windows gcc and g++ are available mainly either by the MinGW port, or Cygwin.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  2. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  3. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  4. Inline functions and inheritance
    By hpy_gilmore8 in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2004, 05:46 PM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21