Array-Based LISTS

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-

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
#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;
}
Well how is the user supposed to communicate how many students they want to input? Are they supposed to specify at the beginning of the program? Are they supposed to enter something during the data entry that indicates they are finished?

Your sorting routine should be if (student[i].gpa < student[j].gpa) since you are going for highest to lowest.
Is your teacher actually calling an array a "list"?
I hate it when people -- particularly teachers -- make such reckless use of jargon.

http://en.wikipedia.org/wiki/List_(computer_science)
no, my teacher is describing array-based lists as an something "that takes the idea of an array, but considers the elements may not all be in use."

and tevsky, thanks for the sort pointer. i'm unsure how he wants me to lay out the cutoff of how many students there are.

apparently, the input is supposed to only control the loop, not to be of use it to set the list capacity -- it should get set before prompting the user for anything.

im not sure how to even approach that idea.
Last edited on
You never break the while loop.
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. =(

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
#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 aren't ever actually assigning any students to your array, you are simply making a temporary student, putting data into it, and then not doing anything with it.
Topic archived. No new replies allowed.