473,386 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Charactor Array Problems

ilikepython
844 Expert 512MB
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
7 1403
DeMan
1,806 1GB
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
844 Expert 512MB
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
1,806 1GB
why not declare it as
Expand|Select|Wrap|Line Numbers
  1. string word = getword(wordlist);
  2.  
Mar 23 '07 #4
ilikepython
844 Expert 512MB
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
1,806 1GB
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
844 Expert 512MB
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
1,806 1GB
No worries, That's what we're here for
Mar 23 '07 #8

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

Similar topics

2
by: Alex Shi | last post by:
In php, is there a way obtain the width of a charactor of a certain font? Alex -- ================================================== Cell Phone Batteries at 30-50%+ off retail prices!...
12
by: shailashri_sk | last post by:
Hi, int *p; p++; here p now increments itself with the size of integer. similarly, I wanted to know, how to declare an pointer to an array ( say array of integers) where in it we do a p++ it...
8
by: engaref | last post by:
Hello Every body, I am new with C programming.I have received the Problems from my advisor on Array but I did not find any Proper answer yet. If Possible,please make a solution for the Problems....
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
7
by: Yongjian | last post by:
Hello, I want to read a text file which contains French charactor, then modify some of the sentence, save it as a new text file. But I found the new text file lost all the French charactors....
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
3
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to...
3
by: inkexit | last post by:
I need help figuring out what is wrong with my code. I posted here a few weeks ago with some code about creating self similar melodies in music. The coding style I'm being taught is apparently a...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
1
by: sandipvdhande | last post by:
I have problem in printinh the extended ascii charactors to file in C.Please tell me the solution for this issue .Is there any function to convert the charactor ..or is there any function to change...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.