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
};
#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();
}
...
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];
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
conststaticint 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
};
/*
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:
};
#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();
}
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