C++ round a double up to 2 decimal places

I am having trouble rounding a GPA double to 2 decimal places. (ex of a GPA needed to be rounded: 3.67924) I am currently using ceil to round up, but it currently outputs it as a whole number (368) here is what I have right now using the above code with 3.67924 would output 368 … Read more

Why do we use volatile keyword? [duplicate]

Consider this code, When this program gets compiled, the compiler may optimize this code, if it finds that the program never ever makes any attempt to change the value of some_int, so it may be tempted to optimize the while loop by changing it from while(some_int == 100) to something which is equivalent to while(true) so that the execution could be fast (since the condition in while loop … Read more

segmentation fault 11 in C++ on Mac

You’ve exceeded your stack space given by the OS. If you need more memory, the easiest way is to allocate it dynamically: However, std::vector is preferred in this context, because the above requires you to free the memory by hand. If you can use C++11, you can possibly save some fraction of time by using unique_ptr array specialization, too: Both of … Read more

‘cout’ was not declared in this scope

Put the following code before int main(): And you will be able to use cout. For example: Now take a moment and read up on what cout is and what is going on here: http://www.cplusplus.com/reference/iostream/cout/ Further, while its quick to do and it works, this is not exactly a good advice to simply add using namespace std; at the top … Read more

Is there a decent wait function in C++?

you can require the user to hit enter before closing the program… something like this works. The cin reads in user input, and the .ignore() function of cin tells the program to just ignore the input. The program will continue once the user hits enter. Link

What is uintptr_t data type

uintptr_t is an unsigned integer type that is capable of storing a data pointer. Which typically means that it’s the same size as a pointer. It is optionally defined in C++11 and later standards. A common reason to want an integer type that can hold an architecture’s pointer type is to perform integer-specific operations on a … Read more

Check if a string is palindrome

Just compare the string with itself reversed: This constructor of string takes a beginning and ending iterator and creates the string from the characters between those two iterators. Since rbegin() is the end of the string and incrementing it goes backwards through the string, the string we create will have the characters of input added to it in reverse, reversing the … Read more