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.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
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.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
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 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.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
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
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
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
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396