Comparing 2 char arrays

This is a discussion on Comparing 2 char arrays within the C++ Programming forums, part of the General Programming Boards category; im in the final stages of writing mastermind and im down to the white pegs i have 2 char arrays ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    18

    Comparing 2 char arrays

    im in the final stages of writing mastermind and im down to the white pegs
    i have 2 char arrays with 4 elements in each i.e

    userguess[4]
    answer[4] the answer is randomly generated

    i need to compare them with each other so if the user guesses for example R i need to search through answer and see if there are any R's more importantly which location they are in and if they are in the same location i.e

    userguess[0] = 'R'
    answer[0] = 'R'

    if they are in the same location i need to ignore it as that would generate a black peg however if they are in different locations i.e

    userguess[0] = 'R'
    answer[1] = 'R' or answer[2],answer[3]

    then i need to add 1 to my white count. I got that working however my problem comes when there is more than 1 'R' in answer[x] i tried using the following method which went wrong

    if guess[0] = answer[1],answer[2],answer[3]
    guess[1] = answer[0],answer[2], answer[3]
    etc.......

    i know thats not the correct syntax! somehow i think i need to change it so if answer1, 2 or 3 are the same it deducts 1 from the white count? i tried this with lots of if statements it compiled but crashed every time it tried to work out the white count. i would post my code but its seriously long. Can anyone point me in the right direction even if its a different way of doing it sorry for the essay!! thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Consider using nested loops, then check if guess[i] == answer[j], where the loop indices are named i and j.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    18
    i tried this and failed
    Code:
            int a=0;
    	int i;
    	int j;
    	for(i=0;i<4;i++)
    	{
    		if(userguess[0]==answer[i])
    		{
    			a++;
    		}
    		for(j=0;j<4;j++)
    		{
    			if(userguess[j]==answer[j])
    			{
    				a--;
    			}
    		}
    	}
    	for(i=0;i<4;i++)
    	{
    		if(userguess[1]==answer[i])
    		{
    			a++;
    		}
    		for(j=0;j<4;j++)
    		{
    			if(userguess[j]==answer[j])
    			{
    				a--;
    			}
    		}
    	}
    	for(i=0;i<4;i++)
    	{
    		if(userguess[2]==answer[i])
    		{
    			a++;
    		}
    		for(j=0;j<4;j++)
    		{
    			if(userguess[j]==answer[j])
    			{
    				a--;
    			}
    		}
    	}
    	for(i=0;i<4;i++)
    	{
    		if(userguess[3]==answer[i])
    		{
    			a++;
    		}
    		for(j=0;j<4;j++)
    		{
    			if(userguess[j]==answer[j])
    			{
    				a--;
    			}
    		}
    	}
    	white = a;
    Epic fail lol

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    18
    just read your post again and can already see thats not what you ment

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    18
    just working through my logic on paper and realised its wrong which puts me back to square one anyone have any ideas?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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