I see two versions of gotoxy - which one is correct / preferred ?
Code:gotoxy(int x, int y) and gotoxy(short x, short y) { ... }
I see two versions of gotoxy - which one is correct / preferred ?
Code:gotoxy(int x, int y) and gotoxy(short x, short y) { ... }
It's a non-standard function, so refer to the relevant documentation or header file.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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"
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.
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
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.
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
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
There is no "belly of c"Originally Posted by zach
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:
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.Code:void gotoxy(short x, short y) { COORD pos = {x, y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
"...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
Come on folks, no need to insult each other like that.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)