im not sure whats happening here, but i dont know what code to use to stop the prompts from looping. My exercise is to create a program revolving Array-Based Lists. I simply want the user to input a set of student's records, but the user can input how many he/she decides. so user can have a choice of either 4 students, 12 students, or even 20 students, etc.

but it just keeps looping. im searching my book for answers, and there are so much information everywhere. i was wondering if someone can point me to the right direction and hint me some ideas. i'm not supposed to use sentinel method to end the loop, which is what i wanted to do. but a spec in our exercise is to not use it. apparently, i am just to "add values to a list."

am i even heading to the right direction here?


PROBLEM:
- prompts keep looping
- because of loop, i cannot get the list of outputs to come about
- i feel like my code for gpa-sort (hi to lo) is wrong, maybe?

my professor introduced array-based lists 2 days ago and expects us to be experts, it sucks.

anyway, a copy of my current program is below. can anyone see what my issues are so far?

-thanks-

#include <fstream>
#include <iomanip>
#include <string>
#include <iostream>
using namespace std;

struct Student
{
  string name;
  int id;
  float gpa;
};

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


int main()
{
  string name;
  int id;
  float gpa;
 
  // create an empty list
  const int MAX_STUDENTS = 100;   
  int nStudents = 0;               
  Student student[MAX_STUDENTS];
  

  // read and save the records
  while (true)
  {
    // create a record and read it from file
    Student aStudent;
    cout << "Student's name: ";
    getline(cin, aStudent.name);
    
    int aID;
    cout << "Student's ID: ";
    cin >> aStudent.id;
    cin.ignore(1000, 10);
    
    float gpa;
    cout << "Student's GPA: ";
    cin >> aStudent.gpa;
    cin.ignore(1000, 10);
    
    cout << " " << endl;
           
    
    // add record to list, if it's not full
    if (nStudents < MAX_STUDENTS)
      student[nStudents++] = aStudent;
  }

  // sort the students 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);
  
  cin.ignore();
  cin.get();
  return 0;
}

If you don't want to end the loop on a sentinel, what about asking the user for a student count before you get the student record data.
Remember to check that it's less than the size of your array.
Then change your loop so that it's based on your count.

i finally got the thing to (sorta) work. i implemented how many students records the user would like to view, and it breaks off however many the user decides to view.

my problem now is that i cannot (for the life of me) get the proper inputs to print out on the console.

it seems as though it DOES put the GPAs in order, but i cant get any NAME or right ID or the correct GPA to come out.

its progress from my previous code, but now im just confused as to why its not printing the proper inputs.

i have no idea what is wrong. =(

#include <fstream>
#include <iomanip>
#include <string>
#include <iostream>
using namespace std;

struct Student
{
  string name;
  int id;
  float gpa;

};


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


int main()
{
 
  // create an empty list
  const int MAX_STUDENTS = 100;   
  int nStudents = 0;               
  Student student[MAX_STUDENTS];
  


  // prompt for how many students
  cout << "How many records would you like to view? ";
  cin >> nStudents;
  cin.ignore(1000, 10);
  
  // read and save the records
  for (int i = 0; i < nStudents; i++)
  {
    if (nStudents < MAX_STUDENTS)
    {
      // create a record and read it from file
      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);
        
        
     cout << " " << endl;
    }
  }

  // sort the students 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);
  
  cin.ignore();
  cin.get();
  return 0;
}

You missed element is to be assigned in Post #3.

// read and save the records
  for (int i = 0; i < nStudents; i++)
  {
    if (nStudents < MAX_STUDENTS)
    {
      .....     
      student[nStudents++] = aStudent;      // assign an element at given index  
     cout << " " << endl;
    }
  }

Sort algorithm requires an outer loop to be executed n-1.

for (int i = 0; i < nStudents-1; i++)
  {
    for (int j = i + 1; j < nStudents; j++)
    {
      ....
    }
  }

You missed element is to be assigned in Post #3.

// read and save the records
  for (int i = 0; i < nStudents; i++)
  {
    if (nStudents < MAX_STUDENTS)
    {
      .....     
      student[nStudents++] = aStudent;      // assign an element at given index  
     cout << " " << endl;
    }
  }

I would assign aStudent to array element i, i.e. make that:

// read and save the records
  for (int i = 0; i < nStudents; i++)
  {
    if (nStudents < MAX_STUDENTS)
    {
      .....     
      student[i] = aStudent;      // assign an element at given index  
     cout << " " << endl;
    }
  }

Also, good to see that you have checked for the number of students exceeding the array size, but it would be better for the user if you gave immediate feedback. So as soon as you get the count, check it, and if it isn't acceptable ask the user again. That's going to be another loop - all good practice. Drop MAX_STUDENTS to 1 so you can test it.

This question has already been answered. Start a new discussion instead.