Solved

String compare not working

Posted on 2013-06-18
8
422 Views
Last Modified: 2013-06-18
I worked so hard to get this program to work, and on the very last important task, it is failing.

I simply need to compare two strings. It's not erroring, but not working.

Can anyone show me my error?
 I simply want to see if szCorrect is equal to "eeheqij"

#include <windows.h>
#include "resource.h"
#include <string>
// +---------------------------------------------------------------------------+
// | Simple Character Encryption                                               |
// +---------------------------------------------------------------------------+
// | main.cpp                                                                  |
// |                                                                           |
// | This program demonstrates how to encrypt and decrypt using                |
// | simple character substitution                                             |
// +---------------------------------------------------------------------------+
// | Acknowledgements:                                                         |
// |                                                                           |
// | Authors: John Tropeano                                                    |
// |                                                                           |
// +---------------------------------------------------------------------------+


//---------------------------------------------------------------------------
// Function Protypes Defined
//---------------------------------------------------------------------------

LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);

//---------------------------------------------------------------------------
// Define Global Variables
//---------------------------------------------------------------------------

HWND hWnd;

//---------------------------------------------------------------------------
// WinMain: Application Entry Point
//---------------------------------------------------------------------------

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine, int nCmdShow)
{
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1),
	          hWnd, reinterpret_cast<DLGPROC>(DlgProc));

	return FALSE;
}
//---------------------------------------------------------------------------
// Dialog Procedure
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
        
    /* Local Variables */
    char szbuffer[255], szbuffer2[255];
    char userCombination[80] = {'/0'};
    
    // This is the word 'crackme' encrypted!!
    std::string encrypted("eeheqij");
    int stringLength;
    
    // The word "crackme" encrypted  = e   e   h   e   q   i   j    
    
    //                               1         2         3        
    //                     01234567890123456789012345678901234
    char keyPhrase[]    = "thequickbrownfoxjumpsoverthelazydog";
    char cypherPhrase[] = "packmyboxwithfivedozenliguorjugs";
    char szCorrect[] = "eeheqij";
    
	switch(Msg)
	{
	case WM_INITDIALOG:
		return TRUE;

	case WM_COMMAND:
		switch(wParam)
		{
		case IDOK:
            // Move users answer to szbuffer 
            GetDlgItemText(hWndDlg, IDC_EDIT1, szbuffer, 80);
            
            // Get length of user string entered
            stringLength = strlen(szbuffer);
            
            // Loop through every character
            for(int xx=0; xx<stringLength; xx++){
                        // Isolate single character
                        char currentCharacter = szbuffer[xx];
                        
                        // Locate character in cypherPhrase
                        std::string strCypherPhrase(cypherPhrase);
                        
                        // Get Position of current character in cypherPhrase
                        // Note: The character should always be found
                        //       because every letter is in cypherPhrase
                        size_t found = strCypherPhrase.find(currentCharacter);
                        
                        // Now get keyPhrase[FOUND] and add to final userCombination buffer
                        userCombination[xx] = keyPhrase[found];
                        
                        // Move current encrypted character to buffer2 ( for MessageBox debugging )    
                        //wsprintf(szbuffer2,"%c", keyPhrase[found]);
                        //MessageBox ( NULL, szbuffer2, "Encrypted Character is...", MB_OK | MB_ICONINFORMATION );
                }
                // Debug Encrypted String
                // Should output: e   e   h   e   q   i 
                //MessageBox ( NULL, userCombination, "Encrypted String Debug", MB_OK | MB_ICONINFORMATION );
               
               
               // Now check to see if they entered the correct answer
               // if userCombination == 'eeheqij' they were CORRECT
               // NOT WORKING
               //--------------------------------------------------------------------------------------
               if(strcmp(userCombination,szCorrect)==0){
                MessageBox ( NULL, "CORRECT", "You Entered the correct password!!!", MB_OK | MB_ICONINFORMATION );
               } 
               //--------------------------------------------------------------------------------------
			EndDialog(hWndDlg, 0);
			return TRUE;
		}
		break;
	}

	return FALSE;
}
//---------------------------------------------------------------------------
// Application Functions
//---------------------------------------------------------------------------
// NONE YET

Open in new window

0
Comment
Question by:edvinson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 36

Accepted Solution

by:
mccarl earned 500 total points
ID: 39255458
It looks like it should be comparing the strings correctly. Have you double/triple checked the actual contents of userCombination and szCorrect character arrays at that point in your code? ie. dumped them to the console, file, etc. to see what is there. Note that using MessageBox is probably NOT the most accurate way of debugging, as you might miss subtle differences like missing/misplaced null terminators, etc.
0
 
LVL 1

Author Comment

by:edvinson
ID: 39255477
Could you show me how to dump these values to the console?
0
 
LVL 1

Author Comment

by:edvinson
ID: 39255510
I figured out how to dump vars

I have a problem with my variable userCombination

Here is my WATCH output of that variable:

"eehequj", '\0' <repeats 72 times>


I see the correct value there, but for some reason it repeats??? I see why strcmp is not working, but why is that value repeating like that?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:edvinson
ID: 39256655
I figured this out. Simple mistake on my part by comparing the wrong value to user input.
0
 
LVL 1

Author Comment

by:edvinson
ID: 39256677
I've requested that this question be closed as follows:

Accepted answer: 0 points for edvinson's comment #a39256655

for the following reason:

Simple typo in code was the problem
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39256678
Which I believe I probably still helped you to find this typo error!?
0
 
LVL 1

Author Closing Comment

by:edvinson
ID: 39258153
always a great help to me mccarl, thx.
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39258212
Not a problem, glad to help!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question