Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all 3021 articles
Browse latest View live

Skip List

$
0
0
I want to implement a Skip List in C++. The problem is I can't understand what it is. I don't know it enough for programming it. I've searched the Web for it (the Wikipedia for example and other websites) but no one has a brief and useful introducing on it. I need someone to tell me what it is and how to create one of them on scratch step-by-step and then I can program it in compiler.

Thank you guys in advance.

Refactoring poor Design: Strategy-Pattern?

$
0
0
I'm currently working with a system/data hierarchy designed as you can see below. Systems represent bare interfaces to some sort of hardware component which is able to deliver raw data to then be further processed into some final representation.

Code:

class SystemData
{
}

class SystemDataA : public SystemData
{
  int x;
}

class SystemDataB : public SystemData
{
  float y;
}

class System
{
  virtual SystemData* getData() = 0;
  virtual Result computeData(SystemData*) = 0;
}

class SystemA : public System
{
  // really returns SystemDataA
  SystemData* getData() override;
  Result computeData(SystemData*) override;
}

class SystemB : public System
{
  // really returns SystemDataB
  SystemData* getData() override;
  Result computeData(SystemData*) override;
}

void Controller::finalProcessing()
{
  for(auto& s : systemVec)
  {
    SystemData* data = s->getData();
    FinalResult final = s->computeData(data);
  }
}

The problem here is: Each system returns a concrete data type abstracted away in some common interface, which is a nice idea. To further process it, there is however still some sort of runtime information needed from which system the data came from because the further processing steps depend on this. To get to this piece of information, the author apparently just passed it back into the system where imo it doesn't belong at all, dynamic_casts to the concrete type in the first line of each compute-function and does the processing there. To me this looks like circumventing all the nice purposes of abstract inheritance hierarchies whilst making it overly complicated.

So what I'm thinking of to make this better is some sort of encapsulated processing algorithm hierarchy. Since they don't carry any state and are really just single methods, maybe a map of function pointers will already do. Each data type then has to carry a piece of information (either just an enum type or a function pointer) of which algorithm to use but can in the end be done completely isolated from the system.
The problem remains however that I must dynamic_cast or identify the concrete data type to feed it into the correct algorithm. So this still doesn't look like the cleanest design there is. Any ideas on how to make this better?

DesktopDuplicatipn API windows8 does not capture fullscreen games

$
0
0
I was experimenting with desktopduplication api sample code and it was mentioned here that

Quote:

even full screen DirectX applications can be duplicated.
Also here it is mentioned that
Quote:

>The following components of the operating system can generate the desktop image:

>* The DWM by composing the desktop image

>* A full-screen DirectX or OpenGL application

>* An application by switching to a separate desktop, for example, the secure desktop that is used to display the login screen
However when I tested the sample code with multiple monitors provided here, it worked fine for other applications running on desktop but when I started a fullscreen directx application, the running applications in the background are pushed onto the secondary monitor and only the secondary monitor is captured in the duplicated window.

What might be the reason for this? I ran the exact sample code provided by MSDN. Is there something more I need to do in order to capture DirectX fullscreen games using DesktopDuplication API

Upgraded project - Progress Bar

$
0
0
I found an old project and I upgraded it from visual C++ 6 to visual studio 2015. It complies and works fine, but I noticed it still has the old blue progress bar. In the dialog edit window it shows the green pulsing bar but when I build and run it, it is back to the old blue bar.

How can I update the bar so it is the green pulsing bar that you see in windows 7 / 10?

Thank you,

Brand New to C++

$
0
0
Hello, I'm in my second week of an intro to comp sci class and we're learning C++. I've been working on a homework assignment for the past few days with no avail. The assignment is the following:
Write an algorithm (using input/output, assignments, conditionals, and loops) to find the sum of all positive integers less than 1,000,000 that are divisible by 3 but not by 6.

So far I have come up with this:
#include <iostream>

using namespace std;

int main()

{
int sum=0;
int x=0;
while
(x<=1000)
x=x+1;
if (x%3==0 && x%6!=0)
{
sum=sum+x;
cout<<sum<<endl;
}
return 0;
}
The first part of the program seems to run fine, but when it gets to the if statement nothing happens. I've asked classmates and otherwise for ideas and help but they were just as stumped as myself. I've emailed the teacher, but just in case I figured I'd looked elsewhere for solutions as well. I'm not looking for the answer, just guidance in the right direction. Thanks for any help in advance!

Taking a string gcc platform

$
0
0
Hello,
I want to take a string in gcc (Ubuntu, Linux) platform but the taken string can't be shown in input screen. So, I hit the key in keyboard, but that is not shown in input screen. What is the process? Please help.

strange performance behavior in character functions: toupper, tolower, isalpha, ...

$
0
0
In our MFC application, one of our dll will call "tolower" frequently. Here is the sample code:

void foo(char c)
{
for(int i=0;i<200000;++i)
{
aaa += tolower(c);
}
}

I found that there is a very stranger behavior:
1) The tolower function only slow the performance signifcantly when foo is called in the thread of the main process.
if we fork a subprocess, then create a thread to call the function foo, it works fine.
2) The release version is even much slower than the debug version.

I have tested toupper, tolower, isalpha, islower. The same phenomenon will be observed.
But if I change to function sin, it works as fast as running with subprocess

I am using VS2012, MFC.

GetInstDir() function.

$
0
0
I am using GetInstDir() to access the current directory of execution of my executable file

When I debugged the code the directory is coming like this one.

"D:\\SVN_NEW\\NewProject\\SetupScr\\..\\setup files\\setup.exe"

on execution of function SHFileOperation with this path it results in returning of value 2. Which is error file not found.

After that I hard code the path to

"D:\\SVN_NEW\\NewProject\\setup files\\setup.exe"

Which is working fine.

Now, the question is \\..\\ Is it giving problem. What should I do to change my directory to second one. As this is working fine and this is what I really need.


Welcome in case of any more information.

Problem with COleDateTime in some of the 64-bit PCs

$
0
0
I am facing this issue of COleDateTime in one of the client PCs that is 64-bit. Where I tried to use this section of the code to see what dates are stored in the string.

Code:

strDate = _T("Date Now: ") + dtNow.Format(_T("%d-%b-%y")) + _T(" Date Installed: ") + dtInstalled.Format(_T("%d-%b-%y"));
MessageBox(0, strDate, L"Error 1", MB_ICONEXCLAMATION);

It simply showed me messageBox Invalid date time. for both dtNow and dtInstalled. Despite the fact that dtNow has been initialized with current date and time whoiole dtInstalled takes a value. In my local PC it works but in one of the client's PC which too is 64-bit it showed that message.

can someone help me in troublshooting this issue?

I am getting weird out put when i run my program. Is there any way to fix this?

$
0
0
For my out put I am getting:

average = -171798688.00 (incorrect)
lowest = -858993472.00 (incorrect)
highest = 91.00 (correct)




Code:

#include <iostream>
#include <iomanip>
using namespace std;
// This program calculates the average of the inputed temperatures and finds the highest and lowest
// Jose Velazquez



int main()
{
        int numOfTemp;
        int temp[50];
        int pos;

        float findAverage(int[], float);
        float findLowest(int[], float);
        float findHighest(int[], float);

        cout << "Please input the number of temperatures to be read (no more than 50)" << endl;
        cin >> numOfTemp;

        for (pos = 1; pos <= numOfTemp; pos++)
        {
                cout << "Input temperature " << pos << ":" << endl;
                cin >> temp[pos];
        }
        cout << fixed << setprecision(2);
        cout << "The average temperature is " << findAverage(temp, numOfTemp) << endl;
        cout << "The lowest temperature is " << findLowest(temp, numOfTemp) << endl;
        cout << "The highest temperature is " << findHighest(temp, numOfTemp) << endl;//calls function

        system("pause");
        return 0;
}

float findAverage(int temp[], float num)
{
        float sum = 0;
        for (int i = 0; i < num; i++)
        {
               
                sum = sum + temp[i];

                return (sum / num); // calculates the average
        }

}

float findLowest(int temp[], float num)
{
        float lowest;

        lowest = temp[0]; // make first element the lowest price

        for (int count = 1; count < num; count++)
                if (lowest > temp[count])
                        lowest = temp[count];

        return lowest;
}

// This function returns the highest price in the array
float findHighest(int temp[], float num)
{
        float highest;

        highest = temp[0]; // make first element the highest price

        for (int count = 1; count < num; count++)
                if (highest < temp[count])
                        highest = temp[count];

        return highest;
}

Need help with my code

$
0
0
Hello there,

just started using Visual Basics for C++ in school.

I made a Roman to Arabic numeral converter and vice versa but when I input a letter when prompted to select an arabic numeral, it breaks :(

how do i fix it?

Code:

#include <iostream>

using namespace std;

int arabicToRoman(int input);

int main()
{
        char inputChar;
        int arabicIn = 0;
        int resultArabic = 0;
        char beforeChar = '0'; /*the digit before V, X, etc... The 'I' in IV.*/
        char iRoman = '0'; /*Roman Numeral Input*/

        while (true)
        {
                cout << " Press A for Arabic, R for Roman or X to Exit :";
                cin >> inputChar;

                switch (inputChar) {
                case 'a':
                case 'A':
                        cout << "Select an Arabic Number from 1000-3000 :";
                        cin >> arabicIn;
                        arabicToRoman(arabicIn);
                        break;

                case 'r':
                case 'R':
                        cout << "Select a Roman Numeral from I-MMM :";
                        resultArabic = 0;

                        cin.get(iRoman);
                        while (cin.get(iRoman))
                        {
                                switch (iRoman)
                                {
                                case 'm':
                                case 'M':
                                        if ((beforeChar == 'C') || (beforeChar == 'c'))
                                                resultArabic += 800;
                                        else
                                                resultArabic += 1000;
                                        break;

                                case 'c':
                                case 'C':
                                        if ((beforeChar == 'x') || (beforeChar == 'X'))
                                                resultArabic += 80;
                                        else
                                                resultArabic += 100;
                                        break;

                                case 'd':
                                case 'D':
                                        if ((beforeChar == 'C') || (beforeChar == 'c'))
                                                resultArabic += 300;
                                        else
                                                resultArabic += 500;
                                        break;

                                case 'x':
                                case 'X':
                                        if ((beforeChar == 'i') || (beforeChar == 'I'))
                                                resultArabic += 8;
                                        else
                                                resultArabic += 10;
                                        break;

                                case 'l':
                                case 'L':
                                        if ((beforeChar == 'X') || (beforeChar == 'x'))
                                                resultArabic += 30;
                                        else
                                                resultArabic += 50;
                                        break;

                                case 'i':
                                case 'I':
                                        resultArabic += 1;
                                        break;

                                case 'v':
                                case 'V':
                                        if ((beforeChar == 'i') || (beforeChar == 'I'))
                                                resultArabic += 3;
                                        else
                                                resultArabic += 5;
                                        break;
                                }
                                beforeChar = iRoman;

                                if (iRoman == '\n')
                                        break;
                        }
                        if (resultArabic >= 0 && resultArabic <= 3000)
                                cout << resultArabic << endl;
                        else
                                cout << "Out" << endl;
                        break;


                case 'x':
                case 'X':
                        cout << "End of Line." << endl;
                        return 0;
                default:
                        cout << inputChar;
                        break;
                }
        }


        return 0;
}


int arabicToRoman(int input)
{
        int romNum1 = 0;
        int romNum2 = 0;
        int romNum3 = 0;
        int romNum4 = 0;

        if (input > 3000)
        {
                cout << " That's too high of a number." << endl;
                return 0;               
        }
        if (input < 1000)
        {
                cout << " That's too low of a number." << endl;
                return 0;
        }
        romNum1 = input % 10;
        input = input / 10;
        romNum2 = input % 10;
        input = input / 10;
        romNum3 = input % 10;
        input = input / 10;
        romNum4 = input % 10;



/*------------------------------[M (1000)]-----------------------------------------------------------------------------*/

        for (int a = 0; a < romNum4; a++)
                cout << "M";


        /*----------------------------------------[C - CMXCIX (100 - 999)}-------------------------------------------------------------------*/
        if ((romNum3 >= 1) && (romNum3 <= 3))
                for (int a = 0; a < romNum3; a++)
                        cout << "C";
        else if (romNum3 == 4)
                cout << "CD";
        else if ((romNum3 >= 5) && (romNum3 <= 8))
        {
                cout << "D";

                if (romNum3 > 5)
                        for (int a = 5; a < romNum3; a++)
                                cout << "C";
        }
        else if (romNum3 == 9)
                cout << "CM";

        /*----------------------------------------[X - L - XC (10 - 50 - 99)}-------------------------------------------------------------------*/





        if ((romNum2 >= 1) && (romNum2 <= 3))
                for (int a = 0; a < romNum2; a++)
                        cout << "X";
        else if (romNum2 == 4)
                cout << "XL";
        else if ((romNum2 >= 5) && (romNum2 <= 8))
        {
                cout << "L";

                if (romNum2 > 5)
                        for (int a = 5; a < romNum2; a++)
                                cout << "X";
        }
        else if (romNum2 == 9)
                cout << "XC";


        /*----------------------------------------[I - IX (1 - 9)}-------------------------------------------------------------------*/



        if ((romNum1 >= 1) && (romNum1 <= 3))
                for (int a = 0; a < romNum1; a++)
                        cout << "I";
        else if (romNum1 == 4)
                cout << "IV";
        else if ((romNum1 >= 5) && (romNum1 <= 8))
        {
                cout << "V";

                if (romNum1 > 5)
                        for (int a = 5; a < romNum1; a++)
                                cout << "I";
        }
        else if (romNum1 == 9)
                cout << "IX";
        /*-----------------------------------------------------------------------------------------------------------*/

        cout << endl;
        return 0;
}

Write BITMAP data to file in C++

$
0
0
Hello all. I am currently trying to copy the header and pixel data from an image file and write this into a new image file. I am using a 256 colour bitmap and for testing have chosen a fixed height and width pixel of 150 and 90. A new file image file is created but when trying to load this, the following message is given: "Paint cannot read this file. This is not a valid bitmap file or its format is currently not supported. Would appreciate the support after many attempts. Below is the code I'm using:


02 const int height = 150;

03 const int width = 90;

04

05 unsigned char m_cHeaderData[1078];

06 unsigned char** m_cImageData = new unsigned char* [height];

07

08 for( int i = 0; i <height; i++)

09 {

10 m_cImageData[i] = new unsigned char [width];

11 }

12

13 ifstream* m_pInFile;

14 m_pInFile = new ifstream;

15 m_pInFile->open("image.bmp", ios::in | ios::binary);

16 m_pInFile->seekg(0, ios::beg);

17 m_pInFile->read(reinterpret_cast<char*>(m_cHeaderData), 1078); //bitmap bits start at offset 1078

18

19 for(int i = 0; i <height; i++)

20 {

21 m_pInFile->read(reinterpret_cast<char*>(m_cImageData[i]), width);

22 }

23

24 m_pInFile->close();

25

26 ofstream* m_pOutFile;

27 m_pOutFile = new ofstream;

28 m_pOutFile->open("imageCopy.bmp", ios::out | ios::trunc | ios::binary);

29 m_pOutFile->write(reinterpret_cast<char*>(m_cHeaderData), 1078);

30 for(int i = 0; i <height; i++)

31 {

32 m_pOutFile->write(reinterpret_cast<char*>(m_cImageData[i]), width;

33 }

34 m_pOutFile->close();

35

36

37 //deallocate memory:

38 delete m_pInFile;

39 delete m_pOutFile;

40 for(int i = 0; i <height; i++)

41 {

42 delete[] m_cImageData[i];

43 }

44 delete[] m_cImageData;

Communication between two Systems

$
0
0
Hi All

I have two independent PC based Systems. I need communication between them.
I want to transfer Data while the software is running.

This is temp and other environmental control systems. I need to transfer temp reading from one system to other.

I am using win XP and visual studio 6.0

pl guide

New_2012

Windows CE restart every 24 hours

$
0
0
Hello,

I want to restart my Windows CE Controller every 24 hours because my Webserver applications has run out of memory and a restart will fix the problem.
I read that I have to put an EXE file for it in the Windows Startup folder. Which code I need to generate the EXE File.
Or are there any other solutions?

Thanks

Bye

Can I add an MFC dialog to a regular win32 application?

$
0
0
Hello,

I am creating a win32 application, and I included MFC in a shared dll, and then created a dialog resource and associated class. Then in my code I call:

Code:

CTestDialog dlg;
dlg.DoModal();

The program then crashes with:

Code:

Debug Assertion Failed!

Program: C:\Windows\SYSTEM32\mfc120ud.dll
File: f:\dd\vctools\vc7libs\ship\atlmfc\include\afxwin1.inl
Line: 24

f: is not a drive I have. Anyone know what I might be doing incorrectly?

Thanks!
Chris

Need Good Graphics Library

$
0
0
Hi All

I need to write a project using Visual Studio MFC.

I need A Good Graphics Library which support 2d/3d graphs
and GUI for MFC .

Any suggestions ?

New_2012

Extracting RGB values from Bitmap Image

$
0
0
Hello. I am currently trying to extract R, G, B values from a 256 colour bitmap image (using for testing purposes a fixed width of 152 and height 150). I have checked using BITMAPINFOHEADER and BITMAPFILEHEADER that the offset is 1078 and bytes per pixel is 1.

The image data is initially placed in a 2 dimensional array before being placed into a 1 dimensional array. Three 2 dimensional arrays were allocated to hold the R, G, B values from each pixel.

When I run the program, the following Debug Assertion Failed! message appears:
Expression: _BLOCK_TYPE_IS_VALID (pHead->nBlockUse)

Below is the code and I would value your expert feedback and how to fix this error.

Code:

unsigned char** Allocate2DArray(unsigned char** buffer, int w, int h)

        buffer = new unsigned char*[h]; 
        if(buffer == NULL) return 0; // return if memory not allocated 
        // For each of these pointers, allocate memory of size ‘w’ 
        for(int j=0; j<h; j++) 
        {  buffer[j] = new unsigned char[w]; 
                if(buffer[j] == NULL) return 0;// return if not allocated 
        } 
        return buffer;


void DeAllocate2DArray(unsigned char** buffer, int h)

        // After complete usage, delete the memory dynamically allocated 
        for(int j=h-1; j>= 0; j--)
                delete[] buffer[j]; //delete rows 
        delete[] buffer; //delete the pointer to pointer
}


using namespace std;

int main()
{
        ifstream image;
        image.open("image.bmp",std::ios_base::binary);

        if (image.is_open())
        {
                cout<< "function success\n";
        }
        else
        {
                cout<< "unable to open file";
        }


            const int width = 152;
            const int height = 150;
       
        unsigned char m_cHeaderData[1078];
        unsigned char** m_cImageData = new unsigned char* [height];
       
        for( int i = 0; i <height; i++)
        {
              m_cImageData[i] = new unsigned char [width];
        }

        ifstream* m_pInFile;   
        m_pInFile = new ifstream;
        m_pInFile->open("image.bmp", ios::in | ios::binary);
        m_pInFile->seekg(0, ios::beg);
        m_pInFile->read(reinterpret_cast<char*>(m_cHeaderData), 1078); //bitmap bits start at offset 1078

        for(int i = 0; i <height; i++)
        {
                m_pInFile->read(reinterpret_cast<char*>(m_cImageData[i]), width);
        }

        m_pInFile->close();


        // Declare a pointer of the type you want.
        // This will point to the 1D array
        unsigned char* array_1D;

        // Now allocate memory for array_1D
        array_1D = new unsigned char[height*width];
        if(array_1D == NULL) return 0;  // return if memory not allocated

        // Copy contents from the existing 2D array
        int offset = 0;

        for(int j=0; j<height; j++)  // traverse height (or rows)
        { 
                offset = width * j; 
                for(int i=0; i<width; i++) // traverse width 
                { 
                        array_1D[offset + i] = m_cImageData[j][i]; // update value at current (i, j) 
                }
        }

        // Declare three 2D arrays to store R,G, and B planes of image.
        unsigned char**arrayR_2D, **arrayG_2D, **arrayB_2D; 
        arrayR_2D = Allocate2DArray(arrayR_2D, width, height);
        arrayG_2D = Allocate2DArray(arrayG_2D, width, height);
        arrayB_2D = Allocate2DArray(arrayB_2D, width, height);

        // return if memory not allocated
        if(arrayR_2D == NULL || arrayG_2D == NULL || arrayB_2D == NULL) return 0;

        // Extract R,G,B planes from the existing composite 1D array
        int offsetx = 0;
        int counter = 0;
        int bytesPerPixel = 1;

        for(int j=0; j<height; j++)  // traverse height (or rows)
        { 
                offset = width * j; 
                for(int i=0; i<width*bytesPerPixel; i+=bytesPerPixel) // width 
                { 
                        arrayB_2D[j][counter++] = array_1D[offsetx + i+0]; 
                        arrayG_2D[j][counter++] = array_1D[offsetx + i+1]; 
                        arrayR_2D[j][counter++] = array_1D[offsetx + i+2]; 
                } 
                counter = 0;
        }


        // After complete usage, delete the memory dynamically allocated
        DeAllocate2DArray(arrayR_2D, height);
        DeAllocate2DArray(arrayG_2D, height);
        DeAllocate2DArray(arrayB_2D, height);

        // After complete usage, delete the memory dynamically allocated
        delete[] array_1D; //delete the pointer to pointer

        //deallocate memory:
        delete m_pInFile;
        delete m_pOutFile;
        for(int i = 0; i <height; i++)
        {
                delete[] m_cImageData[i];
        }
        delete[] m_cImageData;

       
        system("pause");

        return 0;
}

from ms dos editor to visual c the story of informatica

$
0
0
a lot of year i study,anyway let yourself talk............

Can't access dialog elements in MFC

$
0
0
Hello
I'm trying to access static text element from a thread from a second modal dialog.
Code:

CRDPDlg* MainDialog = (CRDPDlg *)AfxGetApp()->m_pMainWnd;
MainDialog->statictext1.SetWindowTextW(L"OK");

It shows unhandled exception error but can't understand why :confused:
Any help is appreciated

Changing the default help file

$
0
0
I´m working on a MFC aplication that I created on Visual studio with VS 6.0. I moved it to VS10 and now I´m working on the help files. I remember that (I dont know why) I lost the original help files (on VS 6.0). I moved on with the project bacause that wasn´t important in that time. Now that I´m working on the help files I´m getting in a issue that I can´t resolve:
I created the help files with Helpndoc, and it created the .chm and all HTML files. I moved the .chm file to my working directory and changed the value of m_pszHelpFilePath on InitInstance to point to it, but it is not working. When a press F1 windows came with the message that the file is not a window help file or it is corrupted. So I created a new aplication in MFC just o to test if the help system on Windows is ok, and the help of this new aplication was called with no problem when I pressed F1 on it. So I changed the adress of m_pszHelpFilePath making it point to the new generated help file (that I now that is is working) but the result was the same: when I pressed F1 the message " The file is not a window help file or it is corrupted" pop up. I tried some google advises and change the help default aplication to hh.exe and even so, the result are the same. how to solve this?

Any help will be aprreciated.
Viewing all 3021 articles
Browse latest View live