Bad array initalisation?

Mar 26, 2010 at 4:59pm
Could someone tell me whats wrong with it?

want to make the array of strings, and then have it so that they are randomised, then i will start the main part of the program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31


#include<iostream>
#include<string>
#include<ctime>

using namespace std;

int main()
{
 
 
     
    
string words [12] ("blue","red","green","yellow","white","black","pink","orange","purple","brown");


 srand(time(NULL));

cout << words[rand()%10] << endl;
 
 
 
 
 
cout<<"\n\n";   
system ("pause");    
return 0;    
} 

Mar 26, 2010 at 5:04pm
closed account (S6k9GNh0)
Use string words [] = {"blue","red","green","yellow","white","black","pink","orange","purple","brown" }; instead or you could use a vector for less hardcoded programming.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
#include <ctime>
#include <vector>

int main()
{
    
   std::vector<std::string> words;
   words.push_back("blue");
   words.push_back("red");
   words.push_back("green");
   words.push_back("yellow");
   words.push_back("white");
   words.push_back("black");
   words.push_back("pink");
   words.push_back("orange");
   words.push_back("purple");
   words.push_back("brown");


   srand(time(NULL));

   cout << words[rand()%( (words.size()-1) - 0 + 1 ) + 0] << endl;
 
 
 
 
 
   cout<<"\n\n";
   //system ("pause");    // BIG NO NO. NEVAR
   getchar();
   return 0;    
} 
Last edited on Mar 26, 2010 at 8:01pm
Mar 26, 2010 at 5:25pm
You could also just randomize the array itself:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <algorithm>
#include <ctime>
#include <iostream>
#include <limits>
using namespace std;

int main()
  {
  // The list
  string words[] = { "blue", "red", "green", "yellow", "white", "black", "pink", "orange", "purple", "brown" };
  unsigned num_words = sizeof( words ) / sizeof( words[ 0 ] );

  // Randomize the list
  srand( (unsigned) time( NULL ) );
  random_shuffle( words, words + num_words );

  // Display the list
  for (unsigned n = 0; n < num_words; n++)
    cout << n << ": " << words[ n ] << endl;

  // Wait for user
  cout << "Press ENTER to quit." << flush;
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
  return 0;
  }

Hope this helps.
Mar 26, 2010 at 6:44pm
thanks for the help with the =

we didnt do vectors yet, sorry


i have this bit of code but just having a prob using it..

1
2
3
4
5
6
7
8
int numofChar;

numofChar = wordlenght.lenght();



cout<<wordlenght.lenght();



I just need to be able to find a random word at the beginning of the program from the array, must be different everytime.

And must then get the lenght of that word, im just having a problem with how to know what word was picked by the randomiser..

the random code dostn seem to be working for me..
Mar 26, 2010 at 8:23pm
closed account (S6k9GNh0)
1) You spelled "length" wrong.
2) The random code as in random_shuffle()? It works, all it does is rearrange where the elements are placed. I would think this is slower than using rand but I honestly don't know.
3) If you use random_shuffle(), just access the first word of the array via words[0]. If your using rand, then you should be able to place the random function inside of the brackets operator and access a random element like that.
Topic archived. No new replies allowed.