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

program

$
0
0
Write a c program to accept a 5 digit number and find the sum of second and last digit

Passing Arrays from Javascript to COM object

$
0
0
I want to pass an Array object from Javascript to a member function of COM.

I thought I can use SAFEARRAY for that purpose.
The COM Implementation of the code is like below

VARIANT CMyObject::ReadValues(VARIANT varIndexes)
{
CComVariant var;
if(varIndexes.vt != VT_ARRAY)
return var;//Empty object
//Otherwise process using SAFEARRAYS
}

but the problem is that array object is not passed as VT = VT_ARRAY.

What is the solution?

ODBC COnfiguration issue

$
0
0
Hi,
I am trying to call SQLConfigDataSource() in my program, but apparently I receive the following:

Quote:

The setup routines for the D ODBC driver could not be found. Please reinstall the driver.
This is the code:

Code:

        wcscpy( buf, L"Driver = " );
        wcscat( buf, driverName );
        ret = sqlConfigDataSource( handle, ODBC_ADD_DSN, const_cast<SQLWCHAR *>( driverName ), buf );
        if( ret != SQL_SUCCESS )
        {
            result = false;
            while( ( ret = sqlGetDiag( SQL_HANDLE_ENV, env, i, sqlState, &nativeError, errMsg, sizeof( errMsg ), &cbErrorMsg ) ) == SQL_SUCCESS )
            {
                errorMsg.push_back( errMsg );
                i++;
            }
        }

Looking under the debugger, the value of 'ret' is 0, which SQL_SUCCESS. The value of 'driverName' is "Driver da Microsoft para arquivos texto (*.txt)".

The window handle passed is correct.

Trying to execute ODBC Administrator program from the Control Panel for this driver correctly produces the setup dialog.

Is there a reason why I get the "D" as the driver name, and the function 'fails'?

This is on Windows 8.1 with MSVC 2010.

Thank you.

How do you create columns on Autocompletionlist in VB.NET?

$
0
0
How do you create columns on Autocompletionlist in VB.NET? From code not design view. My vb 2010 does not support that function

Here is my sample code:

Me.Autcomplete.ColumnHeaders = True

Assistance with enumeration in combination with user-defined functions.

$
0
0
Hello:

I am having a little trouble with an assignment that involves enumeration. While my code seems to work, my instructor informed me that I am only to use user-defined function, with for statements, while loops, switch statements in the main body. Embarrassingly, for whatever reason, I can't seem to wrap my head how I am supposed to rewrite my switch statement as a user-defined function.

Here is what the assignment is asking for:

1a) Define an enumeration type, triangleType, that has the value scalene, isosceles, equilateral, and noTriangle.

b) Write a function, triangleShape that takes as parameters three numbers, each of which represents the length of a side of thed triangle. The function should return the shape of the triangle. (Note: In a triangle, the sum of the lengths of any two sides is greater than the length of the third side).

c) Write a program that prompts the user to input the length of the side of a triangle and outputs the shape of the triangle.

And here is what I have so far:

Code:

#include <iostream>
using namespace std;

//user-defined (enumerator) data type
typedef int side;
enum triangleType { scalene, isosceles, equilateral, noTriangle } shape;

void sideTri(side&, side&, side&);
triangleType triangleShape(side&, side&, side&, triangleType&);


int main() {
        side a;
        side b;
        side c;
        int type;


        type = triangleShape(a, b, c, shape);


        switch (type) {
        case 0:
                cout << "This is a scalene triangle." << endl;
                break;
        case 1:
                cout << "This is an isosceles triangle." << endl;
                break;
        case 2:
                cout << "This is an equilateral triangle." << endl;
                break;
        case 3:
                cout << "This is not a triangle." << endl;
                break;
        }

        return 0;
}

void sideTri(side& a, side& b, side& c)
{
        cout << "Enter side a: ";
        cin >> a;

        cout << "Enter side b: ";
        cin >> b;

        cout << "Enter side c: ";
        cin >> c;
}

triangleType triangleShape(side& a, side& b, side& c, triangleType& shape)
{

        if (a == b && b == c)                                return equilateral;

        else if (a == b || b == c || c == a)                return isosceles;

        else if (a != b && b != c && c != a)                return scalene;

        else                                                return noTriangle;
}

Any help is greatly appreciated. Thank you.

HHmmss.fff - HHmmss.fff - how to achive time difference (duration)

$
0
0
How to achieve time difference

int32 TimeDiff()
{
textBox1->Text="100501.820";
textBox2->Text="100502.831";

return // how to get time difference in milisecond?

}

Binary Tree

$
0
0
Hello everyone, I've been having problems running this program and i cant figure out the problem. its just a simple program to read a mixed file that has number 1-100000 and sort them in a binary tree.I'm not getting any errors the program is just getting stuck at debug mode and not running properly. just a note there is a second program that generates the file that this program is getting the data from, i didn't post it since it seems to be running fine and doing what its suppose to.


Header:
Code:

class Node{
        public: int key;
                        int sort_key;
                        Node *left;
                        Node *right;
                        Node(int key1,int key2){
                                this->sort_key=key1;
                                this->key=key2;
                                this->left=NULL;
                                this->right=NULL;
                        }
};

class BST{
        public:
                Node *root;
                BST(){
                        root=NULL;
                }
               
                void insert(int sort_key, int key)
                {
                        Node *x=root;
                        Node *y=NULL;
                        Node *newNode = new Node(sort_key,key);
                        if(this->root==NULL)
                        {
                                this->root=newNode;
                                return;
                        }
                        while(x!=NULL)
                        {
                                y=x;
                                if(sort_key<x->sort_key)
                                        x=x->left;       
                                else
                                        x=x->right;       
                        }
                        if(sort_key<y->sort_key)
                                        y->left=newNode;       
                                else
                                        y->right=newNode;       
                }
               
                int findDepth(Node *node){
                        if(node==NULL)
                                return -1;
                        int ldepth = findDepth(node->left);
                        int        rdepth = findDepth(node->right);                       
                        if(ldepth>rdepth)
                                return ldepth+1 ;
                        else
                                return rdepth+1;       
                }
               
                int findDepthofNode(int key){
                        Node *temp=this->root;
                        int depth=0;
                        while(temp->sort_key!=key)
                        {
                                if(key<temp->sort_key)
                                        temp=temp->left;       
                                else
                                        temp=temp->right;       
                                depth++;               
                        }
                        return depth;
                }
               
                int numOfEndNodesWithSecondNum(Node *node, int num)
                {
                        if(node==NULL)
                                return 0;
                        if(node->left==NULL && node->right==NULL && node->key==num)
                                return 1;                               
                        int x =        numOfEndNodesWithSecondNum(node->left, num);       
                        int y =        numOfEndNodesWithSecondNum(node->right, num);
                        return x+y;
                }
               
                int numOfOneChildNodesWithSecondNum(Node *node, int num)
                {
                        if(node==NULL)
                                return 0;
                        int x=numOfOneChildNodesWithSecondNum(node->left,num);
                        int y=numOfOneChildNodesWithSecondNum(node->right,num);       
                        if( (node->left==NULL && node->right!=NULL && node->key==num) ||
                                (node->left!=NULL && node->right==NULL && node->key==num)        )
                                return x+y+1;
                        else
                                return x+y;       
                }
               
                int numOfTwoChildNodesWithSecondNum(Node *node, int num){
                        if(node==NULL)
                                return 0;
                        int x=numOfTwoChildNodesWithSecondNum(node->left,num);
                        int y=numOfTwoChildNodesWithSecondNum(node->right,num);
                        if( node->left!=NULL && node->right!=NULL && node->key==num)
                                return x+y+1;
                        else
                                return x+y;               
                }       
               
};

code:
Code:

#include <sstream>
#include <string>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <time.h>
#include <fstream>
#include "BST.h"
//#include "writefile.cpp"
using namespace std;

//extern int writefile();
int main()
{
    int i =0;
    string number;
    int num_count=500;
    int random;
    BST bstree;
    //srand(time(NULL));   
        int *inputnumbers = new int[num_count];           
        //writefile();
        ifstream input("mixed.dat");
        //cout<<"done";
        string temp;           
        while ( getline(input ,number))
        {
        istringstream token(number);
        while ( token>>temp) {
                    inputnumbers[i] = atoi(temp.c_str());
            //cout<<setw(5)<<inputnumbers[i];
           
                        i++;
                }
               
        }
        input.close();
    cout<<"Data read from input file..."<<endl;
    for(i=0;i<num_count;i++)
    {
            random  = rand() % 100 +  1;
            bstree.insert(inputnumbers[i],random);
        }
        cout<<"Binary tree created..."<<endl;
    cout<<"Depth of deepest node in tree is : "<<bstree.findDepth(bstree.root)<<endl;   

    float avg_depth=0;
    for(i=0;i<5000;i++)
    {
            random  = rand() % num_count +  1;
            avg_depth+=bstree.findDepthofNode(random);
    }
    avg_depth/=5000;
    cout<<"Average depth for searching a key is : "<<avg_depth<<endl;
   
    cout<<"Number of end nodes with value 50 is: "<<bstree.numOfEndNodesWithSecondNum(bstree.root,50)<<endl;
    cout<<"Number of one child nodes with value 50 is: "<<bstree.numOfOneChildNodesWithSecondNum(bstree.root,50)<<endl;
    cout<<"Number of two child nodes with value 50 is: "<<bstree.numOfTwoChildNodesWithSecondNum(bstree.root,50)<<endl;
}

Unexpected result when assigning character to std::string or MFC CString

$
0
0
Please can someone explain what I'm seeing here? I expected the same result, a compiler error, in both situations. What's different when the variables are part of the struct? Sorry if the answer is blatantly obvious and I'm just not seeing it...

Code:

std::string str_one = 'a'; // Compiler error
CString str_two = 'a'; // Compiler error

struct MyStruct
{
    std::string str_one;
    CString str_two;
};

MyStruct my_struct;
my_struct.str_one = 'a'; // OK
my_struct.str_two = 'a'; // OK


Looking for someone who is willing to help me with my project

$
0
0
Hey
I am looking for someone who is willing to help me with my project. I am starting a website where users can get programing help, ebooks, tutorials, software(open-source,self coded,...),game server hosting and more and more.. But i also need to help writing posts on my websites forum. So i am looking for someone who can or is willing to write some threads, ebooks or more on my website. My website is developed, hosted on my own centos server(machine) so the only thing i need to fill in is the forum. I use wordpress cms with lots of plugins as you Will see when you visit my website.
So if someone is interested in helping my or partnering up with me is welcome! I am willing to make some money with my website and i am willing to share this with my partners!
So anyone interested contact me via email. Hope to get someone who Will help me! I am serious about that!
My website link: cpuniverse.tk (I Will change the domain to a .com domain in a cuple of weeks)
Email: cpuniverse@mail.com
Greetings
TheX182

XOR encryption using CString

$
0
0
ive been trying to figure out how to do a basic XOR encryption with CStrings but i have errors
im having trouble with the for loop character counter. the code i have to count the number of characters in the CString returns an error from a header file atlsimpstr.h
my code function and error window are as follows:
Name:  Capture.PNG
Views: 50
Size:  14.6 KBName:  Capture1.PNG
Views: 34
Size:  26.6 KB
Attached Images
  

How does Windows Server 2012 load visual studio 2005 build dlls?

$
0
0
Hi,

My application and the depency dlls were all built using Visual Studio 2005. And every thing works fine on Windows 7 and Windows Server 2008 but not on Windows Server 2012.

I'm seeing an issue in my application because of the order the dlls are getting loaded.

For ex:

MyApp depends on DllA,DllB.

DllA depends on DllC and DllD.

In Windows 7 the dlls are getting loaded in the following order...

MyApp,DllA,DllC,DllD and DllB.

In Windows Server 2012 the dlls are getting loaded in the following order...

MyApp,DllA,DllB,DllC and DllD.

Everything compiles and runs fine in Windows Server 2012 but some functions are failing due to the dll load order.

This is causing the issue in my application on Windows Server 2012 as we are doing the things based on the order the dlls are getting loaded in Windows 7. Since the order is different in Windows Server 2012 it is failing to do some initializations.

So my question is there any switch in Visual Studio 2005/2010 that we can force the dependencies to load first?

Or is this how Windows Server 2012 loads the dlls?

Thanks

Loadiing Webbrowser control dynamically

$
0
0
I am developing a WYSIWYG application. While saving it generates an asp code and save as an *.asp file.

Now I have to read the file. IWebBrowser2::Navigate is not working. It treats the file as asp code and gives some error message.

I tried this way:

CComQIPtr<IPersistStream> pPS = pWebBrowser2;//pWebBrowser2 is of type IWebBrowser2
Now I read the file and put the data in Stream Object(IStream* pStm)

But pPS->Load(pStm) returns E_FAIL;

Last I attempted to destroy and create the control dynamically hoping Load will work during the creation process.

I opened CFile object and initialized a pointer to the CFile object as m_pFilePersist.

Now I tried to recreate the control as in the following code:

m_wndControl.CreateControl(m_clsid,_T(""),m_dwStyle,CRect(0,0,100,100),this,1,m_pFilePersist,NULL,m_bstrLicKey)

But again inside the MFC code Load is returning E_FAIL!

Now what to do? Please help me.

Coloring thumbs on CScrollBar

$
0
0
I am trying to coloring thumbs on CScrollBar control ... in order to achieve that, I derived a class from CScrollBar, named CScrollbarExt, and I handled CtlColor:
Code:

protected:
        //{{AFX_MSG(CScrollBarExt)
        afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
        //}}AFX_MSG

and
Code:

HBRUSH CScrollBarExt::CtlColor(CDC* pDC, UINT nCtlColor)
{
        // TODO: Change any attributes of the DC here

        // TODO: Return a non-NULL brush if the parent's handler should not be called
        return m_Brush;
        return NULL;
}

of course, m_Brush has a custom color, and scrollbar shaft has custom color ... but how can I draw thumbs of this CScrollBar object ? What can I do for that ?
I attach a little sample project to illustrate the issue ...
Attached Files

Why is Microsoft pushing Windows 10 so hard?

$
0
0
I've been running Windows 7 ultimate for several years - doing programming largely in C++ using Visual Studio 2010 and lately 2013. I am largely happy with these. I keep getting popup a dialog to download and install 'free' Windows 10. Why is MS$ so persistent? What are the downsides to using Windows 10 with VS 2010/2013? Your thoughts greatly appreciated. Try not to be so politically correct for a change.

Function arguments are undefined in function call

$
0
0
It's been a while, and I've partially forgotten how to manage the scope of variables. Here's the code:

Code:

#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

//Function Prototype(s):
int twoBirdsOneStoneRectangle(int, int, int);


int main()
{
        cout << "The area of our rectangle is: ";
        twoBirdsOneStoneRectangle(length, width, area);//error: length, width, and area are undefined

        system("pause");
        return 0;
}

int twoBirdsOneStoneRectangle(int length, int width, int area)
{
        cout << "Please enter an integer number for the length and width of the rectangle, separated by a space:\n";

        cin >> length >> width;//two birds with one stone
        area = length * width;
        return area;
}

So why is it complaining? Am I supposed to declare the length, width, and area variables in the main function? If so, won't that make them global?

Variable for a member variable

$
0
0
Hello

I have 2 combo boxes in the same class that use the same code.
So I wrote a function for combobox1 and another for combobox2.

Is it possible to assign a variable for the controls and passing it to the function so that the same code can be used?

Any help is much appreciated.

Simple program executed without using return 0 in main?!

$
0
0
Somehow, my simple program that calculates the area of a circle compiled and ran:
Code:

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
        const double PI = 3.14159;
        double area, radius;
        cout << "Please enter a radius.\n";
        cin >> radius;
        area = PI * pow(radius, 2);
        cout << "The area of the circle is: " << area << endl;
        system("pause");
}

even though I didn't put a return 0 statement at the end of my main function. What is up with that?

MFC EXE COM object not accessable through c++ program

$
0
0
I made an MFC program. Added COM object through appwizard. Just added a property to that.

To test it I used vbscript. It is working. But when I use C++ client I get E_NOINTERFACE.
I am querying only IDispatch interface.

Since VBScript is working, definitely IDispatch is available. But why in c++ program I am getting error?

Can anybody give some hint? Marshalling is not a problem, since I am querying for IDispatch only.

Help me! algorithm for difficult probem

$
0
0
Mr. David is planning a business trip to the island of Manhattan where he has arranged meetings with buyers at their offices. He wants to stay in a hotel that is not far from their offices. Assume that the streets on the island of Manhattan are given as a grid layout in the plane where each cell is a unit square. See figures below.
Name:  fd5a63a9-75c4-47b9-b4eb-f03917fbc31a.jpg
Views: 47
Size:  16.0 KB
Assume also that there are N hotels and M offices of buyers at grid points.

1. N hotels are denoted by H1,…,HN, and Hi=(j,k) is located at the grid point of the j-th row and the k-th column.
2. M offices are denoted by O1,…,OM, and Oi=(j,k) is located at the grid point of the j-th row and the k-th column

The distance between two grid points, p=(i,j) and q=(k,l), is defined by their L8 distance (Chebyshev distance), that is, d8 (p,q)=max{|i - k|, |j - l|}. (|i| is the absolute value of i.) For example, there are two offices and four hotels in Figure 1. Hotel H2=(2,3) is at distance 1 from O2=(3,4) while the other three hotels H1,H3,H4 are all at distance 2 from O2. Given two distinct hotels, H and H’, if we have d(H,Oi) < d(H’,Oi) for all i=1,…M, hotel H is said to dominate hotel H’.




In Figure 1, if we compare hotels H1 and 2, we have d8 (H2,O1) < d8 (H1,O1) and d8 (H2,O2) < d8 (H1,O2), and therefore hotel H2 dominates hotel H1.
In Figure 2, we have d8 (H2,O1) < d8 (H3,O1),d8 (H2,O2) < d8 (H3,O2),d8 (H2,O3) < d8 (H3,O3), and therefore hotel H2 dominates hotel H3. For hotels H1 and H3,

we have d8 (H1,O2 )= d8 (H3,O2 )=5, and therefore they do not dominate each other.


Write a program that given N hotels and M offices, outputs all hotels that are not dominated by any other hotels.



Time Limit: 4 seconds for 30 cases. (If your program exceeds this time limit, the answers that have been already printed are ignored and the score becomes 0. So, it may be better to print a wrong answer when a specific test case might cause your program to exceed the time limit. One guide for the time limit excess would be the size of the input.)



[INPUT]
There can be more than one test case in the input file. The first line has T, the number of test cases. Then the totally T test cases are provided in the following lines (1=T=30).

In each test case, the first line has two integers N and M, the numbers of hotels and offices.

In each of the following N lines, two integers A and B are given, where 1=A,B=1024.

Here A and B represent the location of a hotel at the grid point of the A-th row and the B-th column.

After then, in each of the following M lines, two integers A and B are given, where 1=A,B=1024.

Here A and B represent the location of an offices at the grid point of the A-th row and the B-th column.

Hotels and offices are given in increasing order of their indices: H1,…,HN followed by O1,…,OM. The locations of hotels and offices are all distinct



[OUTPUT]
Each test case contains two lines. For the T-th test case, “Case #T” is printed in the first line. The second line contains the list of indices of hotels, sorted in increasing order, that are not dominated by any other hotels.





[I/O Example]
Input
2 ? There are 2 test cases
4 2 ? Starting Case 1
1 5
2 3
3 6
5 2
1 2
3 4
10 3 ? Starting Case 2
6 2
5 4
6 5
2 4
3 2
4 1
7 3
6 6
1 7
4 7
5 3
1 1
2 6



?Output
Case #1
2
Case #2
1 2 4 5 6 9 10
Attached Images
 

Asynchronous call

$
0
0
Hi,
In order to put the file into Trash I have to call SHFileOperation.But this call is synchronous which means I will have to wait until the operation finishes.

Is there an asynchronous call which sends the files to Trash?

Thank you.
Viewing all 3021 articles
Browse latest View live