Charactor Array Problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    Charactor Array Problems

    I am having trouble with this little piece of code:
    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    
    using namespace std;
    
    string getword(string wordlist[]);
           
    
    int main() {
        string wordlist[] = {"robbery", "soccer", "antibiotics"};
        char word[20] = getword(wordlist);
        cout << word << endl;
        system("PAUSE");
        return 0;}
        
    
    
    
    
    
    string getword(string wordlist[]){
           srand((unsigned)time(0));
           int num = rand()%3;   // needs to be length of list
           string word;
           word = wordlist[num];
           return word;}
    The error that I'm getting is this : "invalid initializer" on line 13.
    Can somebody tell me what I'm doing wrong and where the problem is??
    I'm sorry if it's really obvious but I'm new to C++.

    Thanks in advance
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    Try dropping the size from your word array. That is try
    Code:
     char word[] = getword(wordlist);

    Comment

    • ilikepython
      Recognized Expert Contributor
      • Feb 2007
      • 844

      #3
      Originally posted by DeMan
      Try dropping the size from your word array. That is try
      Code:
       char word[] = getword(wordlist);
      I already tried that and it says : "initialize r fails to determine size of word"
      and the previous error

      Any other ideas ?

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

        #4
        why not declare it as
        Code:
        string word = getword(wordlist);

        Comment

        • ilikepython
          Recognized Expert Contributor
          • Feb 2007
          • 844

          #5
          Originally posted by DeMan
          why not declare it as
          Code:
          string word = getword(wordlist);
          Well I thought of that but since I am trying to make a hangman game, I thouhgt it would be easier to see if a letter is in a word like this:
          Code:
              char letter = 'e';
              char word[] = "soccer";
              for (int x = 0; x < 6; x++){
                  if (letter == word[x]){
                      cout << letter << " in " << word << endl;}}
          but is there a way to that with a string?

          Comment

          • DeMan
            Top Contributor
            • Nov 2006
            • 1799

            #6
            If word is a string you can access individual characters, I think you use at as in:

            word.at(i); /* You may wan to double check this returns a character, but I'm fairly confidebnt it does */
            you also have size or end operators so you know if you are ate the end of a string

            Comment

            • ilikepython
              Recognized Expert Contributor
              • Feb 2007
              • 844

              #7
              Originally posted by DeMan
              If word is a string you can access individual characters, I think you use at as in:

              word.at(i); /* You may wan to double check this returns a character, but I'm fairly confidebnt it does */
              you also have size or end operators so you know if you are ate the end of a string
              It does return a character and works fine.
              Thank you very much.

              Comment

              • DeMan
                Top Contributor
                • Nov 2006
                • 1799

                #8
                No worries, That's what we're here for

                Comment

                Working...