C sample help.

This is a discussion on C sample help. within the C++ Programming forums, part of the General Programming Boards category; It's a sample program that asks for a username and password. After three guesses the program shuts down if all ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    2

    C sample help.

    It's a sample program that asks for a username and password. After three guesses the program shuts down if all attempts are incorrect. The username is not supposed to be case sensitive; however, I can't seem to take case sensitivity off the username.

    It's just an assessment that tests my ability to make a user-defined function. It's not a major program. Can anyone help me take case sesitivity off the username?

  2. #2
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107
    Change the given username and the acceptable username to uppercase or lowercase, then compare them.

    Look at this page...
    http://www.mathworks.com/access/help.../strcmpi.shtml
    the function on the above page, strcmpi(), is probably in the string.h file.

    The first answer is what i would do if this function didn't exist.
    Last edited by BillBoeBaggins; 12-06-2003 at 03:27 PM.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    161
    Code:
    void noCase(char *s, int slen){
    	int i;
    	int c;
    	for(i=0;i<slen+1;i++)
    	{
    		c = (int) *s;
    		if (c>96 && c<123){c = c - 32; *s = (char)c;};
    		s=s+1;
    	}
    }
    Here is an example with the code above

    char username[]="MyNaMe";
    noCase(&username,6);

    Now username="MYNAME". The code makes everything be capitalized. For everything you do is for each character treat it as its capitalized. e.g. if you want to compare 2 strings. After the string when through my code it will all be capitalized. Then compare them.

    Hope that helps!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > for(i=0;i<slen+1;i++)
    Why include the \0 at the end of the string?

    > (c>96 && c<123)
    Use islower(c)

    > c = c - 32
    Use toupper(c)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Just convert everything to lowercase using the tolower() function found in <cctype>.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sample Software Development Proposal ?
    By khpuce in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 03-24-2008, 09:38 AM
  2. Bjarne's member function binding sample is wrong?
    By George2 in forum C++ Programming
    Replies: 9
    Last Post: 03-11-2008, 04:05 AM
  3. MSDN volatile sample
    By George2 in forum C++ Programming
    Replies: 38
    Last Post: 01-05-2008, 05:59 AM
  4. MSDN OLE DB Sample Provider entry point
    By George2 in forum C++ Programming
    Replies: 0
    Last Post: 07-21-2007, 07:30 AM
  5. looking for non-MFC based socket programming sample
    By George2 in forum Networking/Device Communication
    Replies: 1
    Last Post: 06-19-2006, 05:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21