Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New at this - Looping through a String 1

Status
Not open for further replies.

JTinIowa

Programmer
Dec 7, 2004
4
US
I am trying to loop through all the characters from a string in an edit box to check them against certain values.

I have a small program I am working on. It accepts a string from a simple edit box:

Very simple edit box like this:

Enter Password: _______________


I am doing this in a Dialog Box, not main window if that makes any difference.

First I initialize my variable as follows:
Code:
char szPassword[25]={0};
and then I assign the value using this:
Code:
GetDlgItemText(hWnd, IDC_PASSWORD, szPassword, 25);
Ultimately I would like my code to resemble something like this psuedocode:

------------ psuedocode begin ------------

Begin Loop
First character a "T" or "t"?
Yes, continue
No, MessageBox invalid.

Second character a "E" or "e"?
Yes, continue

No, MessageBox invalid.
Third character a "S" or "s"?
Yes, continue

No, MessageBox invalid.

Fourth character a "T" or "t"?
Yes, Valid Password MessageBox
No, MessageBox invalid.
End Loop

------------ psuedocode end ------------

The problem I am running into is this, when I do a strcmp against szPassword[1] I am getting an error. It is complaining about converting the data type.

Anybody have any pointers for me on this one? Thank you for reading this.
 
Code:
if ( strcmp( szPassword, "test" ) == 0 ) {
  // it matches
}

There is no standard case-insensitive comparison, but common names (if it's supported by your compiler) are strcasecmp(), strcmpi() or stricmp()

--
 
Thanks Salem, but I was actually only using "TEST" as an example. The password will be a little more complex where I have to do some manipulations on each character based upon their serial number of the hard drive. For now, though, I am just trying to retrieve each character of the password.

For instance, how would I compare the 3rd character to the letter Y , and better yet, how would I retrieve the ASCII value of the letter?

Thanks!
JT
 
> For instance, how would I compare the 3rd character to the letter Y
Code:
if ( toupper(szPassword[2]) == 'Y' )


--
 
to get the ADCII avlue, just assing the cahr to an inmt:

char* str = "HELLO WORLD!";
int ele5 = str[4;

ele5 will be the ASCII value of the 5th element. or, im shure you could cast it like:

(int)str[4];

hope this helps,
~metalhead
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top