Code Correctiong

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;

int main() {
string words[] = {"Tilted Towers","Shifty Shafts","Polar Peak","Pleasant Park","Loot Lake",Frosty Flights","Salty Springs","Dusty Diviot","Lazy Links","Tomato Temple","Haunted Hills","Happy Hamlet","Wailing Woods","The Block","Lonely Lodge","Paradise Palms","Soccer Field","Race Track","Ice Lake","Lucky Landing","Junk Junction","Snobby Shores","Retail Row","Retail Row Woods","Viking Mountain"};
srand(time(NULL));
cout << words[rand()%100] << endl;
Your words array has way less than 100 elements, so chances are you're going to go out of bounds.

You also should be #including the appropriate headers before int main().
1
2
3
4
#include <iostream> // cout
#include <string> // string
#include <cstdlib> // srand, rand
#include <ctime>  // time 


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).
Last edited on
Thanks!
1
2
3
4
5
6
7
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
= {"Tilted Towers","Shifty Shafts","Polar Peak","Pleasant Park","Loot Lake","Frosty Flights","Salty Springs","Dusty Diviot","Lazy Links","Tomato Temple","Haunted Hills","Happy Hamlet","Wailing Woods","The Block","Lonely Lodge","Paradise Palms","Soccer Field","Race Track","Ice Lake","Lucky Landing","Junk Junction","Snobby Shores","Retail Row","Retail Row Woods","Viking Mountain"};
srand(time(NULL));
cout << words[rand()%100] << endl;


Now for 5:1 it says expected primary-expression before '=' token
Last edited on
Uh, something went wrong.

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>.
Last edited on
1) You've ignored how Ganado advised you to use code tags, and invented your own. +5 points for inventiveness, but -50 points for effectiveness.

2) Look at the first line of your main() function, in your latest post. Surely it's obvious why you're getting that error?
For 4:1 it says 'string' was not declared in this scope
How would I fix this?
Last edited on
Post your code.
1
2
3
4
5
6
7
8
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
int main() {
string words[] = {"Tilted Towers","Shifty Shafts","Polar Peak","Pleasant Park","Loot Lake","Frosty Flights","Salty Springs","Dusty Diviot","Lazy Links","Tomato Temple","Haunted Hills","Happy Hamlet","Wailing Woods","The Block","Lonely Lodge","Paradise Palms","Soccer Field","Race Track","Ice Lake","Lucky Landing","Junk Junction","Snobby Shores","Retail Row","Retail Row Woods","Viking Mountain"};
srand(time(NULL));
cout << words[rand()%100] << endl;}
string is the std namespace, so it should be std::string.
1
2
3
4
5
6
7
8
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
int main() {
std::string words[] = {"Tilted Towers","Shifty Shafts","Polar Peak","Pleasant Park","Loot Lake","Frosty Flights","Salty Springs","Dusty Diviot","Lazy Links","Tomato Temple","Haunted Hills","Happy Hamlet","Wailing Woods","The Block","Lonely Lodge","Paradise Palms","Soccer Field","Race Track","Ice Lake","Lucky Landing","Junk Junction","Snobby Shores","Retail Row","Retail Row Woods","Viking Mountain"};
srand(time(NULL));
cout << words[rand()%100] << endl;}


8:1 'cout' was not declared in this scope 8:30: error: 'endl' was not declared in this scope 8:34: error: expected '}' at end of input

For 8:34, I put the '}'
And for future reference, how to I declare something in the scope?
Never mind. I figured it out and it runs! Thanks for your help!
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.
Last edited on
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.
Topic archived. No new replies allowed.