By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Charactor Array Problems

ilikepython
Expert 100+
P: 844
I am having trouble with this little piece of code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. string getword(string wordlist[]);
  8.  
  9.  
  10. int main() {
  11.     string wordlist[] = {"robbery", "soccer", "antibiotics"};
  12.     char word[20] = getword(wordlist);
  13.     cout << word << endl;
  14.     system("PAUSE");
  15.     return 0;}
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22. string getword(string wordlist[]){
  23.        srand((unsigned)time(0));
  24.        int num = rand()%3;   // needs to be length of list
  25.        string word;
  26.        word = wordlist[num];
  27.        return word;}
  28.  
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
Mar 23 '07 #1
Share this Question
Share on Google+
7 Replies


DeMan
100+
P: 1,806
Try dropping the size from your word array. That is try
Expand|Select|Wrap|Line Numbers
  1.  char word[] = getword(wordlist);
  2.  
Mar 23 '07 #2

ilikepython
Expert 100+
P: 844
Try dropping the size from your word array. That is try
Expand|Select|Wrap|Line Numbers
  1.  char word[] = getword(wordlist);
  2.  
I already tried that and it says : "initializer fails to determine size of word"
and the previous error

Any other ideas ?
Mar 23 '07 #3

DeMan
100+
P: 1,806
why not declare it as
Expand|Select|Wrap|Line Numbers
  1. string word = getword(wordlist);
  2.  
Mar 23 '07 #4

ilikepython
Expert 100+
P: 844
why not declare it as
Expand|Select|Wrap|Line Numbers
  1. string word = getword(wordlist);
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1.     char letter = 'e';
  2.     char word[] = "soccer";
  3.     for (int x = 0; x < 6; x++){
  4.         if (letter == word[x]){
  5.             cout << letter << " in " << word << endl;}}
  6.  
but is there a way to that with a string?
Mar 23 '07 #5

DeMan
100+
P: 1,806
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
Mar 23 '07 #6

ilikepython
Expert 100+
P: 844
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.
Mar 23 '07 #7

DeMan
100+
P: 1,806
No worries, That's what we're here for
Mar 23 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.