Thread: Accessing a char array, and comparing a letter, to each letter of a word.

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    21

    Accessing a char array, and comparing a letter, to each letter of a word.

    Hello, I know this is a super easy fix, I just cant for the life of me figure it out. I have and array,

    word[100];
    using an input function (not gets() don't worry) to put lets say 'superman' inside, need to compare a single character to each letter of the word 'superman'. I'm planning on using recursion to compare each character. Is there a way to set and integer equal to the first letter of the name 'superman'? And add one to it each time it loops till I find what i'm looking for.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Hi, welcome!

    This is C++, so why not using string ?

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    21
    A string other than an array? Is that what your saying?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by gipper
    A string other than an array? Is that what your saying?
    As in use a std::string object rather than a null terminated C-style string stored in an array of char. Anyway, that is secondary to the problem that you are trying to solve.

    Quote Originally Posted by gipper
    need to compare a single character to each letter of the word 'superman'.
    After you compare, what do you want to do? Like, if the character is equal to the first character of the word "superman", then what? If not, then what?

    Quote Originally Posted by gipper
    Is there a way to set and integer equal to the first letter of the name 'superman'?
    Yes, but that is probably not what you want to do.

    Quote Originally Posted by gipper
    And add one to it each time it loops till I find what i'm looking for.
    It sounds like you want a counter that starts from 0 and ends when it reaches the string length.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You write code in C++. Right?

    In C for example you could have
    Code:
    char word[100] = "bla";
    Then you would use string.h library for comparing , copying strings etc.

    In C++ , string handling becomes way more easier by the string class i gave you above.
    For example
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
       string first = "Hello";
       string second = "world";
       string str = first+ " " + second;
    
       cout<< str << endl;
    
       return 0;
    }
    this will output Hello world.
    In C you would use functions from the library to achieve the same result.
    If you play with C++, you should learn to handle the string class

    EDIT : laserlight comes first for second time today sorry

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    21
    In the class I'm in I was assigned to write hang-man. So in the game I have char word[100] for the word that the user is guessing. And im taking in a single character every time the user guesses a letter. I would like to recursively check and see if the letter is equal to the word. If it is, then i need to print the letter to the screen, along with the underscores for the missing letters.

    So I thought i could use something like this,
    Code:
    int i = 0;
    
    while(letter != word[i]){
    i++;
    printf("_ ");
    }
    I thought i have done something like this before. But it has been awhile..
    Thanks for the reply!

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    21
    I honestly dont know how I can do that, i do know that i cant compare an integer and a char. Thats what is happening here.

    Is there some kind of pointer that i could make to point to the location of the letter in the word?
    If so how would i initialize that?

    Sorry if this is easy stuff, I have only been doing this for a year(ish)
    Haha, still learning. Also, is there any good books you guys think I should read? I'm just looking for advice and tips from people with more experience. And am always open for constructive criticism.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by gipper View Post
    i do know that i cant compare an integer and a char. Thats what is happening here.
    Is it? is letter really defined as an int? Why would it be defined as an int instead of a char? Could you show more of your code please?

    In C++ one would normally use std::cout rather than printf.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Where did you see comparing an integer and a char? are you being confused by the index? Also there is an interchangeable nature to the values of a char / int as far as a computer is concerned anyway

    Is there some kind of pointer that i could make to point to the location of the letter in the word?
    like the index of an array ?- or equivalent in string object

    the char at postion [0] in the word '"letter" is 'l'

    so if you swap 0 for i (which is - and has to be, an integer - and also without holding negative values..)
    then you can index each of the chars in your string, and retrieve them by referring
    to the position indicated by i
    Last edited by rogster001; 11-23-2012 at 04:08 PM. Reason: erroneous reference to previous post
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    21
    I declared letter as a char, and i as an int. The idea was to use i as an index to the array word[].
    If 'i' is equal to 0, and i say word[i] then it should access the first letter in the array (assuming that the array has a word in it),
    correct?

    Because thats what I was doing and it would spit some error talking about comparison with 'letter' and 'i'.
    Saying that 'letter' is a character and 'i' is an int.

    Thats where I got the compiler error.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by gipper
    I would like to recursively check and see if the letter is equal to the word.
    That does not make sense. Considering that this is hangman, what you want to do is to check to see if the word contains the letter.

    This may well be the source of your confusion: the letter that the user enters is a char. Each letter in the word is also a char. Therefore, you want to compare a char with another char, not a char with an int. However, you use an int as an index of the string, i.e., the integer index i to access the ith letter of the word.

    Quote Originally Posted by gipper
    Because thats what I was doing and it would spit some error talking about comparison with 'letter' and 'i'.
    Saying that 'letter' is a character and 'i' is an int.
    What is your current code that causes this error?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by gipper View Post
    I declared letter as a char, and i as an int. The idea was to use i as an index to the array word[].
    If 'i' is equal to 0, and i say word[i] then it should access the first letter in the array (assuming that the array has a word in it),
    correct?
    Absolutely, 100%

    Because thats what I was doing and it would spit some error talking about comparison with 'letter' and 'i'.
    Saying that 'letter' is a character and 'i' is an int.

    Thats where I got the compiler error.
    That's impossible. You were either mistaken about what line it was referring to, or you were mistaken about what it was telling you, or you were mistaken about something else.
    Please post the relevant code code and actual copy and pasted error messages.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    I dont see any call for using recursion here - excuse pun.. unless you just wish to do it as an interesting exercise.
    As laserlight suggested earlier you just want to check from 0 to len of string - if your char input matches then mark a hit.
    Also bear in mind
    ('A' == 'a') evaluates false to a computer.
    Last edited by rogster001; 11-24-2012 at 04:12 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Capitalize first letter of a word (function)
    By xwielder in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2011, 11:11 PM
  2. make a char display a word or letter
    By dyelax in forum C++ Programming
    Replies: 12
    Last Post: 10-13-2009, 11:55 AM
  3. Capitalize first letter of every word in .txt file
    By crazygopedder in forum C Programming
    Replies: 9
    Last Post: 10-15-2008, 12:09 PM
  4. identifying first letter of word
    By speedyboy in forum C Programming
    Replies: 11
    Last Post: 03-19-2006, 07:56 AM
  5. Capatalizing the first letter of a word
    By cprog in forum C Programming
    Replies: 0
    Last Post: 12-07-2002, 06:58 PM

Tags for this Thread