Struggling with program using functions, vectors and sorting.

I've been assigned the following.

Task #1:
Define a struct type called Student. A Student has the following members:
• last name (a string)
• first name (a string)
• grade in course (float)
• letter grade (a char)

Task #2:
Your program should begin by asking the user how many students are in the class. Once the answer is received, the program should dynamically allocate space on the heap for an array of that many student objects, and then fill them up with data from the user (you should prompt the user, and read in the data for each student).

Once the data has been read in, your program should present a menu that does the following:
1) Add a student to the end of the list
2) Allow the user to display all students as they currently appear in the list
3) Allow the user to sort the students in ascending alphabetical order based on the Last Name, First Name combination
4) Allow the user to sort the students in descending order on Last Name, then first name
5) Allow the user to sort the students in ascending order based on gpa, then last name
6) Allow the user to sort the students in descending order on gpa, then last name
7) Allow the user to sort the students in ascending order on letter grade, then last name
8) Quit

Your program should display all the students neatly, with appropriate spacing when Option 2 is selected.

Your program should sort the lists only when Options 3-7 are chosen; NO OUTPUT should be given from these operations.

Any advice on how to go about doing this? We're having problems using structs stored in vectors and creating sorts for the functions.
closed account (z05DSL3A)
gemineye904,

You will probably need to show code and ask specific questions to get others to help.

assignment wrote:
the program should dynamically allocate space on the heap for an array of that many student objects
you wrote:
We're having problems using structs stored in vectors
Are you going in the right direction with this?
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cstring>

using namespace std;


struct Student
{
string lname;
string fname;
float numgrade;
char letgrade;

};

int input;

void MenuDisplay(Student [],int);
void AddStudent(Student [],int);
void StudentDisplay(Student [],int);

int main()
{

Student LIST[input];

cout << "How many students are in the class?" << endl;
cin >> input;



for (int i=0; i < input; i++)
{
cout << "Students Last Name: " << endl;
cin >> LIST[i].lname;
cout << "Students First Name: " << endl;
cin >> LIST[i].fname;
cout << "Numerical Value of the Grade: " << endl;
cin >> LIST[i].numgrade;
cout << "Letter Grade: " << endl;
cin >> LIST[i].letgrade;
}

MenuDisplay(LIST,input);

return 0;
}


void MenuDisplay(Student LIST[], int input)
{


int selection;


cout << "Please select an option from the menu presented." << endl;
cout << "1) Add a student to the end of the list." << endl;
cout << "2) Display all students in current list." << endl;
cout << "3) Sort student list by last name in ascending alphabetical order." << endl;
cout << "4) Sort student list by last name in descending alphabetical order." << endl;
cout << "5) Sort student list by GPA in ascending order." << endl;
cout << "6) Sort student list by GPA in descending order." << endl;
cout << "7) Sort student list by letter grade in ascendin order." << endl;
cout << "8) Exit program." << endl;

cin >> selection;

if (selection == 1)
{
AddStudent(LIST,input);
}

if (selection == 2)
{
StudentDisplay(LIST,input);
}

if (selection == 3)
{

}

if (selection == 4)
{

}

if (selection == 5)
{

}

if (selection == 6)
{

}

if (selection == 7)
{

}

if (selection == 8)
{

}

else
{
cout << "Invalid option. Please re-enter a valid choice.";
}


}

void AddStudent(Student LIST[], int input)
{
char input2;

cout << "Would you like to add another student? (Y/N)" << endl;
cin >> input2;

while (input2 != 'Y' && input2 != 'N')
{
cout << "Invalid Choice Please Use 'Y' or 'N' " << endl;
cin >> input2;

}


if (input2 == 'Y')
{
input += 1;

cout << "Students Last Name: " << endl;
cin >> LIST[input].lname;

cout << "Students First Name: " << endl;
cin >> LIST[input].fname;

cout << "Numerical Value of the Grade: " << endl;
cin >> LIST[input].numgrade;

cout << "Letter Grade: " << endl;
cin >> LIST[input].letgrade;

MenuDisplay(LIST,input);
}

if (input2 == 'N')
{
MenuDisplay(LIST,input);
}
}


void StudentDisplay(Student LIST[], int input)
{

for (int i=0; i < input; i++)
{
cout << LIST[i].lname;
cout << setw(10) << LIST[i].fname;
cout << setw(10) << setprecision(4)<< LIST[i].numgrade;
cout << setw(10) << LIST[i].letgrade;
cout << endl;
}

MenuDisplay(LIST,input);
}

This is the code using arrays that does not work.
1
2
3
4
Student LIST[input];

cout << "How many students are in the class?" << endl;
cin >> input;


This code is invalid. You cannot create an array with an un-initialized variable. You're also not allocating the space on the heap as per the assignment requirements.

1
2
3
4
cout << "How many students are in the class?" << endl;
cin >> input;

Student *LIST = new Student[input];
Topic archived. No new replies allowed.