What is the relationship between #include <conio.h> and getch() method? - c++

I would like to know about this.
the relationship between #include and getch() method

What is the relationship between #include and getch() method?
getch() is declared inside the header file of conio.h, so you need to set the preprocessor directive #include <conio.h> in order to you use getch(). Without #include <conio.h> you can not use getch().
Note: conio.h is not supported by all C/C++ implementations and is not part of the C standard libraries. Rather use getc() or getchar() for catching a character from the input if you want to compile C or C++ code on Unix or Linux.
Your comment:
Mam, thank you for pointing out me. But, it would be very helpful to understand if you put some clarification of using it, mam.
Beside the case, that this is not the question you have asked for, this is not how Stack Overflow works. This is not a tutorial platform; it is a platform to ask for help if you did not understand a certain topic. You need to declare what you did not understand and we can help you.
If you look for a tutorial about getch(), here is one:
https://www.geeksforgeeks.org/difference-getchar-getch-getc-getche/
By the way, I´m not a Mam, but Ok.

Related

Using swap() without #including its library? [duplicate]

This question already has answers here:
C++ math functions can be used without including the directive "math.h" in VS 2013
(2 answers)
Closed 8 years ago.
I have a simple program in which I arrange the elements of an int array in ascending or descending order, and I use the swap() function to move the elements around. I compiled the program without any errors, and it ran like a charm. I only noticed afterwards that I had forgotten to #include the library that swap() is defined in (<algorithm>, or <utility> as of C++11) before I compiled.
Why did it still work? The top of my program looked like this:
#include <iostream>
#include <cstdlib>
using namespace std;
I tried taking out <iostream>, just to see what would happen, and it predictably put out a bunch of 'cout/cin/endl' was not declared in this scope errors, but I was surprised to see that it gave some 'swap' was not declared in this scope errors as well. Does that mean swap() is defined in <iostream>? I don't think it should be, should it?
Anyways, this is probably a big long question for a simple answer, but I'm pretty curious. I'm still learning C and C++, so I don't know a lot of things, and I couldn't find an answer to this particular mystery via the "Almighty" Google Machine, so here I am.
Thanks in advance!
Generally, do NOT rely on the header files that includes other header files.
Always include and only include the header files you need.
For example, if you want to use std::swap(), Google it and you'll see if requires <algorithm> in c++98 and <utility> in c++11, so you should include the file to make sure your code compiles.

Where is getchar() besides cstdio?

I'm taking my first c++ class (and I'm in week 6.)
I used getchar(), which according to every reference I can find, is located in cstdio (or stdio or stdio.h.) Just to see what would happen I commented out
#include <cstdio>
Much to my surprise, my program still ran without errors. Other libraries I included are: algorithm, cstdlib, iostream, and string. I take it that getchar() is a part of one of these other libraries, but searching the internet, I don't see any reference that mentions any non-cstdio-like library.
Is cstdio and cstdlib the same thing?
Is there a definitive reference for what libraries hold each method/command?
Thank you for tolerating my noob questions. ~d
The answer is that one of your other header files is also including <cstdio> or it's equivalent (I would guess <iostream>).
Including <cstdio> is the right thing to do. If you don't then you might find that your code stops compiling when it's used with a different compiler.
BTW header files are not libraries, and the definitive references for what is found in which header file are the C++ and C standards documents.
Also BTW this kind of experimentation is exactly the sort of thing you should be doing as a new C++ programmer.

#include confusion and classes

I have been making several games with the Allegro API and C++. I have also been putting all my classes in 1 big main.cpp file. I tried many times to make .h and .cpp files, but my big problem is I have trouble with #including at the right place. For example, I want all my classes to access the allegro library without #including allegro.h everywhere. Could someone please explain how to correctly #include things. In .Net, everything seems to come together, but in c++ one thing cannot be used before it is included. Is there also a way to globally include something throughout my entire program?
Thanks
I want all my classes to access the allegro library without #including allegro.h everywhere.
Why? That is how you do it in C++ land.
Could someone please explain how to correctly #include things. In .Net, everything seems to come together, but in c++ one thing cannot be used before it is included
Conceptually, in .NET, it is not much different at all. You still have to place "using " at the top. The difference there is that, in .NET, you could also write this every time if you wanted to:
void Foo( System.Drawing.Drawing2D.BitmapData bData ) { }
A common way to do this is to have a master include file that includes all of the others in the correct order. This works especially well if you use precompiled headers so
in precomp.h
#include <stdio.h>
#include <allegro.h>
.. etc.
in myfile.cpp
#include "precomp.h"
in myfile2.cpp
#include "precomp.h"
and so on.

tempnam equivalent in C++

I need to generate random names which I'll be using to create temporary files in a directory. Currently I am using C standard function tempnam() for this. My code is in C++ and would like to use C++ equivalent for doing the same task. The code needs to work on Solaris as well as on Windows.
Is anyone aware of such thing in C++? Any pointer on this would be highly appreciated.
Try std::tempnam in the cstdio header. ;)
The C standard library is still available in C++ code. For convenience, they provide C++ wrappers (in headers with the 'c' prefix, and no extension), and available in the std namespace.
You can also use the plain C version (stdio.h and tempnam in the global namespace, but you did ask for the C++ version ;))
The C++ standard library only provides new functions when there's actually room for improvement. It has a string class, because a string class is an improvement over char pointers as C has. It has a vector class, because, well, it's useful.
For something like tempnam, what would C++ be able to bring to the party, that we didn't already have from C? So they didn't do anything about it, other than making the old version available.
I know this doesn't answer your question but as a side note, according to the man page:
Although tempnam(3) generates names
that are difficult to guess, it is
nevertheless possible that between the
time that tempnam(3) returns a
pathname, and the time that the
program opens it, another program
might create that pathname using
open(2), or create it as a symbolic
link. This can lead to security
holes. To avoid such possibilities,
use the open(2) O_EXCL flag to open
the pathname. Or better yet, use
mkstemp(3) or tmpfile(3).
Why not just using the same function you are currently using in C? C++ is backward compatible with C.
What's wrong with tempnam()? You can use regular libc function right? tempnam is in stdio.h, which you're likely already including.
#include <cstdio>
using std::tmpnam;
using std::tmpfile;
You should also check this previous question on StackOverflow and avoid race conditions on creating the files using mkstemp, so I would recommend using std::tmpfile

Other's library #define naming conflict

Hard to come up with a proper title for this problem. Anyway...
I'm currently working on a GUI for my games in SDL. I've finished the software drawing and was on my way to start on the OpenGL part of it when a weird error came up. I included the "SDL/SDL_opengl.h" header and compile. It throws "error C2039: 'DrawTextW' : is not a member of 'GameLib::FontHandler'", which is a simple enough error, but I don't have anything called DrawTextW, only FontHandler::DrawText. I search for DrawTextW and find it in a #define call in the header "WinUser.h"!
//WinUser.h
#define DrawText DrawTextW
Apparently it replaces my DrawText with DrawTextW! How can I stop it from spilling over into my code like that?
It's a minor thing changing my own function's name, but naming conflicts like this seem pretty dangerous and I would really like to know how to avoid them all together.
Cheers!
You have a couple of options, all of which suck.
Add #undef DrawText in your own code
Don't include windows.h. If another library includes it for you, don't include that directly. Instead, include it in a separate .cpp file, which can then expose your own wrapper functions in its header.
Rename your own DrawText.
When possible, I usually go for the middle option. windows.h behaves badly in countless other ways (for example, it doesn't actually compile unless you enable Microsoft's proprietary C++ extensions), so I simply avoid it like the plague. It doesn't get included in my files if I can help it. Instead, I write a separate .cpp file to contain it and expose the functionality I need.
Also, feel free to submit it as a bug and/or feedback on connect.microsoft.com. Windows.h is a criminally badly designed header, and if people draw Microsoft's attention to it, there's a (slim) chance that they might one day fix it.
The good news is that windows.h is the only header that behaves this badly. Other headers generally try to prefix their macros with some library-specific name to avoid name collisions, they try to avoid creating macros for common names, and they try avoid using more macros than necessary.
It's an unfortunate side effect of #includeing <windows.h>. Assuming you're not actually using Windows' DrawText() anywhere in your program, it's perfectly safe to #undef it immediately after:
// wherever you #include <windows.h>, or any other windows header
#include <windows.h>
#undef DrawText
There is no general way of avoiding this problem - once you #include a header file using the preprocessor it can redefine any name it likes, and there is nothing you can do about it. You can #undef the name, but that assumes you know the name was #defined in the first place.
Just #undef the symbols you don't want. But Make sure that you include windows.h and do this before you include SDL:
#include <windows.h>
#undef DrawText
#include <SDL/SDL_opengl.h>