Code Correctiong

Jan 17, 2019 at 3:01pm
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;
Jan 17, 2019 at 3:06pm
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 Jan 17, 2019 at 3:16pm
Jan 17, 2019 at 3:23pm
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 Jan 17, 2019 at 3:35pm
Jan 17, 2019 at 3:32pm
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 Jan 17, 2019 at 3:36pm
Jan 17, 2019 at 3:33pm
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?
Jan 17, 2019 at 3:40pm
For 4:1 it says 'string' was not declared in this scope
How would I fix this?
Last edited on Jan 17, 2019 at 3:46pm
Jan 17, 2019 at 3:43pm
Post your code.
Jan 17, 2019 at 3:47pm
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;}
Jan 17, 2019 at 3:49pm
string is the std namespace, so it should be std::string.
Jan 17, 2019 at 3:56pm
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?
Jan 17, 2019 at 4:07pm
Never mind. I figured it out and it runs! Thanks for your help!
Jan 17, 2019 at 4:08pm
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 Jan 17, 2019 at 4:10pm
Jan 17, 2019 at 5:27pm
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.