Program Crashing

Heres my code. You enter 10 student names, and when you enter done it randomly pairs them and prints our a chart.

But after the 10th student and I enter done, My program crashes. Help?

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
#include <iostream>
#include "gen.h"
#include "student.h"
#include <string>
#include <stdlib.h>

using namespace std;

string done = "done";
string Done = "Done";

void Gen::Generate()
{

	bool entering = true;

	cout << "Generate Lab Partners" << endl;
	cout << endl;
	cout << "#################################" << endl;
	cout << endl;
	cout << "Enter \"done\" when you are finished entering students." << endl;
	cout << endl;

	Student s [10];
	int currentStudent = 0;

	while(entering)
	{

			cout << "Enter student name: ";
			cin >> s[currentStudent].name;

			if(s[currentStudent].name == done || s[currentStudent].name == Done)
			{
				entering = false;
				cout << endl;
			}
			
			else
			{
				s[currentStudent].Init();
				currentStudent += 1;
			}
	
	}

	int print = 0;

	cout << "#################################" << endl;
	cout << endl;
	cout << "Student 1	Student 2	Lab Station" << endl;

	int station = 1;

	while(print < 5)
	{
		int RandIndex = rand() % 4;
		cout << "---------------------------------------------" << endl;
		cout << s[RandIndex].name << "		";
		RandIndex = rand() % 4;
		cout << s[RandIndex].name << "		" << station << endl;

		print += 1;
		station += 1;
	}

	cout << endl;

}
You do not check whether the user entered more than 10 names.
Last edited on
For starters I wouldn't recommend using global variables or the keyword using.

Also is there a reason you choose 4 with your random function? what happens when you enter only 3 students? how can it select index 4? also it will never select 5-9.
Also instead of +=1 you can use ++.

One more thing to mention what happens when you have an odd amount of students?

Here's how I would pair them.
1) Generate a list of possible positions
http://www.cplusplus.com/reference/algorithm/generate/
2) sort it randomly
http://www.cplusplus.com/reference/algorithm/random_shuffle/
3) grab the last two people then remove them
4) repeat..

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
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>

int gen( void )
{
    static int x = 0;
    return( x++ );
}
int main()
{
    std::srand( unsigned( time( NULL ) ) );
    const int people = 10;
    std::vector<int> vec( people ); //10 people
    std::generate( vec.begin() , vec.end() , gen );
    for( int i = 0; i < people/2; ++i )
    {
        std::random_shuffle( vec.begin() , vec.end() );
        std::cout << "Person " << vec.back() << std::flush;
        vec.pop_back();
        std::cout << " is paired with Person " << vec.back() << std::endl;
        vec.pop_back();
    }
    if( people % 2 != 0 ) std::cout << "Person " << vec.front() << " is alone." << std::endl;
    return( 0 );
Person 1 is paired with Person 2
Person 5 is paired with Person 9
Person 6 is paired with Person 8
Person 3 is paired with Person 7
Person 4 is paired with Person 0

Process returned 0 (0x0)   execution time : 0.270 s
Press any key to continue.


For example if the user entered 10 names then the word "done" or "Done" will be written in eleven element of the array while it has only ten elements.
Topic archived. No new replies allowed.