random friend to text using an array?

hi i need help figuring out how to find the a random friend to text, i must input a seed ex. 21, 42, 101. . . but i can only input a number of 5 friends. how can i do both at the same time? i need help. and after i find the random friend then i must ouput the friends that did not get picked. this is what i have so far and i think i am in the right track. please help me thank you.

i am using c++

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
35
36
37
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

string get_random_name(string arr[], int nums)
{
    int num = rand() % nums;
    string name = arr[num];
    return name;
}

int main()
{
    srand(time(NULL));
    string names[6];
    int num_of_names;

    cout << "== Who Should I Text? ==" << endl;
    cout << "Enter seed" << endl;
    cin >> num_of_names;

    for (int counter = 0; counter < num_of_names; counter++)
    {
        cout << "Enter friend " << counter << endl;
        cin >> names[counter];
    }

    cout << "You should text: " << get_random_name(names, 6) << endl;

    cout << "These other friends didn't make the cut:" << endl;



}
Last edited on
If you want C++ random numbers read this http://www.cplusplus.com/reference/random/

With the picked name, you need to return the num that you get using random mechanism.
So that you can skip printing name at that index but print the entire string array otherwise.

Also, the name "integers" for array of strings is confusing, name it something like... "names"?
There are other considerations as well, for one the program is not going to work if the user enters 7 for num_of_names, the array can hold only 6 names.
Last edited on
i only want to input 5 names. how can i make it work if i input a number like 101
21,42,101 are called seed numbers in the program i am trying to complete... so i am still stuck :/
Use vector of strings instead of array of strings that way you can keep on adding the names that are entered

Read about vectors here http://www.cplusplus.com/reference/vector/vector/
we havent learned vectors yet in my class so i have to stay within what we were taught. :/
string *names; instead of string names[6];

Then names = new string[desired_number_of_names];
then run the loop from 0-desired_number_of_names-1
Last edited on
please show me an example. of how everything should be layed out please. i am confused. . .so when i make string *names, do i also add the "=" new string []??
is this what you mean. i only added the new string. then idk what else to do.

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
35
36
37
38
39
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

string get_random_name(string arr[], int nums)
{
    int num = rand() % nums;
    string name = arr[num];
    return name;
}

int main()
{
    srand(time(NULL));
// new string you told me to add
    string *names;
    string num_names[6];
    int num_of_names;

    cout << "== Who Should I Text? ==" << endl;
    cout << "Enter seed" << endl;
    cin >> num_of_names;

    for (int counter = 0; counter < num_of_names; counter++)
    {
        cout << "Enter friend " << counter << endl;
        cin >> names[counter];
    }

    cout << "You should text: " << get_random_name(names, 6) << endl;

    cout << "These other friends didn't make the cut:" << endl;



}
Topic archived. No new replies allowed.