r/cpp_questions • • 11h ago

OPEN Are there any other ways to check if a char is equal to different values?

13 Upvotes

I'm working on some homework for class where I'm checking if a character is equal to: '+', '-', '/', or '*' for this postfix to infix converter.

I know I'm able to do this in two ways I can think of with:

// 1.
if (c == '+' || c == '-' || c == '/' || c == '*') {
  // Code
}

// or 2.
bool isOperator(const char c) {
  return (c == '+' || c == '-' || c== '/' || c == '*');
}
...
if (isOperator(c)) {
  // Code
}

But is there another way to do this that's shorter? Maybe something along the lines of this?

if (c == {'+', '-', '/', '*'}) {
  // Code
}

I looked on cpp reference and stack overflow but I wasn't able to find an answer. And to be fair option 1 and 2 work just find, I'm just curious if there are other methods out there that I'm not familiar with. Thanks!


r/cpp_questions • • 14h ago

OPEN Dumb question for someone still learn c++

9 Upvotes

Hi ppl, dumb question for you, do you think is recommended clone minecraft from scratch?. Right now i am chapter 2.12 from learcpp.

My question is it the best way for learn and get some experience or still i am keeping study and reinfoce the base?. sorry for my english


r/cpp_questions • • 5h ago

OPEN How to create project templates

6 Upvotes

Hi, I don't know if this is the right subreddit but I was wondering if anyone would be able to tell me if this is possible, and, if so, to give a step by step guide to how I could do it (I know it might involve extensions):

  • I am on Linux fedora, and lets say I have a completely fresh vscode install
  • I want to have a template I can easily access via ideally pressing a button on the left side panel or something like that
  • When creating a new project with this template, it should ask you for a directory of where to store the project and a name for it - this name should then be used as the folder name for the project in that directory
  • This project should be C++23, use UTF-8 encoding, and support all the features of C++23, such as modules, and include the standard module - this should all work with IntelliSense (or if vscode uses something different) "knowing" this
  • In the project, there should be 4 folders (like Visual Studio) - External Dependencies, Header Files, Resource Files, and Source Files. This is just for sorting my files but actually doesn't matter what I put where - the project acts as if everything is just in one folder, unorganised. In Source Files, by default there should be a file named main.cpp, which should be completely blank - not even int main() etc.
  • When compiling, I would like it to be compiled as an appImage file (where the name of the file is the project name, or the name of this file can be changed) (I hear appImage can run on pretty much any Linux distro - that's why), which, despite being my OS being pretty much the newest and updated fedora, is compiled as if I compiled it on the oldest version of fedora that my project would actually compile on (I hear Linux applications are backwards compatible but not forwards)
  • Also, when running, I should have the option to choose Debug or Release, and x64 or x86 (the defaults being Debug and x64). Then, in the project folder, a folder called "Dist" should be made, inside that "x64" and "x86" (only if ran/compiled for both), and inside each of them "Debug" and "Release" (only if ran/compiled), and inside them their respective versions of the appImage (only if ran/compiled, and should still just be named project_name.appImage (unless renamed)

Thanks to any help any of you can give, it is greatly appreciated - also, please can you tell me where things are that I haven't stated, e.g. where to change the name of the final appImage, where to change Debug or Release and x64 and x86.


r/cpp_questions • • 3h ago

SOLVED Why does this not fail?

4 Upvotes

```

include <utility>

template <typename T, typename... Args> decltype(T(std::declval<Args>()...)) test();

int main() { using testType = decltype(test<int&, int>()); } ```

I was fully expecting it to fail but instead testType resolves to int&.

I was trying to implement is_constructible with SIFNAE which uses this behaviour.

``` namespace detail { template<typename T, typename... Args> decltype(T(std::declval<Args>()...), char{}) tryConstruct(int);

template<typename T, typename... Args> long tryConstruct(...); }

template<typename T, typename... Args> inline constexpr const bool isConstructible = isSame<decltype(detail::tryConstruct<T, Args...>(0)), char>; ```

I was expecting the first overload to fail so that it would chose the other one which works for most cases but dosent for the case i mentioned above.


r/cpp_questions • • 19h ago

OPEN How can I learn to think logically when solving programming problems?

1 Upvotes

At college, I try to study as hard as I can; during computer science and programming classes, I make an effort to grasp every topic, and I practice a lot at home—even using an AI assistant—but it all feels like it’s for nothing. When it comes to practical assignments, I just can’t seem to formulate the code. What should I do?


r/cpp_questions • • 20h ago

OPEN Career advice

0 Upvotes

Am currently studying c++ hopefully to try to get into game development as a junior gameplay programmer yet there is so much heat in the market cuz of ai and companies lay offs and right now the entry level is so high they require so much knowledge so am lost as to where to go stay on that track or start learning data engineering like python sql since it's an uprising field due to ai

What would you advise me with


r/cpp_questions • • 43m ago

OPEN I need help

• Upvotes

So I'm a beginner in coding and was thinking how cna I use int and string in loops while also printing the number required what I mean is Im using string and to use for In the question I need to use int to identify I and I want to make i<= to n while using string and int, I only want to know if it's possible or not ​