c++

Find a prime number?

To find whether N is a prime number we only need to look for all numbers less or equal to sqrt(N). Why is that? I am writing a C code so trying to understand a reason behind it. ...

OpenGL with FBO RTT, blending when it shouldn't

This should be really simple, but it's consumed multi-hours of my time, and I have no clue what's going on. I'm rendering a flat-colored full-screen quad to a texture, then reading back the result with glGetTexImage. It's GPGPU related, so I want the alpha value to behave as if it's any of the other three. I'm using an FBO, texture form...

What is the most efficient way to keep a steady frame rate with DirectX and C++?

I'm learning DirectX from a book about game programming, and it uses the following method for a game loop: long int start = GetTickCount(); while(true) GameRun(); void GameRun() { if(GetTickCount() - start >= 30) //do stuff } This makes start equal whatever the time is (I'm guessing get tick count gives the number o...

Does d3d9.h include windows.h? (C++)

When I use #include <d3d9.h> in my programs, I no longer need to include windows.h to use windows functions like WinMain, and CreateWindow. Is this because d3d9.h &c. include windows.h? Mainly, I'm wondering if it is possible to substitute windows.h with d3d9.h, etc, and still be able to se any functions I could use with windows.h. ...

Why won't my sorting method work?

I have an issue with my C++ code for college. I can't seem to understand why my sRecSort() method isn't working. Any help? This is really confusing me! #include <iostream> #include <algorithm> #include <string> #include <fstream> #include <sstream> using namespace std; void sRecSort(string n[], int s[], string e[], int len){ for (in...

How does get object from point work?

I'm new to programming. I want to make a card game with C++ / Allegro. The graphics api is irrelevant though. I want it to have many buttons you can click. I'm wondering the proper way this is done. For instance, how does windows know which control you click on from your cursor. I would use an array of rectangles and check each rectangle...

How to read cin with whitespace up until a newline character?

I wish to read from cin in C++ from the current position up until a newline character into a string. The characters to be read may include spaces. My first pass fails because it stops on the first space: string result; cin >> result; If cin is given: (cd /my/dir; doSometing)\n The variable result only gets: (cd I would think ...

C++0x constexpr and endianness

A common question that comes up from time to time in the world of C++ programming is compile-time determination of endianness. Usually this is done with barely portable #ifdefs. But does the C++0x constexpr keyword along with template specialization offer us a better solution to this? Would it be legal C++0x to do something like: con...

how to setup irq with masm

How can I setup IRQ using C++ and Masm, most of the search results refer me to Nasm, I am trying to create a bootloader of my own, I get it to boot from cd (iso) then I want to add menu options but cant seem to setup IRQ An example that uses nasm can be found at Bran's Kernel Development looking something like global irq0 ; 32: IRQ0 i...

Is this the right approach for a thread-safe Queue class?

Hey guys, I'm wondering if this is the right approach to writing a thread-safe queue in C++? template <class T> class Queue { public: Queue() {} void Push(T& a) { m_mutex.lock(); m_q.push_back(a); m_mutex.unlock(); } T& Pop() { m_mutex.lock(); T& temp = m_q.pop(); m_mutex.unlock(); return temp; } private...

C++ stack overflow - visual studio 2008

I declared and initialized an array having [100][1000][1000] char elements(100MB), it didn't say about a stack overflow at the first time.But when I running it after a while it throws a Stack overflow exception! I increased the -Stack Reserve Size- to 200,000,000 in project options->linker->system but it didn't worked! I'm working using ...

Most optimal way to find the sum of 2 numbers represented as linked lists

Hello, I was trying to write a program for the problem I mentioned above, the numbers (i.e the lists) can be of unequal length, I was not able to figure out a way to do this other than the most commonly thought of approach i.e reverse list-1 reverse list-2 find the sum and store it in a new list represented by list-3 reverse the li...

Visual Studio Debugger can't view arrays after they have been passed to functions.

I have a function like this: MyFunction(double matrix[4][4]) {/*do stuff*/} I am calling this from an outer function (the otuer function is a member function of a class, in case that matters): OuterFunction() { double[4][4] x; initialize(x); //this function puts the data I want in the matrix MyFunction(x); } I am trying to debug th...

Converting multidimensional arrays to pointers in c++

I have a program that looks like the following: double[4][4] startMatrix; double[4][4] inverseMatrix; initialize(startMatrix) //this puts the information I want in startMatrix I now want to calculate the inverse of startMatrix and put it into inverseMatrix. I have a library function for this purpose whose prototype is the following: ...

Caching a bitmap

I'm writing a Win32 application using C++. In this application I'm handling the WM_PAINT message: case WM_PAINT: hdc = BeginPaint(hWnd, &ps); GdiplusStartup(&gdiplusToken, &gdiPlusStartup, 0); DrawM(ps.hdc, hWnd); EndPaint(hWnd, &ps); break; And in the DrawM function I'm having something like this: void DrawMap(HDC hdc, HWND hW...

GDI+ Bitmap Save problem

Bitmap bff(L"1.jpg"); bff.Save(L"2.jpg", &Gdiplus::ImageFormatJPEG, NULL); This creates a new file 2.jpg with zero-bytes length. Isn't it supposed to write an image file that is identical to 1.jpg? Why I'm having zero-bytes length files? I'm doing this test because writing other Bitmaps to files end the same way. ...

Problems communicating with external editor in Qt4

I am writing a command-line Qt4 script (using QCoreApplication) on Mac OS X. I am using this code adapted from C++ Programming with Qt 4, 2nd ed. p. 313: QTemporaryFile outFile; if (!outFile.open()) return; QString fileName = outFile.fileName(); QTextStream out( out << initial_text; outFile.close(); QProcess::execute(editor, QStr...

Getting character from the Unicode code-point - C++

I have got two questions. 1 - I get Unicode code-points and how do I get the character associated with this code-point? Something like: int code_point = 0xD24; char* chr = (char*) code_point; But the above code fails by throwing exception. 2 - Suppose the code-point is stored in a file and I read the code-point to a string, how do I...

unsigned char array to unsigned int back to unsigned char array via memcpy is reversed

This isn't cross-platform code... everything is being performed on the same platform (i.e. endianess is the same.. little endian). I have this code: unsigned char array[4] = {'t', 'e', 's', 't'}; unsigned int out = ((array[0]<<24)|(array[1]<<16)|(array[2]<<8)|(array[3])); std::cout << out << std::endl; unsigned char b...

Using cin.get() to grab a line of text, then using it in a loop to display that line?

Ok, so I came across this code snippet in my textbook that's supposed to echo every other character a user types in. Now, I understand the every other character part, but I'm having difficulty with the use of cin.get(). I understand why the first cin.get() is there, but why is it also inside the loop? I'm guessing I'm not fully grasping ...