I am a BIG C++ noob, and I am trying to create a code that randomly picks a word from a list. Can someone tell me what is wrong with this?
#include <iostream>
using namespace std;
Btw, please use code formatting tags, you can edit your post and and add [code] and [/code] around your code.
PS: If you want it still work without crashing, you can do string words[100] = {"test element", "test element 2", "etc" };
Because the size is explicitly given, the rest of the elements will be default initialized (empty strings, in this case).
Try this: Edit your post (bottom right corner), delete your code, paste in your code again, and then highlight it with your cursor. Once you've highlighted it, press the "<>" button on the right-hand side of the menu.
Now for 5:1 it says expected primary-expression before '=' token
You accidentally deleted the "string words[]" part of your code.
You should also #include all the headers I mentioned before. <iostream>, <string>, <cstdlib>, and <ctime>.
cout is in the std namespace, so it should be std::cout.
endl is in the std namespace, so it should be std::endl.
Are you starting to detect a pattern here?
And for future reference, how to I declare something in the scope?
I'm not sure what you mean. When you declare a thing, the scope it's declared in depends on the context of the line where you declare it. Can you be more specific about what you mean?
Do you mean that you want everything in the std:: namespace pulled into the global namespace? If so, you do it the same way you did it in your first post. I don't understand why you took it out, if you wanted it in.
Also, the run-time problem that I originally spoke of still is happening.
Your words array has less than 100 elements in it, so accessing a random index with rand()%100 has a good chance of going out of bounds.
Code Noob wrote:
how to I declare something in the scope
MikeyBoy wrote:
I'm not sure what you mean.
Heh, his question stems from the fact that the error message says "X was not declared in this scope", so one way of interpreting that is to say, "well then how do I declare X in this scope". But as you mentioned, it isn't exactly the right question to ask.
Code Noob, basically, "X was not declared in this scope" just means that the compiler is saying "I can't find where you ever declared X to be a thing". Since cout, endl, string, etc. are all in the std namespace, it doesn't know what those are, unless you do what Mikey Boy suggested.