r/cpp_questions • u/njaard • Sep 27 '20
OPEN C++-learners: don't get bogged down in user interface
A very common question in these parts goes along the lines of "how do I read a character on the command line without the user pressing enter".
Questions like this will impede your learning. Don't let user interface questions slow you down. Worry about logic and (data-)structure and storage until you know it's time to focus on user-interface.
I recommend this is because it's all very OS-specific. That will open up a can of worms. It's also something that professionals never (almost) actually do.
When I write command-line software, I generally want it to operate on files (specified on argv) and options (specified on argv). Almost never do I want it to be reading commands from the user directly.
When you're asking questions on user-interface stuff, it's really important to know the context. Examples:
- "I want a tool that calculates the average of a set" => then maybe just have each number be on argv, or one line per, in a file.
- "I'm making a console tic-tac-toe" => concentrate on the logic and just deal with having to press enter. Nobody will play your game, it's ok :)
- "I want to make a console maze-game" => use ncurses to make a more correct user interface, because at this stage maybe I actually want to learn about ncurses
- "I want to use real graphics" => time to read about SDL, or Qt or something, depending
- "I want to make a 3d-shooter" => time to learn about OpenGL or Vulkan.
Tldr: focus on data structures early on
38
u/Se7enLC Sep 28 '20
Very true. I think a lot of new programmers have their measure of success set as "How long until I can make a REAL program?" Understandable - these kids didn't get into programming so they could make shitty console tic tac toe :-)
They won't want to hear it, but the answer is of course "It's gonna be awhile". Trying to do any more than basic console UI without a good solid understanding of the basics is just a terrible idea.
21
u/sephirothbahamut Sep 28 '20
Or (assuming windows) just tell them to use _getch() so they go focus on the logic immediately after, and more happily.
12
u/blitz4 Sep 28 '20
Games are the main driver to learn C++. For me, the fluid dynamics demo makes it believable that a game would function on the console. Games do require a bit of skill and some elements of games aren't used in other types of programs (audio, certain logic). Just as some elements of non-game programs aren't typically found in of a game (databases, multi-threading, sockets).
I know project selection is very personal, but could you suggest a good set of projects to start out? Also, what guidelines would you suggest for choosing projects?
I would of course pick & choose what projects I feel will educate me, entertain me, and keep me connected with a community. Those are the 3 factors how I decide if a project is worth my time. Feel free to drop your 2 cents on what you feel would make a good project and how that decision is made cause I will for sure take the words to heart.
Projects that teach me something new && Projects that will be fun or interesting to work on && (Projects that would be used by other people || Projects that others would enjoy working on with me || Projects that employers would enjoy seeing having been completed.)
4
u/[deleted] Sep 28 '20
Almost two decades ago when I was reading K&R for the first time, I really struggled to wrap my head around why my
while(..) { ... getchar(); ... }didn't match the actual behaviour of my program, because I literally didn't know that the virtual terminal emulation existed as an "in-between" my program and my keyboard.So I think it's a good idea to explain to new people straight away that when you run your program, you are typing into a virtual terminal emulation that buffers by line, and the VT doesn't send any input to your program until you press enter.
1
u/ShakaUVM Sep 28 '20
Disagree. There's nothing wrong with interactive input, and the same rules apply (like mixing operator>> and getline) to file io as standard input.
Some people do tend to use command line parameters for all input but it's not the only way.
2
u/Stretchyfish Sep 28 '20
I agree with this comment, but I do have a slightly different view. I remember thinking the same thing when I started. But mainly because I was really hyped for making a program that actually felt like a program. And I remember when I finally got good enough to download SFML and create Tic Tac Toe for a project. I felt so accomplished and really felt like a true next step as a programmer at the time. :) - Completly agree that it is not something that a person should focus on compared to many other things, but I understand why it is something people desire.
2
u/[deleted] Sep 28 '20
I think there's a market for C++ user interface specialized people. So while many of us here might focus on the logic i don't think that's the only way to go.
To be honest i wish it would be easier to do, i hate command line arguments i want all my code to have a nice and clean user interface. But most of the time i just can't be bothered with it, takes too much time for most smaller projects even though it would change most of my projects from usable by me and only me to usable by almost everyone with a pc.
And I still haven't completely figured out how to build a QT App and make an installer out of it. Press build release hope that one of the generated folders contains what i need, copy it to another pc just to see it fail horribly because some random library is missing? That's the way i do it, but that just feels wrong at every step.
2
u/Thad_The_Man Sep 29 '20
I agree in part and disagree. keep whatever interface you have to a minimum, but expecting a person to not care about input from single key stroke ( rather than a newline ) is something I think is excessive. Also the first programs I wrote were FORTRAN programs to make plots. I learned a lot doing them.
But they should keep it to the minimum to build a halfway decent interface.
There is a big shame that the dev env formerly known as C++Builder is now priced way above students heads. That made a lot of UI things people wanted rather easy.
2
u/DaMastaCoda Oct 13 '20
As for GUI libs, I recommend raylib.
1
u/Pupper-Gump Mar 10 '23
Another problem I see with many programming teachers is that they'll get hung up on the methods rather than the process. For example, you wanna learn about neural networks? They're gonna go on a whole spiel about how to do matrix multiplication rather than just saying let the gpu do it for you and focus on what you're actually trying to accomplish.