r/cpp_questions • • Sep 01 '25

META Important: Read Before Posting

189 Upvotes

Hello people,

Please read this sticky post before creating a post. It answers some frequently asked questions and provides helpful tips on learning C++ and asking questions in a way that gives you the best responses.

Frequently Asked Questions

What is the best way to learn C++?

The community recommends you to use this website: https://www.learncpp.com/ and we also have a list of recommended books here.

What is the easiest/fastest way to learn C++?

There are no shortcuts, it will take time and it's not going to be easy. Use https://www.learncpp.com/ and write code, don't just read tutorials.

What IDE should I use?

If you are on Windows, it is very strongly recommended that you install Visual Studio and use that (note: Visual Studio Code is a different program). For other OSes viable options are Clion, KDevelop, QtCreator, and XCode. Setting up Visual Studio Code involves more steps that are not well-suited for beginners, but if you want to use it, follow this post by /u/narase33 . Ultimately you should be using the one you feel the most comfortable with.

What projects should I do?

Whatever comes to your mind. If you have a specific problem at hand, tackle that. Otherwise here are some ideas for inspiration:

  • (Re)Implement some (small) programs you have already used. Linux commands like ls or wc are good examples.
  • (Re)Implement some things from the standard library, for example std::vector, to better learn how they work.
  • If you are interested in games, start with small console based games like Hangman, Wordle, etc., then progress to 2D games (reimplementing old arcade games like Asteroids, Pong, or Tetris is quite nice to do), and eventually 3D. SFML is a helpful library for (game) graphics.
  • Take a look at lists like https://github.com/codecrafters-io/build-your-own-x for inspiration on what to do.
  • Use a website like https://adventofcode.com/ to have a list of problems you can work on.

Formatting Code

Post the code in a formatted way, do not post screenshots. For small amounts of code it is preferred to put it directly in the post, if you have more than Reddit can handle or multiple files, use a website like GitHub or pastebin and then provide us with the link.

You can format code in the following ways:

For inline code like std::vector<int>, simply put backticks (`) around it.

For multiline code, it depends on whether you are using Reddit's Markdown editor or the "Fancypants Editor" from Reddit.

If you are using the markdown editor, you need to indent every code line with 4 spaces (or one tab) and have an empty line between code lines and any actual text you want before or after the code. You can trivially do this indentation by having your code in your favourite editor, selecting everything (CTRL+A), pressing tab once, then selecting everything again, and then copy paste it into Reddit.

Do not use triple backticks for marking codeblocks. While this seems to work on the new Reddit website, it does not work on the superior old.reddit.com platform, which many of the people answering questions here are using. If they can't see your code properly, it introduces unnecessary friction.

If you use the fancypants editor, simply select the codeblock formatting block (might be behind the triple dots menu) and paste your code into there, no indentation needed.

import std;

int main()
{
    std::println("This code will look correct on every platform.");
    return 0;
}

Asking Questions

If you want people to be able to help you, you need to provide them with the information necessary to do so. We do not have magic crystal balls nor can we read your mind.

Please make sure to do the following things:

  • Give your post a meaningful title, i.e. "Problem with nested for loops" instead of "I have a C++ problem".
  • Include a precise description the task you are trying to do/solve ("X doesn't work" does not help us because we don't know what you mean by "work").
  • Include the actual code in question, if possible as a minimal reproducible example if it comes from a larger project.
  • Include the full error message, do not try to shorten it. You most likely lack the experience to judge what context is relevant.

Also take a look at these guidelines on how to ask smart questions.

Other Things/Tips

  • Please use the flair function, you can mark your question as "solved" or "updated".
  • While we are happy to help you with questions that occur while you do your homework, we will not do your homework for you. Read the section above on how to properly ask questions. Homework is not there to punish you, it is there for you to learn something and giving you the solution defeats that entire point and only hurts you in the long run.
  • Don't rely on AI/LLM tools like ChatGPT for learning. They can and will make massive mistakes (especially for C++) and as a beginner you do not have the experience to accurately judge their output.

r/cpp_questions • • 4h ago

OPEN Is C++ worth it?

17 Upvotes

I've been wondering whether learning C++ would be worth it. I'm really interested in hardware design. I've even built my own CPU in Logisim, and now I'm looking to get into parallel programming and GPU computing.

I also enjoy programming highly efficient simulations and machine learning projects purely out of curiosity. Would C++ be a good investment for these interests, or would I be better off focusing on other languages?


r/cpp_questions • • 9h ago

OPEN how do header files work?

11 Upvotes

been trying to learn c++ recently, but ive been caught on header files. I have been told that they declare functions/variables, and including them basically makes the compiler copy/paste it above the code. however, i still dont quite understand it. How does the compiler know what to run in a function declared in a header file if there's no definition? My intuition tells me i need a .h file and a .cpp file along with the main file in order for it to work but i still dont understand how that would let the code run.

would i declare a function in the header file and then write the inside of the function in the cpp file?

like with the following code

//in blah.h

int sum(int a);

*********************

//in script.cpp

#include blah.h

int sum(int a)

{

return a+1;

}

******************

//in main.cpp

#include blah.h

#include <iostream>

int main()

{

std::cout << sum(1);

return 0;

}

*****************

would that compile?????????

and even so, how does the compiler read that?????

does it like know the declaration then search for the definition????


r/cpp_questions • • 48m ago

OPEN Rendering text through ImGui?

2 Upvotes

I'm hoping to get a basic repl running with ImGui, OpenGL, and GLFW. I'm comfortable with GLfW and OpenGL rendering but not the ImGui portion.

My question is, can I use the OpenGL context and the GLFW window and then get basic text editing functionality with just ImGui? No opengl buffers, no custom shaders. I think the opengl context needs a default vertex and fragment shader to function but I'm looking to keep that very minimal and handle basic repl input and text rendering between imgui::begin() and imgui::end(). A basic per keypress render loop where each GLFW key event updates a string and then ImGui renders a new frame for the window. I haven't worked with ImGui before. Do you think this is possible?


r/cpp_questions • • 14h ago

OPEN Learning to write video games from scratch

4 Upvotes

Hello everyone! This year I took a course on game development programming. I learnt alot of cpp basics, and eventually went on to write:

  • a couple classes from the Standard Templated Library
  • some trees and sorting algorithms
  • matrix and vector types for a raylib game (math as well)
  • a small raylib game with tanks
  • some enemy ai stuff such as pathfinding algorithims, a state machine & a behavior tree

eventually the course went into more Unreal Engine territory and I feel that as a C++ dev, it leaves alot to be desired. There's heeps I need to learn, and I want to also break it down to the basics before I go and write some more complex and large programs, but my end goal is write games basically from scratch, with the use of maybe some libraries at the very best. I do not enjoy or like the smell of game engines, since I do not know what is really happening underneath. I am trying to create a curriculum for myself for this coming year, of things to study on my own. Some ideas for what I want to do this year are: (in no particular order)

  • eventually create a 3D rendering engine (and understand the maths that makes it all work)
  • eventually write a minecraft clone (for learning purposes)
  • make some sort of RPG based heavily on this new game coming out on steam UNDERKEEP
  • and eventually.. become a C++ wise guy and make sellable games from scratch

does anyone have any suggestions, books, tutorials/guides, things to try and write, or communities to join, any suggestions at all? Thankyou!


r/cpp_questions • • 13h ago

OPEN Going to Study OOPS in C++ From Tommorow, any advice???

3 Upvotes

Just Completed basics of C++ Programming language like :- \*Syntex

\*Logical Operators

\*Conditional Statements

\*Loops

\*Some of data Structures like :- 1.Arrays , 2.Vectors , 3.Stack , 4.Queue

Now going to Study OOPS from tomorrow, Seeking for advices Based on Practical Industry Experience.

It will be a lot of help


r/cpp_questions • • 1d ago

OPEN How can I make a snake game in C++?

18 Upvotes

So for a project, I have to make a snake game and I'm completely lost. It doesn't have to be good graphics but my resources are limited to text output. I have visual studio 2022 and I also have online ide's. Any guidance will be helpful


r/cpp_questions • • 1d ago

OPEN cpp class vs namespace

3 Upvotes

in my personal opinion, at least half of the purpose of using nested namespace is to manage visibility, the rust's mod allows it but cpp's namespace does not, only class allows it( the public and private)

is using classes with static functions inside everywhere instead of using namespace an already existing way to use the "namespace" in a way that i can more conveniently manage visibility?


r/cpp_questions • • 1d ago

OPEN C++ and Data structure

3 Upvotes

Which topics of C++ required for Data Structure??


r/cpp_questions • • 1d ago

OPEN Reccommend Book

0 Upvotes

finding a book that teaches from basic intermediate to advanced c++ and also have projects in it.


r/cpp_questions • • 2d ago

SOLVED How do you implement/create tests correctly ?

3 Upvotes

Hi !

I have been working on a personal project for 9 month which allowed me to learn a lot but I’m going through a somewhat more challenging phase as I explore unit testing due to CMake configuration and project architecture.

I choose Google Test but my questions are not very specific to this framework. Like I said, I already have a lot of code and I want to use tests cause it seems very useful to create a solid program.

Moreover, my project is a library + an executable so this complicates things in the linkage for example.

How do you manage tests ?

What do you test (because the test exec doesn't have access to private method or private include of a library) ?

About CMake and Google Test, are there any things I should watch out for ? How properly use folders to organize that ?

Do you have any recommendations on where I should look to learn more on the good habit with that ?

I can give you more information if you need, thanks for your time !


r/cpp_questions • • 2d ago

OPEN I want to be a professional C++ systems programmer, but my university and its internship companies are obsessed with the apps programming

79 Upvotes

This is depressing; C++ is a language I love for the freedom it gives the developer, And my university is obsessed with high-level programming, Python, Javascript, and they even consider PHP to be cutting-edge technology!.

I don't want to simply do a job I don't love and do low-level work in my free time; I want to incorporate C++ into my future work, and don't come at me with "then look for a remote job" we all know that's not an option these days.

Even so, robotics is one of the majors at my university, but they only teach V+ and Linear Algebra; I suppose that's the lowest level I can reach in a work environment.

Have you ever been in a similar situation? How did you cope? Any advice?.


r/cpp_questions • • 1d ago

OPEN Book on cpp

0 Upvotes

So i was looking on amazon for a book about cpp and i found this https://a.co/d/06VJe0OR and was planning to buy it. The people that have read and used it is it good for learning the language?


r/cpp_questions • • 2d ago

OPEN Integration testing using gtest

2 Upvotes

I am confused about what is the meaning of integration testing. I have done unit testing using gtest where we will mock whatever external dependencies are there for the targeted unit (function) to test all range of values that covers all lines, functions and branches.

If we say integration test is meant to use real code instead of mocking. Then I am getting this doubt that lets say we have 3 components (separate processes) running and dependent on one another in a embedded system.

A -> B -> C

So if you are calling an api provided by A it will internally talk to B and then through C and return the result over IPC.

Now to do integration testing using gtest, do I need to compile this into a single binary by eliminating the IPC and include each code as a library. This way I have a single proces that runs gtest with all the components logic but excludes the real IPC communication.

Or instead should I keep the individual processes to be running separately and link my gtest binary to the component A so that the communication flow through the components in the target environment.

In both ways how we would actually be checking the interactions? And which one are you guys using?

Context:
Language used: C++
Domain: Automotive IT (TCU software)


r/cpp_questions • • 3d ago

OPEN For those that work with C++ in their jobs; how has AI affected your work?

84 Upvotes

Hello,

I am someone who is really passionate about programming. I dont really care as much about the business side of things in SWE - i just love problem solving and programming is one way for me to express that, kind of like writing a mathematical proof

C++ is one of my biggest passions in programming. I love the language and everything about it. Sometimes if im bored, i read a cppreference page about something I am unfamiliar about, so I have grown somewhat attached to the language

I was so excited to put my passion for C++ to use at a job. Thankfully, I managed to land a (C++) internship and completed it this summer; I had barely used any AI, but still managed to keep up and receive a return offer, so I will be starting as a graduate next year. After completing the internship I had alot of hope that Id be able to do my job the way i want to and not have to use much AI (because at the time, id say most of the good C++ devs still didnt rely on AI much)

Ofcourse since then there have been advancements in the models, so I dont know what the current state of my future workplace is

What is your guys' experience? Is everyone using AI to write the code for them? How do you guys maintain your skillset if youre not writing the code yourself? Whats your outlook on the future of the C++ industry? Are people like me just going to have to suck it up and keep C++ as a hobby?


r/cpp_questions • • 2d ago

Question wanna get into sfml.

4 Upvotes

how do i start coding in sfml? which version is the best? i want to use visual studio but i don't like how hardware expensive it is and that it doesn't give easy descriptions of code before i use it. I think using code::blocks is good for me but i want to hear your opinion on which IDE i should use.

Also what is best i start learning using sfml? i already know basic things like shapes, .move, keyboard commands, and textures but where to learn more physique base code and make games similar to factorio and rollercoaster tycoon, sandbox games with a lot of work bts.


r/cpp_questions • • 3d ago

OPEN Cpp books

23 Upvotes

Hi I'm a first year from a tier 3 university. I want to learn about cpp i have basic knowledge about C++

Inheritance, getters and setters etc I'm also learning data structures and algorithms. I want to improve my cpp knowledge, what books you guys recommend . I personally thought about going for The C++ Programming Language by Bharne Stroustrup. What are some of the better books than this I'm also targeting quant as of now but as a backup I'm keeping software engineering.


r/cpp_questions • • 3d ago

OPEN Rate my code.

0 Upvotes

I´m new at programing, I just download code::block today to start learning c++ and I got obsessed.

Here is an excersise I´ve done, and im really proud of:

#include <iostream>
using namespace std;

int precio = 80;
int desn = -30;  //desn es el descuento para niños
int desam = -35; // desam = descuento adultos mayores

int edad;

int cliente = 0;
int mayor = 0;
int menor = 0;

int recaudomenor = 50;
int recaudomayor = 45;
int recaudo = 80;

int main(){

  cout << "Bienvenido al cine. El precio del boleto regular es de $80, pero tal vez pueda obtener un descuento." << endl;

  cout << "Por favor, ingrese su edad: ";
  cin >> edad;

  while (edad != -137){

  if (edad < 12) {
  precio += desn;
  menor = menor+1;
  cout << "Felicidades, niño, tienes un descuento." << endl;
  }

  else if (edad >= 60) {
  precio += desam;
  mayor = mayor+1;
  cout << "Enhorabuena, usted tiene un descuento." << endl;
  }

  else {
  cout << "Disfrute la función, es lo único que puede." << endl;
  }

  cout << "Su total a pagar es de: $" << precio << "." << endl;

  precio = 80;
  cliente +=1;

  cout << "\n\n\n\nBienvenido al cine. El precio del boleto regular es de $80, pero tal vez pueda obtener un descuento." << endl;

  cout << "Por favor, ingrese su edad: ";
  cin >> edad;
  }

  recaudomenor = recaudomenor*menor;
  recaudomayor = recaudomayor*mayor;

  cout << "\n\nFin del día." << endl;

  cout << "Total de clientes: " << cliente << endl;

  cout << "Total de menores de edad: " << menor << endl << "Dinero recaudado: $" << recaudomenor<< endl;

  cout << "Total de mayores de edad: " << mayor << endl << "Dinero recaudado: $" << recaudomayor << endl;

  cliente = cliente-mayor-menor;
  recaudo = recaudo*cliente;

  cout << "Total de adultos: " << cliente << endl << "Dinero recaudado: $" << recaudo << endl;

  int recaudototal = recaudo+recaudomayor+recaudomenor;

  cout << "Recaudo total: $" << recaudototal;
  return 0;
}

what i do here is applying a discount to a child or an elder, taking a count of how many clients we have in a day and the money earned.

it all keeps going until i type -137 cause is my secret code. typing -137 checks the day and counts everything. its awesome.

Btw its all wrote in spanish cause im from México and i dont know if redit has spanic people, i´m also new in here.


r/cpp_questions • • 4d ago

SOLVED x1 and x2 give me "nan"

4 Upvotes

Can someone tell me what is wrong with the operations?

#include <iostream>
#include <cmath>

int main()
{
    double a = 0;
    double b = 0;
    double c = 0;
    

    std::cout << "===CALCULADORA DE ECUACIONES CUADRATICAS===\n";
    std::cout << "ingrese el coeficiente a ";
    std::cin >> a;
    std::cout << "ingrese el coeficiente b ";
    std::cin >> b;
    std::cout << "ingrese el coeficiente c ";
    std::cin >> c;

    std::cout << "tu ecuacion es " << a << "x^2 + " << b << "x + " << c << " = 0\n";
    const double x_de_vertice = -b / (2 * a);
    double solucion1 = x_de_vertice + sqrt(pow(b, 2) - 4 * a * c) / (2 * a);
    double solucion2 = x_de_vertice - sqrt(pow(b, 2) - 4 * a * c) / (2 * a);
    
    std::cout << "===========================================\n";
    std::cout << "el valor del vertice es " << x_de_vertice << "\n";
    std::cout << "x1 vale " << solucion1 << "\n";
    std::cout << "x2 vale " << solucion2 << "\n";

    
}

r/cpp_questions • • 4d ago

OPEN How could i do operations on multidimensional Arrays where the Cell might not be there ?

3 Upvotes

One of the assignements for home our professor gave us was :

-a 10x10 Matrix using multidimensional arrays

-the matrix contains all 1s and 0s

-count how many "Islands" there are (a 1 that's Has only 0s next to It in every direction)

My problem Is, i could do a pretty long line for where It checks every generic Cell [ ] [ ] next to One given, but what if that Cell Is in a position where it doesnt have cells next to It In some directions, for example, if the 1 was in a corner ?


r/cpp_questions • • 4d ago

OPEN How can I use time in my project?

3 Upvotes

I'm currently developing a c++ RPG textgame and I want to know how to improve the combat in my game. Currently it's just a turn based punch trade. How can I make the enemy hit someone for example once every 5 seconds? I know that I'll need specific libraries () which I installed by I don't know how to use them and what to do.

https://onecompiler.com/cpp/44qwdefz6

\\=> This is my project which I need help with.


r/cpp_questions • • 4d ago

OPEN built a Schnorr ZKP in C++ how can I make it more secure?

1 Upvotes

whenever I search online or ask AI's how to get cryptographically secure random numbers in C++, all I get is overcomplicated setups with huge libraries. what is the cleanest, simplest way in practice to make secure randomness into a small C++ learning project like this? Also open to any general feedback on the protocol structure

Repo: https://github.com/dev-dawud/ZeroKnowledgeProof


r/cpp_questions • • 4d ago

OPEN C++ systems route,will start building redis from scratch as a project,any guidance will be appreciated

32 Upvotes

Hey all, 2nd-year CS student from India.I want to go the C++ systems route.

I just started building Redis from scratch with the "Build Your Own Redis" book. The book is in C, and I'm planning to write mine in C++.

Would love advice on:

1.What should I learn alongside it? (Linux, networking, concurrency, debugging tools, etc.)

2.Any pitfalls when following a C book but writing C++?

3.What project should I build after Redis? I'm thinking a storage engine or something database-ish.

4.Good resources or open-source codebases to read?

5.What do systems roles expect from someone early in their career?

6.Is it even a probably and realistic career option or not?

I am also pursuing competitive programming in cpp

Any guidance would be highly appreciated as to what do u think abt my trajectory and what would be the best possible route to follow according to you

Any "I wish I'd known this earlier" advice is welcome.Thanks in advance looking forward ro some great guidance:)


r/cpp_questions • • 4d ago

OPEN Any sources to learn how C++ source projects get lowered from source code to intermediate representation and eventually to code gen part.

3 Upvotes

Can someone help me out....I'm not able to find any proper sources to follow how c++ projects get lowered from sources codes with headers and archive files to intermediate representation and then object files and shared files and linking like how do they work and their internals ....just trying to learn about them.