Thread: Re gotoxy

  1. #1
    zach
    Guest

    Re gotoxy

    I see two versions of gotoxy - which one is correct / preferred ?

    Code:
    gotoxy(int x, int y) and gotoxy(short x, short y)
    {
    ...
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It's a non-standard function, so refer to the relevant documentation or header file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    zach
    Guest
    Quote Originally Posted by laserlight View Post
    It's a non-standard function, so refer to the relevant documentation or header file.
    I had searched the Net before posting. Couldn't find anything. Hence my post. Although your comment is well taken.

    Code:
    void gotoxy(short x, short y)  
     	{
     		COORD pos = {x, y};
     		SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
      	}
    


  4. #4
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    It depends on your usage and what you intend to do with the data.

    Difference between short, short int and int data types in C programming - IncludeHelp
    "without goto we would be wtf'd"

  5. #5
    zach
    Guest
    Quote Originally Posted by Structure View Post
    It depends on your usage and what you intend to do with the data.

    Difference between short, short int and int data types in C programming - IncludeHelp
    Yes, I needed ints, have used ints, and the program is running a treat. But saw shorts used, elsewhere on the Net. I wondered if, in the belly of c, where the code of gotoxy is doing its job, shorts would be preferable. I couldn't understand all the compiler was telling me. I am not pursuing the matter any further.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Do you know what an library is in C?

    The function prototype needs to match the library or other source code implementation of the function.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    zach
    Guest
    Quote Originally Posted by stahta01 View Post
    Do you know what an library is in C?

    The function prototype needs to match the library or other source code implementation of the function.

    Tim S.
    What do you mean by having to match the library? And of other source code of the function? I would have thought that the correct types might depend on the Windows OS, and / or the code that is using gotoxy - i.e., - the code passing values to the function. The latter is obvious, the first is what my query is about. Although the code of the queried function is - as lasterlight says - non-standard, surely someone must be using gotoxy in a visual application, who doesn't want to go the Microsoft monopoly way.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I can not explain a library to a newbie.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Just thought of a newbie level explanation.

    An typical library consists of header(s) and either source code or binary file(s).

    The header must match the source code or binary file(s) or you will get weird build or run-time problems.

    The function prototype must match the function implementation! Or you will risk getting compiler/linker error or weird run-time errors!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    zach
    Guest
    Quote Originally Posted by stahta01 View Post
    I can not explain a library to a newbie.

    Tim S.
    Then wait with responding to till you have read an introductory book on C and in the mean time, don't trip over your ego.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by zach
    Yes, I needed ints, have used ints, and the program is running a treat. But saw shorts used, elsewhere on the Net. I wondered if, in the belly of c, where the code of gotoxy is doing its job, shorts would be preferable.
    There is no "belly of c"

    Since this function is non-standard, there are a few possibilities:
    • The function happens to be included with the default libraries that come with your compiler. This may be the case on the old Borland Turbo C/C++ compilers, possibly by way of the non-standard <conio.h>. In this case, to figure out what are the types of the parameters, you would either refer to the documentation that comes with the compiler, or you would track down the header file in which gotoxy is declared. These are your two sources of truth as to what is the preferred (or even more strongly, what is the correct) signature of gotoxy.
    • You happened to install a library that defines this function. Like in the case where gotoxy is included with the compiler's default libraries, you would either refer to the documentation that comes with the library, or you would find the header file in which gotoxy is declared.
    • You wrote the function yourself. Of course, in this case you would rightly ask: am I declaring gotoxy correctly? For this, I might look at the implementation you proposed:
      Code:
      void gotoxy(short x, short y)  
      {
          COORD pos = {x, y};
          SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
      }
      A quick check of the documentation for windows.h shows that COORD is a struct of two SHORT objects, where SHORT is presumably an alias for short. Therefore, the short parameters are preferable to the int parameters. But again, this is only true for this particular implementation: if you happen to be using an old Borland compiler and the gotoxy that comes with its default libraries use int parameters, taking the parameters to be short would be wrong. Furthermore, while short is preferable because it matches COORD, you would very well choose to declare your version of gotoxy with long parameters and just do something to make it fit the particular implementation, e.g., some combination of modulo, casting, and/or error checking.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    zach
    Guest
    Re laserlight Hi! This is a second post trying to get past so called errors in my submission.

    Yes I was declaring the function and indeed wondered what the best parameters were from the point of view of the innards of the function.

    I now presume that my choice should have been for shorts (and, that of course, my approaching code should have been in keeping with that).
    And also, that this explains the narrowing down remark of my compiler.

    Thank you for your comprehensive reply and apologies for snipping.

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by zach View Post
    Then wait with responding to till you have read an introductory book on C and in the mean time, don't trip over your ego.
    YOU READ A BOOK ON C YOU ARE THE IGNORANT ONE!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Come on folks, no need to insult each other like that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    zach
    Guest
    Quote Originally Posted by laserlight View Post
    Come on folks, no need to insult each other like that.

    Laserlight, I find it difficult to contain myself when people are being rude to me. Yet a lot to learn, I fear. The program you helped me with, which I used to learn C, turned out beautifully.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to use gotoxy()??
    By Beginnerinc in forum C Programming
    Replies: 5
    Last Post: 08-15-2010, 12:23 PM
  2. gotoxy
    By BEN10 in forum C Programming
    Replies: 4
    Last Post: 08-03-2009, 12:11 PM
  3. What is gotoxy??
    By Beachblue in forum C Programming
    Replies: 6
    Last Post: 11-23-2008, 01:24 AM
  4. gotoxy
    By Sekti in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2002, 07:00 PM
  5. Using gotoxy
    By Esparno in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 08:20 AM

Tags for this Thread