student line up program

I am writing a student line up program and for some reason I am getting an error at the following line...

1
2
3

string name[numStudents];


The number should be set to whatever I am asked in the first output. Here is the code to my entire program.

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
  /*
	A teacher has asked all her students to line up single file according to their first name.
	For example, in one class Amy will be at the front of the line and Yolanda will be at the end.
	Write a program that prompts the user to enter the number of students in the class, then loops to read in that many names.
	Once all the names have been read in it report which student would be at the front of the line and which one would be at the end of the line.
	You may assume that no two students have the same name.
	Input Validation:
		Do not accept a numbes less than 1 or greater than 25 for the number of students
*/
#include <iostream>
#include <string>
using namespace std;

int main()
{
	//Declare variables
	int numStudents;
	int count = 0;
	
	//Get input
	cout << "How many students are in the class? ";
	cin >> numStudents;

	while (numStudents < 1 || numStudents > 25)
	{
		cout << "\nThat is an invalid number of studentss\n";
		cout << "Enter a valid number of students: ";
		cin >> numStudents;
	}
	cout << endl;

	string name[numStudents];

	for (int i = 0; i < numStudents; i++)
	{
		cout << "Enter the name of student " << i + 1 << ": ";
		cin >> name[i];
	}

	string temp;

	for (int j = 1; j < numStudents; ++j)
	{
		temp = name[j];
		int k;

		for (k = j - 1; k >= 0 && name[k] > temp; k--)
		{
			name[k+1] = name[k];
		}
		name[k + 1] = temp;
	}

	cout <<"\nThe first student is: " << name[0] << endl;
	cout << "The last student is: " << name[numStudents - 1] << endl;
	return 0;
}
Last edited on
You can only declare an array of fixed size known at compile time. If you want it to be variable size, you should look up dynamic array allocation. However, the problem description says that the max students is 25, so in your case you can just make your array have a size of 25.
I had one additional question for anyone that can help. I am going through this c++ book so this is not for a class and want to understand this but can someone explain the following code to me...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

	string name[25]; //Array for up to 25 students

	for (int i = 0; i < numStudents; i++)
	{
		cout << "Enter the name of student " << i + 1 << ": ";
		cin >> name[i];
	}

	string temp;

	for (int j = 1; j < numStudents; ++j)
	{
		temp = name[j];
		int k;

		for (k = j - 1; k >= 0 && name[k] > temp; k--)
		{
			name[k+1] = name[k];
		}
		name[k + 1] = temp;
	}


I understand the first for loop as it is storing names for i but after that I am not sure. I know it is finding the alphabetical order but if someone can break this down for me where I can understand it better I would really appreciate it.
It is a bubble sort algorithm. You can look it up, since I'm not good at explaining things.

It's taking a student and then checking the student ahead of him/her in line to see if they really should be ahead of him/her. If not, then have them switch, and continue checking till the head of the line.

And you do that for every single student in line.
Topic archived. No new replies allowed.