outputting remainder of random names

Pages: 12
ok i know how to find the random name and that part of the program is completed, now i need to output the remainder of the names that are left of the input,
ex: names
cat
mario
anakin
master chief
bioshock
random name: mario

now i need to out put the names that did not get chosen.
ex: names
cat
anakin
master chief
bioshock.

please help me i am stuck. i need to figure this out and one other thing, but i think i can do that later. it is entering a seed. the seed can be up to 150, but the problem is i can only input 5 random names(MAX).


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
#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 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 >> num_names[counter];
    }

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

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



}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
string get_random_name(string arr[], int& nums)
{
    int num = rand() % nums;
    string name = arr[num];
    arr[num] == arr[nums - 1];
    --nums;
    return name;
}
//Later after you have chosen your random name:
for(int i = 0; i < nums; ++i)
    std::cout << num_names[i] << '\n';
Last edited on
where do i add the for loop in? in the function or in my main? i added it in my main but the chosen name still comes out in the output. i want to remove that name from the output list, how should i do this should i make it take subtract the chosen name from the list?, and another question. now since i have that done. how can i add in a seed input? like if i input a number 151, how can i make it so that i can only input 5 names Maximum?
Last edited on
this is the new code that i have the problem is on lines. . . 38-42

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
40
41
42
43
44
45
46
47

#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];
    arr[num] == arr[nums - 1];
    --nums;
    return name;


}

int main()
{
    srand(time(NULL));
    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 >> num_names[counter];
    }


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

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

    for (int i = 0; i < num_of_names; ++i)
    {
        cout << num_names[i] << endl;
    }


}
Last edited on
Find the difference:
Your code: string get_random_name(string arr[], int nums)
My code: string get_random_name(string arr[], int& nums)
it is telling me i cannot change the string num_names[6] to the funtion (int& nums) problem is at line with //

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
40
41
42
43
44
45
#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];
    arr[num] == arr[nums - 1];
    --nums;
    return name;


}

int main()
{
    srand(time(NULL));
    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 >> num_names[counter];
    }


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

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

    for (int i = 0; i < num_of_names; ++i)
    {
        cout << num_names[i] << endl;
    }
    return 0;

}
Last edited on
What that magic number 6 means? Use variable or constant which makes sense in context.
its for the function, the number gets stored in the int nums above
ok new code, with that part fixed. now i need to output the non chosen friends. the chosen friend still becomes outputted in the list. what should i 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
40
41
42
43
44
45
46
47
#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];
    arr[num] == arr[nums - 1];
    --nums;
    return name;


}

int main()
{
    srand(time(NULL));
    string num_names[6];
    int num_of_names;
    int N = 6;

    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 >> num_names[counter];
    }


    cout << "You should text: " << get_random_name(num_names, N) << endl;

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

    for (int i = 0; i < num_of_names; ++i)
    {
        cout << num_names[i] << endl;
    }
    return 0;

}
Because you are passing N into function, but iterating over num_of_names.
get_random_names modifies parameter to reflect new size. If you are still using old one, you are your own enemy.
so how should i make it not do that. im kind of new to c++ so i dont know all the things yet.
Just pass num_of_names instead of N.
ok good, what i get now is only the last name subtracted from the input. how can i make it take out the chosen name from the outputted list of names?

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
40
41
42
43
44
45
46
47
48

#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];
    arr[num] == arr[nums - 1];
    --nums;
    return name;


}

int main()
{
    srand(time(NULL));
    string num_names[6];
    int num_of_names;
    int N = 6;

    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 >> num_names[counter];
    }


    cout << "You should text: " << get_random_name(num_names, num_of_names) << endl;

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

    for (int i = 0; i < num_of_names; ++i)
    {
        cout << num_names[i] << endl;
    }
    return 0;

}
Last edited on
It is replacing chosen name with last one and removing last one example:
1
2
3
4
5
Array:
Sam
Max
Sarah
Rose
You are searching for: 
Max
Array after swap and removal:
1
2
3
4
Sam
Rose
Sarah
Rose
yeah thats the problem. what should i do in this case?
oh but it leaves the chose name in and takes away 1 from the original list of names.
Whoops. My bad. replace == with = in get_random_name. Actually your IDE should issue a warning about that.

http://ideone.com/aFnpNZ
Last edited on
yay it worked haha TY<3. . .but now when the name is taken out of the list, and the new list is shown, an empty line is left where the chosen name was.

the firs // was needed to show the list without the name. and the second // was an idea i had.

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
40
41
42
43
44
45
46
47
48
#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];
    arr[num] = arr[nums - 0];
    //--nums;
    return name;


}

int main()
{
    srand(time(NULL));
    string num_names[6];
    int num_of_names;
    int N = 6;

    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 >> num_names[counter];
    }

    cout << "You should text: " << get_random_name(num_names, num_of_names) << endl;

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

    //string chosen = get_random_name(num_names, num_of_names);

    for (int i = 0; i < num_of_names; ++i)
    {
        cout << num_names[i] << endl;
    }
    return 0;

}
Last edited on
arr[nums - 0]; WHY? It was working perfectly before.
arr[nums - 0] is the same as arr[nums]; which is outside of array bounds. At least outside of initialized values range.
oh wait NVM!, it didnt work for me the first time because i forogt that i had
--nums; as a comment haha. but now it works YAY!! TY SO MUCH
Pages: 12