0

I'm creating a small program that allows the user to input 3 names (or whatever string they want). The program should then display all three strings (which is working), then it should use the rand() function to randomly display one of the three strings. This is the part that isn't functioning properly.

#include <iostream>
#include <string>

using namespace std;

void display(string[], int);

const int SIZE = 3;

int main()
{
    string names[SIZE];

    for (int i = 0; i < SIZE; i++)
    {
        cout << i + 1 << ": ";
        getline(cin, names[i]);
    }

    cout << endl;
    display(names, SIZE);

    int name = rand() % (2 + 1 - 0) + 0;
    cout << names[name];

    cin.get();

    return 0;
}

void display(string nm[], int n)
{
    int i = 0;

    for (i; i < n; i++)
    {
        cout << "Name " << i + 1 << ": ";
        cout << nm[i] << endl;
    }
}

I had it set up differently before, and it gave me an error, but after changing it to what it is now, it always gives me the last element [2].

Is this a code error, or is it just that rand() always gives the same output on the same system?

11
  • 5
    (2 + 1 - 0) + 0; what? Commented Jan 5, 2018 at 11:47
  • 2
    @manni66 the cppreference on rand() said this is how ranges are set up. I might have misunderstood this badly, but after reading the page 4 times that is what i got from that part. Basically the range should be from 0 - 2 to choose from 1 of the 3 elements Commented Jan 5, 2018 at 11:50
  • 1
    @Zharios instead? Did you read the linked page? Commented Jan 5, 2018 at 11:50
  • 3
    Do you think there happens some magic when you write (2 + 1 - 0) + 0; instead of 3;? Commented Jan 5, 2018 at 11:53
  • 3
    I don't know, on what cppreference you are looking, but mine doesn't show such a crap. And it also states how to use srand. Commented Jan 5, 2018 at 11:58

2 Answers 2

2

After some discussion in the comments, it became apparent that the issue was that I was not seeding the rand() function. Below is part of the code that was not functioning, corrected.

(Also, as a sidenote, to use the time() function, <ctime> or <time.h> has to be included.)

srand(time(NULL));
int name = rand() % 3;
cout << names[name];

(Thanks to @manni66 for pointing out that it was useless to include an overly complicated calculation to get the range for rand(), as it just had to be a single integer.

Sign up to request clarification or add additional context in comments.

Comments

1

seeding with current time works :

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cstdio>

using namespace std;

void display(string[], int);

const int SIZE = 3;

int main()
{
    string names[SIZE];

    for (int i = 0; i < SIZE; i++)
    {
        cout << i + 1 << ": ";
        getline(cin, names[i]);
    }

    cout << endl;
    display(names, SIZE);

    srand(time(NULL)); // use current time as seed for random generator
    int name = rand() % 3 ;
    printf(" random %i \n", name);
    cout << names[name];

    cin.get();

    return 0;
}

void display(string nm[], int n)
{
    int i = 0;

    for (i; i < n; i++)
    {
        cout << "Name " << i + 1 << ": ";
        cout << nm[i] << endl;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.