Apr 27, 2014 at 5:47am UTC
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 Apr 28, 2014 at 5:59am UTC
Apr 27, 2014 at 8:31am UTC
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 Apr 27, 2014 at 8:31am UTC
Apr 28, 2014 at 5:12am UTC
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 Apr 28, 2014 at 5:13am UTC
Apr 28, 2014 at 5:16am UTC
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 Apr 28, 2014 at 5:25am UTC
Apr 28, 2014 at 5:49am UTC
Find the difference:
Your code: string get_random_name(string arr[], int nums)
My code: string get_random_name(string arr[], int & nums)
Apr 28, 2014 at 5:53am UTC
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 Apr 28, 2014 at 6:00am UTC
Apr 28, 2014 at 6:01am UTC
What that magic number 6 means? Use variable or constant which makes sense in context.
Apr 28, 2014 at 6:03am UTC
its for the function, the number gets stored in the int nums above
Apr 28, 2014 at 6:19am UTC
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.
Apr 28, 2014 at 6:28am UTC
so how should i make it not do that. im kind of new to c++ so i dont know all the things yet.
Apr 28, 2014 at 6:32am UTC
Just pass num_of_names instead of N.
Apr 28, 2014 at 6:37am UTC
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 Apr 28, 2014 at 6:38am UTC
Apr 28, 2014 at 6:42am UTC
yeah thats the problem. what should i do in this case?
Apr 28, 2014 at 6:47am UTC
oh but it leaves the chose name in and takes away 1 from the original list of names.
Apr 28, 2014 at 6:50am UTC
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 Apr 28, 2014 at 6:52am UTC
Apr 28, 2014 at 6:54am UTC
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 Apr 28, 2014 at 6:55am UTC
Apr 28, 2014 at 6:57am UTC
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.
Apr 28, 2014 at 7:03am UTC
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