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;
}