Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

C++ Comparing character arrays

Question posted by: mwhit74 (Newbie) on July 13th, 2008 03:57 PM
working on a 'hangman' type project. i need to know how to compare the character arrays so i can "return true", i.e. the word was correctly guessed, or false if they weren't.

below is what i have so far but all it does is compare individual cells and not the whole string.

any help would be awesome, and just FYI i am a beginning programmer.



Expand|Select|Wrap|Line Numbers
  1. bool matchTemplate ( const char secretWord[], const char guessTemplate[] )
  2. {
  3.  
  4.     //  will return true if secretWord and guessTemplate contain the same
  5.     //  C++ string and will return false otherwise
  6.  
  7.     //   ONLY ONE LOOP IS ALLOWED IN THIS FUNCTION
  8.  
  9.  
  10.  
  11.     for ( int i = 0; i < MAX_SIZE; i++ )
  12.     {
  13.         if ( secretWord[i] == guessTemplate[i] )
  14.             return true;
  15.         else
  16.             return false;        
  17.     }
  18.  
  19. }
JosAH's Avatar
JosAH
Chief Editor
7,781 Posts
July 13th, 2008
04:10 PM
#2

Re: C++ Comparing character arrays
You can either use plain old C strings (the strcmp function would do the job for you)
or use C++ string objects (where the compare function would help you out).

kind regards,

Jos

Reply
mwhit74's Avatar
mwhit74
Newbie
10 Posts
July 13th, 2008
04:13 PM
#3

Re: C++ Comparing character arrays
[QUOTE
or use C++ string objects (where the compare function would help you out).

kind regards,

Jos[/QUOTE]

ok i understand i have to compare the two strings, and i dont know how to convert the char arrays to strings

Reply
JosAH's Avatar
JosAH
Chief Editor
7,781 Posts
July 13th, 2008
04:32 PM
#4

Re: C++ Comparing character arrays
Quote:
ok i understand i have to compare the two strings, and i dont know how to convert the char arrays to strings


Are you programming in C or are you programming in C++? both languages
have their own solutions for 'strings'. C uses the notion of a simple character
array where the last character is a '\0' (just a byte/char with all bits zero) and
C++ uses the 'string' class for the representation of strings.

kind regards,

Jos

Reply
mwhit74's Avatar
mwhit74
Newbie
10 Posts
July 13th, 2008
04:36 PM
#5

Re: C++ Comparing character arrays
Quote:
Are you programming in C or are you programming in C++? both languages
have their own solutions for 'strings'. C uses the notion of a simple character
array where the last character is a '\0' (just a byte/char with all bits zero) and
C++ uses the 'string' class for the representation of strings.

kind regards,

Jos


i am programming in C++. i am not sure if you are understanding the question i am getting at.... i have char arrays and i need them to be strings but i dont know how to convert from char arrays to strings so i can compare them.

Reply
JosAH's Avatar
JosAH
Chief Editor
7,781 Posts
July 13th, 2008
04:40 PM
#6

Re: C++ Comparing character arrays
Quote:
i am programming in C++. i am not sure if you are understanding the question i am getting at.... i have char arrays and i need them to be strings but i dont know how to convert from char arrays to strings so i can compare them.


Don't worry, I do understand ;-) There is a string constructor that takes an ordinary
char array and the number of chars that should be converted to a string object.
Have a look at this reference.

kind regards,

Jos

Reply
newb16's Avatar
newb16
Member
98 Posts
July 14th, 2008
11:27 AM
#7

Re: C++ Comparing character arrays
Quote:


I don't think his prof will be happy with something like if(std::string(secretWord)== std::string(guessTemplate)) { ... }
It's op's own responsibility to understand how strings are represented as arrays and write loop-based strcmp routine.

Reply
oler1s's Avatar
oler1s
Expert
630 Posts
July 14th, 2008
01:16 PM
#8

Re: C++ Comparing character arrays
Quote:
It's op's own responsibility to understand how strings are represented as arrays and write loop-based strcmp routine.
There's no evidence that this project has a "use only C strings and no cstring allowed" clause. Writing string handling routines is an excellent learning exercise, but I doubt this is it.

Reply
newb16's Avatar
newb16
Member
98 Posts
July 14th, 2008
01:59 PM
#9

Re: C++ Comparing character arrays
Quote:
There's no evidence that this project has a "use only C strings and no cstring allowed" clause. Writing string handling routines is an excellent learning exercise, but I doubt this is it.


Then what is the following comment for in case of std::string allowed?
// ONLY ONE LOOP IS ALLOWED IN THIS FUNCTION

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,938 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top C / C++ Forum Contributors