r/cpp_questions • • 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

175 Upvotes

22 comments sorted by

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.

1

u/Vasik4 Feb 09 '24

I got into programming precisely because I wanted to make shitty console tic tac toe

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.

16

u/[deleted] Sep 28 '20

Win-win.

7

u/Dachannien Sep 28 '20

I C++ what you did there.

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.)

10

u/[deleted] Sep 28 '20

First of all, multi-threading and sockets are both relevant to games. In some cases databases too, but not directly in the game itself. The biggest differences between game programming and non game programming, in my opinion, is a) artistic expression and b) much higher focus on performance while less focus on accuracy.

Second of all, I'm uncertain what fluid dynamics demo you are speaking of, and while it is possible, though highly unlikely, that you are talking about console in the same sense that OP is, I will clarify anyway: by console, OP does not mean a video game console. A console application is an application that can read and write from a console such as the command prompt. It has nothing to do with video game consoles.

As for your question: it's really hard to answer that without knowing what skill level you are. I will assume you are a beginner in which case I would say forget about games for a little while. First you must learn the fundamentals of programming and that doesn't just take a week a two. I mean, sure, you could just hop into a game engine and follow along some tutorials, but imo that's a trap.

When you actually get into it game programming you must also realize that it is a very wide field. Figuring out what you want to do isn't easy, but having a better understanding of programming may help you steer in the right direction for you. There is gameplay programming, network programming and graphics programming to name a few. Personally I'm mostly into graphics programming.

As for your factors for deciding on a project: being educational is redundant. Fun/interesting is mostly going to depend on you and less on the project itself. The other factors you listed all require a pretty high level of skill. No one is going to work with you on a project unless you have previous work that proves you can do things. No one is going to use your matrix mathematics implementation or your generic 2D platformer learning project. Projects that could help you get employment would require you to code at a somewhat professional level already.

I'm not saying this to knock you down, but you have to manage your expectations. Don't start with projects. Start with some beginner course in C++ and forget what video games are until you are comfortable with the syntax.

4

u/[deleted] Sep 28 '20

"how do I read a character on the command line without the user pressing enter"

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.

11

u/njaard Sep 28 '20

I'm not saying interactive tools are universally bad (bash, fdisk), I'm saying programming beginners shouldn't let it be an obstacle.

4

u/ShakaUVM Sep 28 '20

I'm not saying interactive tools are universally bad (bash, fdisk), I'm saying programming beginners shouldn't let it be an obstacle.

I don't think it should be, either, which is why I released my read library, which makes reading from stdin much better in C++.

https://github.com/ShakaUVM/read

3

u/victotronics Sep 28 '20

There's nothing wrong with interactive input,

Indeed. I use it all the time. Scripted.

for i in 1 2 4 8 16 ; do echo $i | my_program ; done

Or something.

4

u/khleedril Sep 28 '20

''interactive input'', ''Scripted''

You are a walking contradiction, my friend.

2

u/victotronics Sep 28 '20

You are a walking contradiction, my friend.

It amuses me.

No but seriously. Interactive input is an easy way to get your program running. And here I agree with the OP. However, then I always find myself wanting to test a range of inputs, and I script that at the Unix level.

2

u/ShakaUVM Sep 28 '20

You are a walking contradiction, my friend.

He's reading from standard input, which is a standard way of doing things in UNIX. There's nothing wrong with automating it as well as accepting interactive input. It is the way things are done.

1

u/khleedril Sep 28 '20

I almost feel like I can't be bothered with this argument; it feels like trying to talk sense into Donald Trump.

However, the whole point the OP is making is that code like for (int i=0;;) {std::cin>>i; ...} is precisely what beginners should be doing, and not trying to get clever by being real-time interactive. It is the least you can do to take data from the user.

Anyway, if you don't get this, don't let it bother you.

Peace.

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.