|
I have a list class here that is being handed a tree class(not included in the thread). I need to get this List to be ordered and have been having a devil of a time doing so.
It seems not to like anything I throw at it; bool operators to decide less than or greater than, if statements. Can sombody give me a push in the right direction.
Thanks.
B.
#include <iostream>
#include <string>
#ifndef LIST_H
#define LIST_H
using namespace std;
struct node
{
string data;
node *next;
node *successor;
node *predec;
};
class List
{
node *first;
node *successor;
node *predec;
public:
List()
{
first=NULL;
successor=NULL;
predec=NULL;
}
void insertNode(string newName)
{
node *p;
p=new node;
p->data=newName;
p->next=first;
first=p;
}
void outputList(ostream &out)
{
node *p;
p=first;
if(!empty())
{
cout << endl;
while(p!=NULL)
{
cout << p->data << " ";
p=p->next;
}
}
else
cout << "The list is empty!" << endl;
}
bool empty()
{
if(first==NULL)
return true;
else
return false;
}
};
#endif
|
|
|
|
|
I don't get your question. How are you trying to sort the list? Also, if this is an option you might consider using std::list instead of your own homemade container, it'll save you a lot of problems.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
One way would be to modify the insertNode() method, since it always adds to the front of the list. Something like:
void insertNode( string newName )
{
node *p, *n, *t;
p = new node;
p->data = newName;
p->next = NULL;
n = first;
t = first;
while (NULL != n && p->data > n->data)
{
t = n;
n = n->next;
}
if (NULL == first)
first = p;
else if (n == first)
{
first = p;
p->next = n;
}
else
{
t->next = p;
p->next = n;
}
}
Five birds are sitting on a fence.
Three of them decide to fly off.
How many are left?
|
|
|
|
|
Thanks for showing me another way to look at it. I was fixed on one method that didn't want to compile.
Thanks.
B.
|
|
|
|
|
Hi Everyone,
I have two applications (one developed in VC++ 6.0 and the other developed in VB.NET )which communicate to each other in secure manner.) I am using RC2 encryption to encrypt messages between the two applications. Both of them use the same key , IV , padding and the same cipher mode (ECB). I want to encrypt messages in one application and decrypt in the other application. But I am not able to do that. I am getting bad data exception when i Encrypt in one application and try to decrypt the message in another application.
I am pasting the code below from both the applications.
VC++ 6.0
GetSessionKey {
CryptAcquireContext( &hProv, NULL,
MS_ENHANCED_PROV , PROV_RSA_FULL, 0 );
CryptGetUserKey(hProv, AT_KEYEXCHANGE ,&hPublicKey);
BYTE tKey[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
DWORD tLen = 16 ;
DWORD dtLen = 16 ;
fResult = CryptEncrypt(hPublicKey ,0,TRUE ,0 , NULL , &tLen , dtLen );
BYTE *tKey1 = new BYTE [tLen] ;
memset(tKey1 ,'\0', tLen) ;
memcpy(tKey1 , tKey , dtLen) ;
BOOL fResult = CryptEncrypt(hPublicKey ,0,TRUE ,0 , tKey1 , &dtLen , tLen );
fResult = CryptGenKey(hProv, CALG_RC2, CRYPT_EXPORTABLE, &hTempKey);
dwSize = sizeof(DWORD);
DWORD dwBlobLen ;
fResult = CryptGetKeyParam(hTempKey, KP_KEYLEN, (LPBYTE)&dwProvSessionKeySize,
&dwSize, 0);
CryptExportKey( hTempKey, hPublicKey, SIMPLEBLOB, 0, NULL, &dwBlobLen);
pbKeyBlob = (BYTE*)malloc(dwBlobLen);
//--------------------------------------------------------------------
// Export the key into a simple key BLOB.
fResult = CryptExportKey(hTempKey, hPublicKey, SIMPLEBLOB, pbKeyBlob, &dwBlobLen);
int tlen = sizeof(ALG_ID) + sizeof(BLOBHEADER) ;
pbPtr = pbKeyBlob;
pbPtr =pbKeyBlob + sizeof(ALG_ID) + sizeof(BLOBHEADER) ;
//copy the key in the blob
for (n = 0 ; n < dwProvSessionKeySize ; n++)
{
pbPtr[n] = tKey1 [n];
}
if (hTempKey) CryptDestroyKey(hTempKey);
DWORD dErr = GetLastError() ;
fResult = CryptImportKey(hProv, pbKeyBlob , dwBlobLen,
hPublicKey, CRYPT_EXPORTABLE, &m_hcryptSessionKey);
dErr = GetLastError() ;
BYTE ivarray[] = {1,1,1,1,1,1,1,1};
fResult = CryptSetKeyParam(m_hcryptSessionKey , KP_IV , ivarray ,0) ;
DWORD dwMode = CRYPT_MODE_ECB;
fResult =CryptSetKeyParam(m_hcryptSessionKey, KP_MODE, (BYTE*)&dwMode, 0) ;
DWORD dwCount = 0 ;
dwMode = PKCS5_PADDING ;
fResult =CryptSetKeyParam(m_hcryptSessionKey , KP_PADDING , (BYTE*)&dwMode , 0 );
}
int CEncryption::EncryptData(LPSTR inStr , DWORD &dwLen , CString & outStr)
{
// convert string to bytes
DWORD dataLen = dwLen ;
if (dwLen == 0) return 0 ;
if (inStr == NULL) return 0 ;
BOOL bResult = CryptEncrypt(m_hcryptSessionKey ,0,TRUE , 0, NULL , &dwLen , dataLen) ;
BYTE *encTxt = new BYTE [dwLen] ;
memset(encTxt ,'\0' ,dwLen) ;
memcpy (encTxt ,inStr , dataLen) ;
//dwLen = dataLen ;
bResult = CryptEncrypt(m_hcryptSessionKey ,0,TRUE , 0, encTxt , &dataLen , dwLen) ;
delete encTxt;
return bResult;
}
int CEncryption::DecryptData(LPSTR inStr , DWORD &dLen , CString & outStr)
{
DWORD dataLen = 0 ;
BYTE *outBytes = NULL ;
__try
{
outBytes = new BYTE [dLen ];
memset(outBytes ,'\0' ,Len);
dataLen = Len ;
BOOL bResult = CryptDecrypt(m_hcryptSessionKey ,0,TRUE , 0, outBytes , &dataLen) ;
DWORD dErr = GetLastError() ;
outBytes[dataLen] = '\0' ;
outStr = (char *)outBytes ;
}
return TRUE;
}
vb.net code
Dim iv As Byte() = {1, 1, 1, 1, 1, 1, 1, 1}
Dim key As Byte() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
dim rc2CSP As new RC2CryptoServiceProvider
rc2CSP.iv = iv
rc2CSP.key = key
// encryption
Dim encrypted() As Byte
Dim toEncrypt() As Byte
Console.WriteLine("Effective key size is {0} bits.", rc2CSP.EffectiveKeySize)
Dim keysize As Integer = rc2CSP.EffectiveKeySize
'Create a new key and initialization vector.
'Get an encryptor.
Dim encryptor As ICryptoTransform = rc2CSP.CreateEncryptor(key, iv)
'Encrypt the data.
Dim msEncrypt As New MemoryStream
Dim csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
'Convert the data to a byte array.
'toEncrypt = textConverter.GetBytes(InnerString)
toEncrypt = plainText
'Write all data to the crypto stream and flush it.
csEncrypt.Write(toEncrypt, 0, toEncrypt.Length)
csEncrypt.FlushFinalBlock()
'Get encrypted array of bytes.
encrypted = msEncrypt.ToArray()
' Destruct classes
csEncrypt.Close()
csEncrypt = Nothing
msEncrypt.Flush()
msEncrypt.Close()
msEncrypt = Nothing
// decryption
Dim decryptor As ICryptoTransform = rc2CSP.CreateDecryptor(key, iv)
'Now decrypt the previously encrypted message using the decryptor
' obtained in the above step.
Dim msDecrypt As New MemoryStream(cipherText)
Dim csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
Dim fromEncrypt As Byte()
fromEncrypt = New Byte(cipherText.Length) {}
'Read the data out of the crypto stream.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)
csDecrypt.Close()
csDecrypt = Nothing
msDecrypt.Close()
msDecrypt = Nothing
Any help in pointing me in the right direction will be highly appreciated
|
|
|
|
|
Hi, All:
I have a MFC application developed five years ago and it used CDaoDatabase and CDaoRecordset. It works fine with MS Access 97 DB. After we converted the DB to MS Access 2000. The Application fails on the DB open. I use the following to open the DB:
CDAODatabase pDB = new CDaoDatabase();
try
{
pDB->Open( "MyDBFilewithFullPath", FALSE, FALSE, _T("") );
}
I wonder if there is any easy way to make this work with MS Access 2000. Will using ODBC fix the problem?
Thanks in advance.
Dion
|
|
|
|
|
Use GetError() to find out the error code, then look it up, this will give a little bit of information. You do have to set up the ODBC Data Source Administrator to the correct value. This is in the Control Panel. In addition, look at any pre-compiled headers and you should look at what version daoXXX.lib you are linking to.
J.
----------------------------
|
|
|
|
|
I had this problem a few years back as well. The problem is that the MFC classes
uses DAO 3.5 to open the Access databases. Access 2000 and above however is
incompatible with 3.5.
There is a workaround on the Microsoft Knowledgebase. Have a look at:
http://support.microsoft.com/default.aspx?kbid=236991[^]
Maybe now's a good time to switch to ADO for database access.
I Dream of Absolute Zero
|
|
|
|
|
How do you create menus in VC++ dialog based MFC application?
Provide a sample code for it.
Karteek
|
|
|
|
|
Your dialog object will need a CMenu member variable. In the dialog's OnInitDialog() method, simply call that variable's LoadMenu() method followed by CDialog::SetMenu() . It's that easy!
Five birds are sitting on a fence.
Three of them decide to fly off.
How many are left?
|
|
|
|
|
Hi,
I'm using
RECT r;
GetClientRect(hWnd,&r);
CreateWindow(WC_EDIT,etc,etc,r.right,r.bottom....);
to get the client area of my window and putting an edit control in there which should fit precisely. I noticed that the returned value for the first window created by the application is wrong. The height is 10 pixels short(about the same height as a titlebar).
When used with any subsequently created windows the value is correct. Does anybody know what this is about? Maybe a bug?
|
|
|
|
|
I have found that when I want to have things sized perfectly in a client that I also need to process the WM_SIZE message to maintain proper sizing of the child.
|
|
|
|
|
Anyone know why VC++7 completely craps out when you change the name of the class or the .cpp or the .h files to be created in the MFC ODBC Consumer Wizard? Am I the only one with this problem? The OBDC driver is MySQL 3.51.
Matt (Padawan Learner)
|
|
|
|
|
|
Perhaps GetAdaptersInfo would help?
|
|
|
|
|
I just wanted to make it clear that i have heard that it is possible to have global memory in windows 95,97 which could be shared by all. What i mean by shared memory is that some new operator works on global memory.
I need exact details that how to have memory which i allocate from any process is shared between all the processes.
I need the api or is it ka any new operation allocate memory on the global level not private memory as windows nt has private address space for each process and pointer in one process is invalid in another.
|
|
|
|
|
Please help
Here is my code. I was able to modified to go to main menu.
But If user enter a value outside the range it should not
go to the main menu. It should display only:
cout<<"Please enter 1 or 2 (3 to exit program):";
cin>>choice;
#include <iostream.h>
//using namespace std;
int main(void)
{
int choice;
float c; //Declare variable to be displayed.
float f;
//Question:Will it be the best practice to do
this: float c=0?
//Question:I used float, but I am not sure if in
this case better to use int or double?
for(;;)
// empty to promt a user to enter correct
choice value
{
cout<<"Please select the task to
perform:"<<endl;
cout<<"convert="" fahrenheit="" to=""
centigrade="" (1)"<<="" endl;
="" centigrade=""
fahrenheit(2)"<<="" cout<<"exit="" the="" program(3)"<<=""
="" do
="" use="" do-while="" loop
="" {
="" cout<<"please="" enter="" 1="" or="" 2="" (3="" exit=""
program):";
="" cin="">>choice;
switch (choice)
{
// use switch in order to do
validation and calculation
case 1:
{
cout<<"Enter value
in Fahrenheit:" ;
cin>>f;
c=((f-32)*5/9);
cout<<"The value
in Centigrade is:"<<c<<endl;
}
="" break;
="" case="" 2:
="" {
="" cout<<"enter="" value=""
in="" centigrade:";
="" cin="">>c;
f=((c+32)*9/5);
cout<<"The value
in Fahrenheit is:"<
|
|
|
|
|
You could create another function to handle the switching.. I would make that function of type BOOL and if they enter a invalid response then you could return false and prompt the user again with the same question...
Rob
Whoever said nothing's impossible never tried slamming a revolving door!
|
|
|
|
|
can you give the example? I am new to C++
|
|
|
|
|
I didn't compile this so it may have errors.. At least I think you will get the basic idea...
BOOL OnPrompt();
int main(void)
{
cout<<"Please select the task to perform:"<<endl;
cout<<"Convert Fahrenheit to Centigrade (1)"<< endl;
cout<<"Convert Centigrade to Fahrenheit(2)"<< endl;
cout<<"Exit the program(3)"<< endl;
while(true)
{
if(OnPrompt())
break;
}
return 0;
}
BOOL OnPrompt()
{
int choice;
float c;
float f;
cout<<"Please enter 1 or 2 (3 to exit program):";
cin>>choice;
switch (choice)
{
case 1:
{
cout<<"Enter value
in Fahrenheit:" ;
cin>>f;
c=((f-32)*5/9);
cout<<"The value
in Centigrade is:"<<c<<endl;
return TRUE;
break;
}
case 2:
{
cout<<"Enter value in Centigrade:";
cin>>c;
f=((c+32)*9/5);
cout<<"The value in Fahrenheit is:"<<f<<endl;
return TRUE;
break;
}
case 3:
{
cout<<"Thank you for using this program."<<endl;
return TRUE;
break;
}
default:
{
cout<<"Incorrect input"<< endl;
return FALSE;
}
}
}
Rob
Whoever said nothing's impossible never tried slamming a revolving door!
|
|
|
|
|
I am so sorry. But I could not compile your example.
And also I did not understand
Please explain one more time...
|
|
|
|
|
For brevity, Rob left out some of the basics. It was assumed you knew how to add the missing pieces. Work through the compiler/linker errors one by one until you encounter one that you just cannot figure out.
Five birds are sitting on a fence.
Three of them decide to fly off.
How many are left?
|
|
|
|
|
DavidCrow wrote:
Five birds are sitting on a fence.
Three of them decide to fly off.
How many are left?
I would love to guess "two", but since that's just *way* too easy, I'm gonna go with "a number".
The kindest thing you can do for a stupid person, and for the gene pool, is to let him expire of his own dumb choices.
[Roger Wright on stupid people]
|
|
|
|
|
DavidCrow wrote:
Five birds are sitting on a fence.
Three of them decide to fly off.
How many are left?
None - if one bird takes off, so does the whole flock.
Who is 'General Failure'? And why is he reading my harddisk?!?
|
|
|
|
|
Your while loop is definitely testing the wrong thing. Or is there a typo?
You probably need to drop the useless for( ;; ) -loop and switch to a while-loop testing for (choice == 3) as exit-condition.
You obviously need to get the input for choice before this loop.
As last statements in this loop (after the switch block), you need to duplicate the input for choice.
That way, you will be asking for choice in any subsequent loops.
The switch-statement will then have cases only for 1, 2, and default.
Be careful what entering a letter into your float variable does!
An additional hint:
do
{ seems to be a very silly comentary. Everyone can see this.
Try not to comment what you do, but why and why exactly so and not different.
Who is 'General Failure'? And why is he reading my harddisk?!?
|
|
|
|
|