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

#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

#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();
}

Recommended Answers

All 9 Replies

Look at the last function you wrote before compiling for the last time. Assuming there were no errors before writing that function then the errors should be somewhere therein. If you didn't compile until after you wrote all of that, then shame on you. You can work it out yourself by going back and commenting everything out down to the bare bones and do it over again, compiling with each new function added, at minimum, and fixing errors as you find them.


As a weak second alternative you might be able to make some progress by posting the first couple error messages you are getting and indicate where in the code the compiler is pointing to when it spots the error. But such post hoc debugging is really not cool.

Sorry i didn't post errors, there was about 100 of them and i was very frustrated, once again i apolgize and thanks for helping me anyways. 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,

#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
};
/* 
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();
}

I don't see any istream functions being used in getData() which according to the comments is supposed to get student info from user. It's a little much to ask your computer make the data up itself, though I swear sometimes it does just that. Better to empower the user to do what you want them to by using a istream function or two.

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!!!!

#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();	
	
}

In line 2 of the following code snippet you try using numStudents before it has been given a value. Your options as I see them are to move love 2 to below line 4 and keep the same code otherwise or to declare student_names with the implementations default vector capacity and use push_back() to fill the vector instead of assigning values to the individualt elements. push_back() will automatically extend the vectors capacity as needed. Assigning to individual elements will not.

int numStudents;
vector<string> student_names(numStudents);

cout <<"Enter number of students to enter: ";
cin >> numStudents;

Bottom line, since numStudents doesn't have a valid value before being used to declare the capacity of the vector the vector probably has capacity of zero. This means when you try to input information into the vector by asssignment it doesn't work because there is no memory to assign values to and when you try to display what's in the vector it doesn't have anything to display.

hey man thanks that worked wonders. I have edited the code and it does run smoother and doesnt crash. The problem i run into now is that when i run it last name and first name are displayed at the same time for the user to enter. Like this last name: first name:. How get them to be seperate so the user can enter them individually. Also it not sorting them but i think it has to do with the program not getting the first and last name seperatly.

#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 << endl << "Welcome to the Student Scores Application." << endl << endl;
}
        

void students::getData()
{
    int numStudents = 0;
    string name;	
    int score = 0;
    int i = 0;
    
    cout <<"Enter number of students to enter: ";
    cin >> numStudents;
	
    vector<string> student_lastnames(numStudents);
    vector<string> student_firstnames(numStudents);
    vector <int> student_score(numStudents);

    do
   
    { 
            cout << "Student " << i + 1 <<" last name: "; 
            getline (cin, name);
      	    student_lastnames.push_back(name);
    
            cout << "Student " << i + 1 <<" first name: "; 
            getline (cin, name);
     	    student_firstnames.push_back(name);
    
            cout << "Student " << i + 1 <<" score: "; 
            cin >> score;
	    student_score.push_back(score);
     		
     i++;
     }

     while ( i < numStudents);
    

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

    for (int l =0; l < student_lastnames.size(); l++)
    {
            cout << student_lastnames[l] << " " << student_firstnames[l] << " " << student_score[l];
    }
      
    
    
}

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

    mystudent.getData();    
    
}

hey man thanks that worked wonders. I have edited the code and it does run smoother and doesnt crash. The problem i run into now is that when i run it last name and first name are displayed at the same time for the user to enter. Like this last name: first name:. How get them to be seperate so the user can enter them individually. Also it not sorting them but i think it has to do with the program not getting the first and last name seperatly.

#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 << endl << "Welcome to the Student Scores Application." << endl << endl;
}
        

void students::getData()
{
    int numStudents = 0;
    string name;	
    int score = 0;
    int i = 0;
    
    cout <<"Enter number of students to enter: ";
    cin >> numStudents;
	
    vector<string> student_lastnames(numStudents);
    vector<string> student_firstnames(numStudents);
    vector <int> student_score(numStudents);

    do
   
    { 
            cout << "Student " << i + 1 <<" last name: "; 
            getline (cin, name);
      	    student_lastnames.push_back(name);
    
            cout << "Student " << i + 1 <<" first name: "; 
            getline (cin, name);
     	    student_firstnames.push_back(name);
    
            cout << "Student " << i + 1 <<" score: "; 
            cin >> score;
	    student_score.push_back(score);
     		
     i++;
     }

     while ( i < numStudents);
    

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

    for (int l =0; l < student_lastnames.size(); l++)
    {
            cout << student_lastnames[l] << " " << student_firstnames[l] << " " << student_score[l];
    }
      
    
    
}

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

    mystudent.getData();    
    
}

Could be that the input stream is not empty so you are getting an empty string for the first name. Check out this thread, particularly post 3
http://www.daniweb.com/forums/post540430.html#post540430

and this thread at the top of the C++ forum. Try clearing that input stream and see if the problem goes away.
http://www.daniweb.com/forums/thread90228.html

thanks for the info on using ignore. I have including it in program and fixed things that now the user can enter all the info indivudally. And it the sort works... the problem i run into now is that for some reason when it prints it always priints two zeros before the text. and while it sorts the last names it prints the last name that is sorted withe first name and score of the first entry. ex. user inputs
studnt 1 last name: oaks
student 1 first name: john
student 1 grade: 100

studnt 2 last name: hill
student 2 first name: mike
student 2 grade: 75

program prints out:

0 0 hill john 100 oaks mike 75

i realize that since i am just sorting the lastname vector, that it is storing the sorted last name with the user input of the first name. not sure how to fix it and get rid of the zeros. thanks for the help so far.

#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 <fstream>
#include <ios>
#include <istream>
#include <limits>
#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;
using std::max;


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

void students::getData()
{
    int numStudents = 0;
    string name;	
    int score = 0;
    int i = 0;
    
    cout <<"Enter number of students to enter: ";
    cin >> numStudents;
    cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
    
    vector<string> student_lastnames(numStudents);
    vector<string> student_firstnames(numStudents);
    vector <int> student_score(numStudents);

    do
   
    { 
            cout << "Student " << i + 1 <<" last name: "; 
            cin >> name;
      	    cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );    
	    student_lastnames.push_back(name);
    	    



            cout << "Student " << i + 1 <<" first name: "; 
            cin >> name;
     	    cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
	    student_firstnames.push_back(name);
      
            cout << "Student " << i + 1 <<" score: "; 
            cin >> score;
	    cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );    
	    student_score.push_back(score);
     		
     i++;
     }

     while ( i < numStudents);
    

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

    for (int l =0; l < student_lastnames.size(); l++)
    {
            cout << student_lastnames[l] << " " << student_firstnames[l] << " " << student_score[l];
    }
      
    
    
}

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

    mystudent.getData();    
    
}

You can't use the standard sort() function to accomplish your task, sorry. Since you have three separate vectors contaning linked data (sometimes this is called parallel arrays or parallel vectors), you have to write the function yourself. Writing a bubblesort(), or some other sorting protocol, which swaps parallel indexes in all three arrays/vectors each time the sentinel index needs to swap is the only to do it. If you were able to store student information in a single object and have a single array/vector of objects, then you could use std::sort().

for() //control index i here
  for() //control index j here
    if (appropriate conditions if elements with index i and j)
      swap i and j elements of last name vector
      swap i and j elements of first name vector
      swap i and j elements of score vector
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.