Comparing Character Arrays problem...

This is a discussion on Comparing Character Arrays problem... within the C++ Programming forums, part of the General Programming Boards category; Hi Started c++ at uni (doing games programming) 5 weeks ago....its way tougher than i expected !!! Anyway....been on with ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    10

    Comparing Character Arrays problem...

    Hi
    Started c++ at uni (doing games programming) 5 weeks ago....its way tougher than i expected !!!

    Anyway....been on with this for ages and i cant get it to work....i have 2 names stored in a character array, and the user inputs into the aryInput array and i need it to verify that what has been entered is in the aryLoginName array....my problem is that if John is input, it outputs correctly, if Steve is entered it says invalid name...so it doesnt seem to be looping (?)....let me know if i havent explained it clearly....heads in a spin after working on the thing for hours...thanks!:

    Code:
    void login()
    {
    	char aryInput[10];
    	char aryLoginName [2] [10] =    {	"John",
                                                                                   "Steve",
                                                                     };
    
    
    		cout << "Please enter a valid name> ";
    		cin >> aryInput;
    		
    		for(int k = 0; k < 2; k++) //  names
    		{
    			int temp = strcmp( aryInput, aryLoginName[k]);
    
    			if( temp == 0)
    			{
    				cout << "You entered the correct name" << endl;
    				break;
    			}else cout << "Invalid name";
    			break;
    		}
    }
    Last edited by newy100; 11-16-2003 at 07:35 PM.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Take out the break statement after the else.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    10
    Thanks for the quick reply, wasnt expecting one that fast!!

    I have taken it out (dunno why i had it there to start with ??) and it does loop twice, but the name Steve will be on the second loop around i think, and so when Steve is input it ouputs:
    "Invalid Name You entered the correct name"
    which is looping through the first time, sees that steve isnt there and ouputs the else statement, and the second time it finds it and ouputs that it is correct......had this problem about 11am and been through a full circle back to it lol

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You could do something like:

    Code:
    for(int k = 0; k < 2; k++) //  names
    {
    int i=0;
       while ( (strcmp( aryInput, aryLoginName[i] ) != 0) ) && (i<2) )
           i++;
      if (i==2)
         cout << "Invalid name";
      else
         cout << "You entered the correct name" << endl;
     }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    What you're gonna need to do is keep a flag as to whether the username is valid or not. Then, outside of the loop, just check the flag and output the appropriate message.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 12:27 PM
  2. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 03:24 AM
  3. Comparing elements of character pointer arrays
    By axe in forum C Programming
    Replies: 2
    Last Post: 11-14-2007, 12:20 AM
  4. Just a quick question about character arrays
    By Welshy in forum C Programming
    Replies: 3
    Last Post: 04-03-2006, 08:20 AM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM

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