-2
string quotes[2];
        quotes[0] = "This is number one";
        quotes[1] = "this is two";
        quotes[2] = "and three";

            cout << quotes[rand() % 2] << " " << endl;
            cout << endl;

Hi guys,

I'm trying to make it so a random string in the array prints out.

I'm very new to c++ and this looks correct to me. But when run, I get 1 and then 0 display?

MrTurvey
  • 23
  • 3

5 Answers5

4

The problem is that your array does not have enough space:

string quotes[2];

means the valid indexes are 0 and 1. Index 2 is past the end of the array.

When you intend to initialize all elements of an array, you can skip its dimension, like this:

string quotes[] = {
    "This is number one"
,   "this is two"
,   "and three"
};

In addition, if you would like to get a random number between 0 and 2, inclusive, use %3 instead of %2:

cout << quotes[rand() % 3] << " " << endl;
cout << endl;
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

You declare string quotes[2], string quotes[2] will be out of the array range.It should string quotes[3] or you just comment //quotes[2] = "and three"; because rand()%2 will never access quotes[2].

notbad
  • 2,797
  • 3
  • 23
  • 34
1

declare your array to have 3 elements

string quotes[3];

and also change your indexer to have the posibility of hitting all 3 elements

...quotes[rand() % 3]...
0

I'm very new to c++ and this looks correct to me.

Ok, so let me tell you something: in C++ we don't use C style arrays. Period. If you really want fixed sized arrays use std::array. If you want something more dynamic: use std::vector. If you want fixed size array of bool, use std::bitset, and if you want it dynamic, use boost::dynamic_bitset.

But when run, I get 1 and then 0 display?

Of course you do. The % (modulus) operator will return the remainder of the division between the left and right hand side. For example: 3 % 2 = 1 and 2 % 2 = 0. In your example, since 2 % 2 = 0, rand() % 2 will always return either 0 or 1 (never 2); which is perfect, since your array only contains 2 elements of index 0 and 1.

If what you meant is to have an array until the 2nd index, then you meant to declare an array of 3 items:

std::array<std::string, 3> quotes;

and then define the "randomizer" to:

std::cout << quotes[rand() % 3] << " " << std::endl;

Also, please, take a look at this answer that will point to you to the flaws of using rand() with the modulus operator, and how to fix them.

Community
  • 1
  • 1
Shoe
  • 74,840
  • 36
  • 166
  • 272
  • *in C++ we don't use C style arrays. Period.* If only I could say this were true. Anyway, if using `std::array`, might as well use ``. – chris Dec 12 '13 at 22:28
  • @chris, hope is the last thing to die. – Shoe Dec 12 '13 at 22:34
0

You have your array set to contain 2 elements but assign it 3 elements. In addition you have rand() generating 2 values (0, and 1) instead of the needed three values. (0, 1, and 2) I am unsure if this is a problem for you but the rand() isn't seeded and thus it will generate the same order of numbers each time. A possible revision of your code is below:

#include <ctime>
string quotes[3];
    quotes[0] = "This is number one";
    quotes[1] = "this is two";
    quotes[2] = "and three";
srand(time(NULL)); //Seed the rand();
        cout << quotes[rand() % 3] << " " << endl;
        cout << endl;

Hope this helps.

Lilith Daemon
  • 1,473
  • 1
  • 19
  • 37