Error with gotoxy in code::block compiler

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 3752 Views - Last Post: 21 November 2012 - 09:49 PM Rate Topic: -----

#16 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Error with gotoxy in code::block compiler

Posted 20 November 2012 - 06:35 PM

If you rename it gotoxy(), you could use it to run programs made for conio.h, as well.

For now, put on your dancing shoes...
#include <stdio.h>
#include <windows.h>


void Gotoxy(int x, int y);

int main(void) {
   int i,x, y;

   
   for(i=0;i<20;i++) {
      x=rand() % 60 + 1;
      y=rand() % 30 + 1;
      Gotoxy(x,y);
      printf("<< Let's dance! >>");
   }

   printf("\n");
   return 0;
}
void Gotoxy(int x, int y) {
  COORD coord;
  coord.X = x;
  coord.Y = y;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}



Was This Post Helpful? 0
  • +
  • -

#17 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,225
  • Joined: 31-December 10

Re: Error with gotoxy in code::block compiler

Posted 21 November 2012 - 08:01 AM

View PostAdak, on 20 November 2012 - 08:08 PM, said:

You'd be surprised how many modern compilers still support conio.h, btw. It's well over 50%.
(but this will be straight windows.h, not conio.h, and I'll call the function Gotoxy() instead of gotoxy(), to differentiate it.

It doesn't matter if a compiler supports it or not, if you want to write portable applications then you should avoid using conio.h at all. Just out of curiosity, what compiler are you using Adak?
Was This Post Helpful? 0
  • +
  • -

#18 jimblumberg  Icon User is online

  • member icon


Reputation: 3989
  • View blog
  • Posts: 12,307
  • Joined: 25-December 09

Re: Error with gotoxy in code::block compiler

Posted 21 November 2012 - 08:20 AM

Quote

if you want to write portable applications then you should avoid using conio.h at all.

But since positioning the cursor is operating system specific using this operating system specific header is not necessarily an issue. But I agree it's use does pose several portability issues, one of which is that this header could be different from one compiler to another. And few of the modern compilers support the gotoxy() or _gotoxy() function in conio.h.

Also note that even the latest Microsoft compiler and many of the Windows based compilers support this header, for use in console only programs. Most if not all support things like getch(), _getch(), kbhit(), _kbhit() and several other console only routines.

Jim

This post has been edited by jimblumberg: 21 November 2012 - 08:22 AM

Was This Post Helpful? 1
  • +
  • -

#19 c++ultimate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-November 12

Re: Error with gotoxy in code::block compiler

Posted 21 November 2012 - 08:49 AM

I'm Using a code::blocks compiler on the Windows OS
Was This Post Helpful? 0
  • +
  • -

#20 jimblumberg  Icon User is online

  • member icon


Reputation: 3989
  • View blog
  • Posts: 12,307
  • Joined: 25-December 09

Re: Error with gotoxy in code::block compiler

Posted 21 November 2012 - 08:57 AM

Did you try the solution provided by Adak in post #16? You will probably need to add that gotoxy() function to your code because your compiler doesn't support that function.

Jim
Was This Post Helpful? 0
  • +
  • -

#21 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Error with gotoxy in code::block compiler

Posted 21 November 2012 - 02:07 PM

View Postvividexstance, on 21 November 2012 - 08:01 AM, said:

View PostAdak, on 20 November 2012 - 08:08 PM, said:

You'd be surprised how many modern compilers still support conio.h, btw. It's well over 50%.
(but this will be straight windows.h, not conio.h, and I'll call the function Gotoxy() instead of gotoxy(), to differentiate it.

It doesn't matter if a compiler supports it or not, if you want to write portable applications then you should avoid using conio.h at all. Just out of curiosity, what compiler are you using Adak?


I use Pelles C 64 bit, on Windows 7 64 bit, currently. However, I have two other compilers here (Microsoft and Borland), that also support conio.h).

When you say "it doesn't matter if a compiler supports it or not", you're mis-speaking. There is NO way to implement moving a cursor to (x,y), using portable C code, because C doesn't standardize functions for output devices. It stays "above" that level of implementation.

Which is exactly why conio.h was created in the first place. There was a need, and conio.h helped fill it. If you are a practical person, you have to give it props for that.

This post has been edited by Adak: 21 November 2012 - 02:08 PM

Was This Post Helpful? 0
  • +
  • -

#22 jimblumberg  Icon User is online

  • member icon


Reputation: 3989
  • View blog
  • Posts: 12,307
  • Joined: 25-December 09

Re: Error with gotoxy in code::block compiler

Posted 21 November 2012 - 02:22 PM

Is the gotoxy() or _gotoxy() function supported in Microsoft C? It appears to be supported by Pelles C.

Jim

This post has been edited by jimblumberg: 21 November 2012 - 02:23 PM

Was This Post Helpful? 0
  • +
  • -

#23 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Error with gotoxy in code::block compiler

Posted 21 November 2012 - 08:35 PM

Yes, _gotoxy() is part of Pelles C. Microsoft has gone all Windows style now (no surprise there), so the newer MS C/C++ compilers would need to use SetConsoleCursorPosition(), like the example above.

I'm not sure whether code::blocks has support for conio.h or not, even if he downloaded a library from Borland. Just stick with the Windows equivalent as shown above.

Vividexstance is right about the fact that I need to move over to more of the Windows standard, I've gotten lazy on that. When you've used the functions in conio.h for 20 years+, you get comfortable with it, and of course, lots of program you wrote years back, used it - you're invested in it. I bristle when someone comes along and disparages it - but I shouldn't. Time and technology marches on.

This post has been edited by Adak: 21 November 2012 - 08:37 PM

Was This Post Helpful? 1
  • +
  • -

#24 jimblumberg  Icon User is online

  • member icon


Reputation: 3989
  • View blog
  • Posts: 12,307
  • Joined: 25-December 09

Re: Error with gotoxy in code::block compiler

Posted 21 November 2012 - 09:49 PM

I don't think that gcc supports the conio.h header in Windows. And since Code::Blocks normally uses gcc you probably won't have access to any of the functions contained within it. This is why conio.h is problematic and should be avoided, the inconsistencies as to what items are provided by this non-standard header, if the header is actually provided by your compiler. However the snippet you provided should work with any compiler that targets the Windows environment.

But until the institutions of "higher" learning stop teaching these old functions and using ancient compilers it will be difficult to avoid this problem.




Jim
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2