hangman

i have stored 50 words in .txt file and now want to any 5 random words in the memory using this code

1. ifstream fin("project.txt");
2. int i,n;
3. char s[5][50],a[50][50];
4.for(i=0;i<5;i++)
5. {
6. n=random(50);
7. fin>>a[n];
8. s[i]=a[n];
9. }

but error shows that" Lvalue required " in line 8.
can anyone tell me how to go about it ?? please its urgent
you should use strcpy function http://cplusplus.com/reference/clibrary/cstring/strcpy/
strcpy(s[i], a[n]);

and that's not how to post code in the forum, please read this
http://cplusplus.com/articles/firedraco1/
Last edited on
Is there a reason you are using char[][] instead of vector<string>?
strcpy did work .... thanks blackcoder

how am i supposed to use vector<string> ??
havent used it before
In that case, this question is more appropriate on the Beginners forum. You will get different help and answers there. In this forum we assume a fair level of knowledge about the language, which would certainly include vector and string.

I assume this is for a programming class. If that's the case, your C++ instructor should be lambasted for teaching C strings and arrays before std::string and vector.
closed account (S6k9GNh0)
Also, a big ass array like that on stack isn't the best way to go. The char array should be declared on heap or you should use a vector which uses an allocator for you.

You have an array of 50 c-strings that hold 50 characters. This is always going to be 50*50*sizeof(char) in size. Instead, we suggest you use a vector which is dynamic and doesn't originally take up as much space but can if it ends up requiring it to do so. It also has a built in iterator which for your use would almost be perfect. The vector is also probably the least complex of the STL containers and very easy to use: http://cplusplus.com/reference/stl/vector/

We do not know what you are coding but the way its coded seems a bit lacking in design and flexibility. We can't really tell though, so who knows?
Last edited on
Topic archived. No new replies allowed.