FAQ

From CPP

Table of contents

General FAQ

Where can I get a free C++ compiler?

There are many places to get one.

Why do I get an "unresolved external symbol _WinMain@16" (or similar) error at link-time? (MSVC)

This is most likely due to attempting to build a console program as a GUI application. If you intended to create a console app, make a new project with the console wizard.

How do I clear the screen in my console program?

With Borland's C++ compilers, you can simply include conio.h and call clrscr(). However, since Microsoft Visual C++ does not have this function, you have to do it using the WinAPI. See the article on Clearing the Console Screen to learn how to do this.

What is the difference between static and dynamic memory allocation?

Memory that is allocated "statically" comes from the local stack. This is an efficient way to allocate trivial amounts of memory, but the size of the requested memory must be known at compile time. Thus it must have the same size on every program execution (hence the name "static"). Dynamic memory, on the other hand, is allocated from a heap. The heap is much as it sounds: just a bunch of memory. It is slightly less efficient than stack allocation, but allows larger allocations, and much more flexibility. For example, the size of a block can be specified at runtime, meaning it may be different each time the program runs. In addition, a block can be "resized" (conceptually) by releasing it and allocating a new block.

How do I convert a char to its ASCII value, or vice versa?

To convert a char to its ASCII value, simply cast it to an integer. For example, the expression static_cast<unsigned int>('A') will evaluate to 65. To convert an ASCII value to its corresponding character is similarly simple. The expression static_cast<char>(65) will evaluate to 'A'.

How do I convert from string to int?

The simplest way is to use the function int std::atoi(const char*) declared in cstdlib. For example, std::atoi("31337") returns 31337. Alternatively, you can use std::strtol() or std::strtoul(). These functions provide more functionality such as the ability to convert hexadecimal or octal number strings, and are also declared in cstdlib.

How do I convert from int to string?

Converting from int to string is a little more complicated than converting a string to int. There are no standard C++ functions for this, but there is a way to do it using standard stringstreams. The class stringstream is declared in header sstream and is used like this:

std::stringstream ss;
std::string str;
ss << 31337;
ss >> str;

The variable str now contains "31337".

Question FAQ

Can I ask a question?

Yes.

Is there someone here who can answer a question for me?

Maybe. It depends what your question is, and who is around at the time. The only way to find out is to actually ask the question.

I need some help please

That's not even a question. See above.