I'm making a student database using array based lists. I'm having trouble with making the list size created by the user. I have a for loop there now but once I put it there, I get an error saying aStudent is an undeclared variable and I don't understand why. There wasn't an error before the for loop.

int* stud = new int[size];

  // creating a record
  int b;
  for (int b = 0; b < size; b++)
  {
  Student aStudent;
  cout << "Student's name? ";
  getline(cin, aStudent.name);
  cout << "Student's ID? ";
  cin >> aStudent.id;
  cin.ignore(1000, 10);
  cout << "Student's GPA? ";
  cin >> aStudent.gpa;
  cin.ignore(1000, 10);
  }

  // skipping separator
  cin.ignore(1000, 10);

  // adding record to list
  if (nStudents < MAX_STUDENTS)
	  student[nStudents++] = aStudent;

  // sorting by gpa
  for (int i = 0; i < nStudents; i++)
  {
	  for (int j = i + 1; j < nStudents; j++)
	  {
		  if (student[i].gpa > student[j].gpa)
		  {
			  Student temp = student [i];
			  student [i] = student[j];
			  student [j] = temp;
		  }
	  }
  }

  printStudents(student, nStudents);

  cout << endl;

  return 0;
}

Recommended Answers

All 4 Replies

You have a scoping problem. The code right now create Student object dynamically. I'm assuming you are keeping track of each one by their id or something else, because every Student object you made in the for loop is aStudent. Because you are initializing the object inside the for loop, the object aStudent is lost once you are out of the loop. To solve your problem you need throw your adding record inside of the loop where you still have a handle on the object. Max_Students should be the same as size? (you only have size Student objects)

I don't understand the last part of what you said for the MAX_STUDENTS. Currently when I want it to input 6 students, it lets me input 6 students. And it prints all 6 by their gpas.

Expanding my for loop to include the adding a record has solved the aStudent problem.

The problem I'm having now is I the output to be on all one line. I thought I made it that way in my void function but it prints the name on one line, goes to the next and prints the id and gpa.

This is the void function

void printStudents(Student* student, int nStudents)
 {
	 int i;
	 for (int i = 0; i < nStudents; i++)
	 {
		 cout << "Name: " << left << setw(30) << student[i].name << endl;
		 cout.fill('0');
		 cout << " ID = " << right << setw(7)
			 << student[i].id << ", gpa = "
			 << student[i].gpa << endl;
		 cout.fill(' ');
	 }
 }

And the output looks like this:
Name: Brittany
ID: 54754623, GPA: 3.42

But I want it like this:
Name: Brittany ID: 4356346, GPA: 3.23

remove your <<endl at the end of cout, more specifically the endl inline 6. endl=="\n"

also all your strings have null terminator so cout.fill(' ') dosen't do much

Thank you, I didn't notice that. And thanks for the other help. My program is running just how I needed it to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.