Random pick without repeats

What I'm trying to do is to have the program pick random people for different jobs. The user enters the number of people, then, enters both jobs and the people's names. I need the program to pick random people, but randomly pick again if the person has already been picked, so, no repeats. I have the program picking random people, but I can't get it to not pick the person again. I'll run it and there will be no errors, but it's almost like Xcode completely bi-passes the for/if to check if it's picked the person already.
Below is the code.
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
	int runagain=0, correctnames=0, versiondesciosion=0, correctjobs=0;//vars that make descisions for running again
rerun:
	cout<<"This program will randomly chose jobs for people."<<endl;
goback:
	int number=0;
	cout<<endl;
	cout<<"Please enter the number of people: ";
	cin>>number;
	number=number+1;
	string people[number];
	//int reflist=number;
	cout<<"Please enter their names: "<<endl;
	for(int x=0; x<=number; x++)
	{
		getline(cin,people[x]);
		if((x+2)>number)
			break;
	}
	for(int x=0; x<=number; x++)
	{
		cout<<people[x]<<endl;
		if((x+2)>number)
			break;
	}
	cout<<endl;
renames:
	cout<<"Are the names correct?(Y=1||N=2): ";
	cin>>correctnames;
	if(correctnames==2)
		goto rerun;
	else if(correctnames!=1 && correctnames!=2)
	{
		cout<<"Please enter 1 or 2!"<<endl;
		goto renames;
	}
reversion:
	cout<<endl;
	cout<<"Do you want the \"Fun\" version of chores or do you want to enter your own chores? Note: Fun version does not yet work."<<endl;
	cout<<"(Fun=1||Own=2||Re-enter names=0): ";
	cin>>versiondesciosion;
	if(versiondesciosion!=0 && versiondesciosion!=1 && versiondesciosion!=2)
	{
		cout<<"Please enter 1, 2, or 0!"<<endl;
		goto reversion;
	}
	int numjobs=0, num1=0, xx=0, xxx=0, y=0;
	double a=1, b=number;
	srand((unsigned)time(NULL));
	num1=a+int(rand())*((b-a)/RAND_MAX); //Creates Random Number
	num1=a+int(rand())*((b-a)/RAND_MAX);
	switch(versiondesciosion)
	{
		case 0:
		{
			goto goback;
		}
		case 1:
		{
			
		}
		case 2:
		{
			cout<<endl;
			numjobs=number;
			string jobss[numjobs];
		recorrectjobs:
			cout<<"Please enter the jobs: "<<endl;
			for(int x=0; x<=numjobs; x++)
			{
				getline(cin,jobss[x]);
				if((x+2)>numjobs)
					break;
			}
			for(int x=0; x<=numjobs; x++)
			{
				cout<<jobss[x]<<endl;
				if((x+2)>numjobs)
					break;
			}
			cout<<endl;
			cout<<"Are the jobs you've entered correct?(Y=1||N=2): ";
			cin>>correctjobs;
			if(correctjobs==2)
			{
				goto recorrectjobs;
			}
			else if(correctjobs!=1 && correctjobs!=2)
			{
				cout<<endl;
				cout<<"Please enter 1 or 2!";
				goto recorrectjobs;
			}
			int numrand[number];                //<-----Here is the random picker
			for(int x=0; x<=(number+1); x++)
			{
				y=y+1;
			rerand:
				num1=a+int(rand())*((b-a)/RAND_MAX); //Creates Random Number
				num1=a+int(rand())*((b-a)/RAND_MAX);
				numrand[x]=num1;
				xx=x+1;
				if(x!=0)
				{
					for(int h=0; numrand[h]==numrand[xxx]; h++)
					{
						if(h==y)
							goto numjobsifbreak;
						if(h!=0)
						{
							xxx=xxx+1;
							cout<<"Not Bi-passing if"<<endl;
							goto rerand;
						}
					}
				}
			numjobsifbreak:
				if((x+2)>numjobs)
				   break;
				cout<<jobss[xx]<<": "<<people[num1]<<endl;
			}
				
		}
	}
	
	
	
	
	
	
	
	cout<<endl;
rerunagain:
	cout<<"Run again?(Y=1||N=2): ";
	cin>>runagain;
	if(runagain!=1 && runagain!=2)
	{
		cout<<"Please enter 1 or 2!"<<endl;
		goto rerunagain;
	}
	if(runagain==2)
	{
		cout<<"Have a nice day!";
	}
	if(runagain==1)
	{
		goto rerun;
	}
	
	return 0;
}


Dont look at the "fun" version for the switch statement. It's not ready yet, I'm just having trouble with the random thing.
Line 15,70,98: It's not legal to allocate an array this way using a variable in C++.

Line 18,24,73,79,99: Your for loops are defective. You're going to be indexing out of bounds. If number=10, array elements are 0-9. <= is going to index an non-existant element [10].

Line 54-55, 103-104: Why are these lines duplicated?

Get rid of the goto statements. GOTOs are EVIL.



Last edited on
Not going to try parse that spagetti code, but I would suggest you, instead of picking random people, just shuffle your array. After that people in the array would be in the random order and you can just pick one by one.
Standard library has random_shuffle() function:
http://www.cplusplus.com/reference/algorithm/random_shuffle/
Last edited on
Keep unpicked people at the front of the array, and picked people at the end.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
string people[ 10 ];
unsigned num_people = 0;
unsigned num_picked = 0;

...

people[ 0 ] = "Susan";  num_people++;
people[ 1 ] = "James";  num_people++;
people[ 2 ] = "Annie";  num_people++;
people[ 3 ] = "Leigh";  num_people++;

...

if (num_picked < num_people)
{
  // Let's pick James (chose 1 out of 0..3)

  std::swap( people[ 1 ], people[ num_people - 1 - num_picked ] );
  num_picked++;
}

Hope this helps.
Topic archived. No new replies allowed.