I've been looking around because I know how getch() isn't standard and I wanted to get started on making everything in my code a little more standard. I read http://cplusplus.com/articles/yAUq5Di1/ and the code works really well, with the exception of arrow keys, it just returns one value, the 0. Is there anyway to get the secondary value of the arrow keys? or some alternative to checking for arrow keys? Currently, I check to see if an arrowkey was pressed with getch() and then check the second value again.
Seems like you can't by just using only standard c++.
Absolutely true (well, it's standard C++ but it using libraries not described in the standard); C++ (very deliberately) does not come with any awareness of keyboards or monitors or sound or any of that sort of thing.
On the downside (although I really don't see it as a downside) you have to learn/use something a bit different for different systems to use these things (although of course cross-platform kits hide the different API from you, giving you a nice common interface). On the upside, because there is nothing mandated, everything is possible.
By leaving that final step completely open (and indeed, in the majority of the world's processors there is no keyboard or monitor or sound or anything like that), C++ is fantastically adaptable. Compare with languages that insist of a keyboard, or need a VM just to run on (I'm not saying better/worse - I'm saying meant for different things, and designed with different principles).
Thanks for the read, and it gives me more insight to what I originally thought was going on. And I figured any of my code that used windows.h would only work on windows anyways, but I am really interested in seeing if there is a way to make it portable. I've read up on some portability from Duoas, and they're great reads, but without having a *nic system to test it, idk how well it works.
Edit: I've seen a lot about ncurses and quite frankly it seems just as portable as standard libraries, but is it standard? Would everyone have to download the ncurses library just to use my functions? Would it be able to everything I want it to do?
If you're keen on making your code portable, or just want to experiment with it (always an excellent idea) consider getting one of the free VM programs (I like VirtualBox - https://www.virtualbox.org/wiki/Downloads - but of course there are others) and install a Linux on it. You can then easily build your code in there, as well as on your primary Win OS.
I have had Linux on my system before, redhat I believe it was, and I liked it, but I didn't feel comfortable. Now, would everything I program in there be as if I was running Linux directly? Would it be a better idea to run Linux on another hard drive instead? Or would the virtual machine allow me to switch back and forth faster? New to using it, obviously.
But I guess the biggest question is, if my program works great in the virtual machine, would it work exactly the same in all other *nix OS's? Mac, unix, etc.?
The VM is not perfect, but as an approximation of a separate PC running the Operating System, it's really very good. It would, as a general rule, be as if you were running Linux on a separate PC. You can map shared directories across your host OS and the virtual one, and from there easily build in the two separate OSes.
But I guess the biggest question is, if my program works great in the virtual machine, would it work exactly the same in all other *nix OS's? Mac, unix, etc.?
No. There is no such guarantee; there is no such thing as software that works the same everywhere. The best you're going to ever manage is to pick a few target OSes, keep your code as standard as possible, use cross-platform libraries, and make sure you test on a good range of target systems.
So you're saying, if it's not standard to C++, it can't easily be done without a good bit of trial and error and without doing research for each OS? I can imagine standard libraries can do a wide range of things, but where do I find a complete list of what exactly is standard? What is the best way to deal with none standard things? Is there one? Keep all none standard functions in a separate header and cpp file until I can get back to them to make them portable?
I can imagine standard libraries can do a wide range of things, but where do I find a complete list of what exactly is standard?
The definitive reference is the ISO C++ standard. Each C++ compiler strives to meet that standard. I wouldn't be surprised if there were copies of it floating around on the internet.
What is the best way to deal with none standard things? Is there one?
I've seen (and used) various approaches over time. Sometimes people do things like
1 2
#ifdef WIN32
// Windows specifics
Sometimes people have different makefiles for each operating system. I'm sure there are more ways. If you go mooching about google looking for ideas, you'll find some.
If you stick to the standard, no worries - of course, that means no graphics, no mouse, no sound, and the input from the keyboard limited to what you can get through the existing input streams.
You could do worse than just pick one of the cross-platform widget kits (http://en.wikipedia.org/wiki/List_of_widget_toolkits#Cross-platform). It will take care of all that sort of thing for you, and you just have a different installation library for each target system. Your code is unchanged.
Wow, never realized there were so many third party toolkits. Anything specifically you recommend? I've seen Qt a lot, but would this cover a nice console interface that I would like? Mainly want things that allow for console manipulation like clear screen, something similar to getch that works the same on each of the three big OS's, and maybe some other features.
I assume all these toolkits are is basically the same thing I would have done to make certain code multi platform, much faster and more professionally done. So another question pops into my mind, is it worth all of this trouble since its not standard? What are the biggest downsides to using a toolkit aside from individual system installs and learning a new library?
QT gets a good press, for good reason, although to my mind it's become too much of a programming environment in its own right (QT creator etc). I used to use wxWidgets a lot.
If you just want console interaction, these are overkill and you might find the best way is just to identify and wrap the actual console calls in platform dependent form, and push out the appropriate console library with each version.
I understand ncurses (nix) and pdcurses (win, and maybe nix as well, actually) use the same function names, so you wouldn't even need to do anything different with your code - just have a different build script for each target.
Is curses now standard? I've been seeing it pop up a lot, but I don't believe the libraries came with the compiler. I started doing research with them, but I didn't want to jump in too far just to find out I was learning a bad practice. And when you say the code wouldn't be any different, you mean I'd just need #include <pdcurses.h> for windows and #include <ncurses.h> for *nix?
Drawbacks using curses?
And currently I've been trying to move all none standard code into a header file and keep it with all of my applications when I need none standard functions, but at this rate it seems like I'm getting more window dependent than actually getting more portable. I'm just worried about doing platform dependent calls simply for the fact that I'm comfortable with windows and there is still a lot of things I don't know about windows specific C++, let alone everything that I'd need to pick up in a new OS.
Btw, I greatly appreciate taking the time to explain all of this, reading websites becomes very overwhelming to me since a lot of them are long winded and not exactly easily read.
Edit: I don't JUST want console interaction, I feel there are a good bit of things that should be simpler, or a better way to do certain things, atleast in my mind, like I believe there should be a division function that divides two ints and returns a float/double/double double, but again, that's just my opinion. Plus if there is a toolkit that offers what I want, I'd be more encouraged to learn about all of the other goodies it has to offer, expand what I can do in a portable environment.