GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Feb-2009, 09:31
The sinner The sinner is offline
New Member
 
Join Date: Feb 2009
Posts: 3
The sinner is on a distinguished road

Can't compare the contents of arrays


i realy need help with this code
i made it but i can't compare the contents of the arrays

i need to compare it to know if the entered word is the same as the given word

CPP / C++ / C Code:
#include <conio.h>
#include<stdio.h>
#include <string.h>
main()
{

char word[20],e_word[20],ch,*found;
int l_word,letter_location,i,number,score;


score = 0;


puts("Enter any word :");
scanf("%s",&word);


l_word =strlen(word );

printf("%s has %d characters \n ", word , l_word );


clrscr();
printf("please enter a character to guess the word : \n");

 ch = '_';
for(i=0;i!=l_word;i++)
	 {
		  printf("\n %c ",ch);

	 }
	 gotoxy (10 , 10);

		printf("please start entering the letters: \n");


		  for(number=0;number!=l_word;i++)
	 {

		  number=number+1;    /* increment the letter number to each time to got to the next location */
		 // a=0;
		 // b=0;
		  printf("\n\n %d LETTER\n",number); /* it shows how many letters the word contain */
		  scanf("%s",&e_word[number]);          /* scan letteres from the keyboard to enter in the new array "e_word" */
		  found=strchr(word,e_word[number]); /* use the found pointer to assign and compare the letters from the given word in array[word] and the written letters in the new array [e_word]*/


		  scanf("%c",&e_word[letter_location]);
		  printf("\n");
	}

	 for(letter_location=0;letter_location<l_word;letter_location++)
		{
		letter_location=letter_location+1;  /* assign the letter location as 1 so you can start entering the letteres */


		 }

		 }

please help

P.S i'm using Turbo C ++
Last edited by LuciWiz : 02-Feb-2009 at 09:38. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 02-Feb-2009, 10:37
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Can't compare the contents of arrays


You are doing several things wrong here. For one, when using scanf() to read in a string, you pass the array of characters to scanf(), not the address of the array.

To answer your question, you can compare two strings with strcmp().
  #3  
Old 02-Feb-2009, 12:17
The sinner The sinner is offline
New Member
 
Join Date: Feb 2009
Posts: 3
The sinner is on a distinguished road

Re: Can't compare the contents of arrays


Thnx ok i modified it and used the strcmp() function but i'm still facing some problems

CPP / C++ / C Code:
#include<conio.h>
#include<string.h>
#include <stdio.h>

main()
{
	 char word[30],e_word[30],*found;
	 int loop,word_length,i;

	 printf("\n ENTER THE WORD: ");
	 gets(word);

	 i=0;


	 word_length=strlen(word);

	 char h;

	 clrscr();
	 printf("\n LET's START: ");
	 h='_';
	 printf("\n LOOK HERE: ");
	 for(loop=0;loop!=word_length;loop++)
	 {
		  printf("\n %c ",h);
	 }
	 for(loop=0;loop!=word_length;loop++)
	 {

		  i=i+1;

		  printf("\n\n %d LETTER\n",i);
		  scanf("%s",&e_word[i]);
		  found=strchr(word,e_word[i]);
		  }
			while (strcmp (word,e_word) == 0);
				printf("\n OK\n");

		  while (strcmp (word,e_word) != 0);
				printf("\n NO\n");



	 printf("\n THE WORD IS '%s'\n",word);
	 getch();
	 return 0;
}
Last edited by LuciWiz : 02-Feb-2009 at 15:08. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #4  
Old 02-Feb-2009, 13:16
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Can't compare the contents of arrays


Quote:
Originally Posted by The sinner
...i modified it and used the strcmp() function but i'm still facing some problems...
What are the problems? Please be specific.
  #5  
Old 03-Feb-2009, 13:29
The sinner The sinner is offline
New Member
 
Join Date: Feb 2009
Posts: 3
The sinner is on a distinguished road

Re: Can't compare the contents of arrays


it still isn't comparing the two arrays and there is a logical mistake that i can't find
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble integrating console code into GUI Barman007 Java Forum 18 15-May-2008 14:05
How to sort random access file? wmmccoy0910 C Programming Language 12 04-Sep-2006 04:40
Here it is again! 35% - 40% off For Life! my-e-space Web Hosting Advertisements & Offers 0 20-Apr-2006 15:48
Guidelines for posting requests for help - UPDATED! WaltP C Programming Language 0 21-Apr-2005 03:44

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:46.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.