Grade book program using vectors

I am writing a gradbook type program. It first allows the user to enter the number of students they want to enter. then allows them to enter the first name, last name, and grade of each student. The program then should sort the names alphabetically by last name and display a list of all the students names and grades in alphabetical order. I have completed all my functions and classes, but i am getting a god aweful amount of errors and i am not sure what is wrong. It is my first attempt at using classes and objects and bubble sort so i am not sure. here is my code let me know any problems you see, i would greatly appreciate some guidance. First is the header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>  // allows the program to output data to the screen

// students class definition
class students
{
public:
	int arraySize; // size of array entered by user for number of students
	
	int getNumstudents(int arraySize); // function to get the number of students user wants to enter
	void getData(int arraySize); // function to get first and last name and the students grade
	void displayMessage(); // displays welcome message
	void sortData (); // function to alphabetize by last name using buble sort
	void printData (); // function to print data
private:
	char*firstName[arraySize]; // array of students first names
	char*lastName[arraySize]; // array of students last names
	int grades[arraySize]; // array of students grades
};


and here is the cpp file
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
90
91
92
93
94
95
96
97
 #include <iostream>  // allows the program to output data to the screen
#include <conio.h>
#include  <iomanip>
#include <cstdlib>
#include <string.h>
#include "students.h" // gradebook class defintion


using std::cout; // program uses cout
using std::cin; // program uses cin
using std::endl; // program uses endl
using std::setprecision; // set numeric output precision
using std::fixed; // ensures that decimal point is displayed
using std::setw;


void  students::displayMessage()
{
		
	cout << "Welcome to the Student Scores Application." << endl;
}
		
// sorts data alphabetically by last name using bubble sort	
void students::sortData()
{

	for(int i = 0; i < arraySize ; i++){
    		for(int j = i+1; i < arraySize; j++){
      			if(strcmp(lastName[i]){
        			char *temp = lastName[i];    // swap pointers
        			lastName[i] = lastName[j];
        			lastName[j] = temp;
        		}
     		 }
    	}
 }

// Prints all the students data
void students::printData()
{
	
	for (int i = 0; i < arraySize; i++)
	{

	cout << "Student " << num << " last name: " << lastName[i] <<", " << firstName[i] << ": " << grades[i] << endl;

	}
}

// gets the number of students the user wnats to enter and sets that as the arraysize
int students::getNumstudents(int arraySize)
{
	cout << "Enter number of students to enter: ";
	cin >> arraySize;
}

// gets students info from user
void students::getData(int arraysize)
{
	int num = 1;
	char*userinput1;
	char*userinput2;
	int userinput3;
	
	while ( num <= arraySize){

	cout << "Student " << num << " last name: ";

	lastName[arraysize] = userinput1;

	cout << "Student " << num << " first name: ";

	firstName[arraysize] = userinput2;

	cout << "Student " << num << " score: ";

	grades[arraysize] = userinput3;
	
	num++
	}
}

// function main begins program exectuion
int main() 
{

	
	students.displayMessage();
	
	students.getNumstudents();

	students.getData();	

	students.sortData();
	
	students.printData();
}
Last edited on
closed account (z05DSL3A)
The errors will be because of
1
2
3
4
5
6
...
    int arraySize; // size of array entered by user for number of students
...
    char*firstName[arraySize]; // array of students first names
    char*lastName[arraySize]; // array of students last names
    int grades[arraySize];


You can't do this.
thanks for the help man, i got rid of all the errors and the program "runs' now. When it runs it just prints out getData() cout stuff 100 times, and then prints out Student Last name: folloed by a bunch of jibberish. I have commened out the bubble sort, becaussei can't figure out how to make it sort the last names. Any help with with getting the program working better would be apprecriated. here is what i have now,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
#include <iostream>  // allows the program to output data to the screen
#include <string.h>

// students class definition
class students
{
public:
	int getNumstudents(); // function to get the number of students user wants to enter
	void getData(); // function to get first and last name and the students grade
	void displayMessage(); // displays welcome message
	//void sortData (); // function to alphabetize by last name using buble sort
	void printData (); // function to print data
	const static int numStudents = 100;
private:
	char firstName[100]; // array of students first names
	char lastName[100]; // array of students last names
	int grades[100]; // array of students grades
};


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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
 /* 
Josh Mauney
CSC215
4-28-08
student grade program
project 2
*/

#include <iostream>  // allows the program to output data to the screen
#include <conio.h>
#include  <iomanip>
#include <cstdlib>
#include <string.h>
#include "students.h" // gradebook class defintion


using std::cout; // program uses cout
using std::cin; // program uses cin
using std::endl; // program uses endl
using std::setprecision; // set numeric output precision
using std::fixed; // ensures that decimal point is displayed
using std::setw;
using std::string;


void  students::displayMessage()
{
		
	cout << "Welcome to the Student Scores Application." << endl;
}
		
// sorts data alphabetically by last name using bubble sort	
/*void students::sortData(int numStudents)
{
	char temp[100];

	for(int i = 0; i < numStudents ; i++){
    		for(int j = i+1; i < numStudents; j++){
      			if(strcmp(lastName[i], lastName[j] > 0)){
        			temp = lastName[i];    
        			lastName[i] = lastName[j];
        			lastName[j] = temp;
        		}
     		 }
    	}
 }
*/
// Prints all the students data
void students::printData()
{
	
	
	for (int i = 0; i < numStudents; i++)
	{

	cout << "Student last name: " << lastName[i] <<", " << firstName[i] << ": " << grades[i] << endl;

	}
}

// gets the number of students the user wnats to enter and sets that as the arraysize
int students::getNumstudents()
{
	int arraySize;
	cout << "Enter number of students to enter: ";
	return arraySize;
}

// gets students info from user
void students::getData()
{
	int num = 1;
	char userinput1;
	char userinput2;
	int userinput3;
	
	while ( num <= numStudents){

	cout << "Student " << num << " last name: ";

	lastName[numStudents] = userinput1;

	cout << "Student " << num << " first name: ";

	firstName[numStudents] = userinput2;

	cout << "Student " << num << " score: ";

	grades[numStudents] = userinput3;
	
	num++;
	}
}

// function main begins program exectuion
int main() 
{
	students mystudent;
	
	mystudent.displayMessage();
	
	mystudent.getNumstudents();

	mystudent.getData();	

	//mystudent.sortData();
	
	mystudent.printData();
	
	char pause = getchar();  // program pauses before ending

	getchar();
} 
Ok i completly redid my program after i realized i was overcomplicating it. here is what i got. I can get the program to read the names, but it stops after the last name is enered and doesn't print the names. So i can't tell if the sorting is working. any ideas? thanks for the help so far!!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
#include <iostream>  // allows the program to output data to the screen
#include <string>

// students class definition
class students
{
public:
	void getData(); // function to get first and last name and the students grade
	void displayMessage(); // displays welcome message

private:

};


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
#include <iostream>  // allows the program to output data to the screen
#include <conio.h>
#include  <iomanip>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include "students.h" // gradebook class defintion


using std::cout; // program uses cout
using std::cin; // program uses cin
using std::endl; // program uses endl
using std::setprecision; // set numeric output precision
using std::fixed; // ensures that decimal point is displayed
using std::setw;
using std::string;
using std::vector;


void  students::displayMessage()
{
		
	cout << "Welcome to the Student Scores Application." << endl << endl;
}
		

void students::getData()
{
	int numStudents;
	vector<string> student_names(numStudents);
	
	cout <<"Enter number of students to enter: ";
	cin >> numStudents;
	
	for (int i = 0; i <= numStudents; i++)
	{ 
            cout << "Enter student name (Enter to finish): "; 
            getline (cin, student_names[i]);
	}

            // sort them alphabetically
        sort (student_names.begin(), student_names.end());

	for (int i =0; i < numStudents; i++)
	{
			cout << student_names[i];
	}
	  
	
	
}

// function main begins program exectuion
int main() 
{
	students mystudent;
	
	mystudent.displayMessage();

	mystudent.getData();	
	
}
Last edited on
This is how I would use a vector approach. Alot simpler than your implementation and will have alot less code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct StudentRecord {
 string FirstName;
 string LastName;
 int Grade;
}

int main() {

 vector<StudentRecord> myList;

 StudentRecord myRecord;
 myRecord.FirstName = "Zaita";
 myRecord.LastName = "Zaita";
 myRecord.Grade = 100;

 myList.push_back(myRecord);

}


No memory allocation errors to worry about. Then you can implement a sort callback method on the vector to sort them.

Otherwise, I'd still be using the Struct Approach to store student information.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

int main() {

 cout << "Enter No. Students: "
 cin >> number;

 StudentRecord *pRecords = new StudentRecord[number];

 for (int i = 0; i < number; ++i) {
  do whatever;
 }

 delete [] pRecords;
}


also. Instead of going
using std:endl etc for every command. Just use

 
using namespace std;


That will encompass them all.
Last edited on
I am supposed to have a class named students, so don't think i can use the struct aprroach. So is there a way to acomplish printing and sorting the list using what i have? I thought the for loop would work to print, like in a array, but i dont know why that isnt working. thanks
Class and struct are interchangeable. I used a struct to show simplicity.

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
class StudentRecord {
public:
 void setFirstName(string value) { FirstName = value; }
 string getFirstName() { return FirstName; }
 // etc

 bool operator < (const StudentRecord& left, const StudentRecord& right) {
   // Add Sort Code, Return True if Left is LESS THAN right, otherwise False
 }

protected:
 string FirstName;
 string LastName;
 int Grade;
}

int main() {

 vector<StudentRecord> vList;

 StudentRecord rec = StudentRecord();
 rec.setFirstName("Zaita");
 // etc

 vList.push_back(StudentRecord);
 vList.push_back(StudentRecord2);
 vList.push_back(StudentRecord3);
 vList.sort();

}
Topic archived. No new replies allowed.