Does static constexpr variable inside a function make sense?

The short answer is that not only is static useful, it is pretty well always going to be desired. First, note that static and constexpr are completely independent of each other. static defines the object’s lifetime during execution; constexpr specifies that the object should be available during compilation. Compilation and execution are disjoint and discontiguous, both in time and space. So once the program is … Read more

cmath vs math.h (And similar c-prefixed vs .h extension headers)

I’ve seen some information about differences between things like iostream vs iostream.h. [iostream.h] is not a standard header. it is not an example of the issue you’re raising. [cmath] defines symbols in the std namespace, and may also define symbols in the global namespace. [math.h] defines symbols in the global namespace, and may also define symbols in … Read more

What is a `char*`?

It is a pointer to a char. When declaring a pointer, the asterisk goes after the type and before the identifier, with whitespace being insignificant. These all declare char pointers: To make things even more confusing, when declaring multiple variables at once, the asterisk only applies to a single identifier (on its right). E.g.: It is primarily for … Read more

Converting string to unsigned int returns the wrong result

You should instead use std::strtoul, found in <cstdlib>, which is designed for unsigned numbers, has a larger range, and reports errors better. If you want to use std::string for input and exceptions for error handling, use std::stoul. A short, highly efficient implementation would be as follows: This will be much faster than istringstream, culture-invariant (so no unexpected changes to behavior when … Read more

Finding square root without using sqrt function?

There is a better algorithm, which needs at most 6 iterations to converge to maximum precision for double numbers: Algorithm starts with 1 as first approximation for square root value. Then, on each step, it improves next approximation by taking average between current value y and x/y. If y = sqrt(x), it will be the same. If y > sqrt(x), then x/y < sqrt(x) by about the same … Read more

error: expected unqualified-id before ‘while’|

Your code is missing a int main(void) function in which all of your code should be inside. By definition, a C++ program is required to have a int main(void) function. You need to put all your code inside the int main(void) function. Also, your cin << and cout << statements are missing the namespace tag std::. Therefore, you need to add the std:: at every instance you use cin or cout See the code … Read more

MinGW .exe requires a few gcc dll’s regardless of the code?

Your commands are wrong ! Go to the directory where your main.cpp file is, and try the following. then you’ll no longer need to copy the DLLs (for your Hello World program). Other notes: The MinGW installation instructions recommends setting to the PATH environment variable. Normally the linker options should work (try all 3 of them at … Read more