Everytime i run my program after i enter my first value for the number of students, i get the windows box that pops up and says studens.exe has encountered a problem and needs to close. Is this a problem with my cose of a computer problem? thanks
#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 num = 1;
int num2 = 1;
int num3 = 1;
int numStudents = 0;
vector<string> student_lastnames(numStudents);
vector<string> student_firstnames(numStudents);
vector<int> student_score(numStudents);
cout <<"Enter number of students to enter: ";
cin >> numStudents;
for (int i = 0; i <= student_lastnames.size(); i++)
{
cout << "Student " << num <<" last name: ";
getline (cin, student_lastnames[i]);
num++;
}
for (int j = 0; j <= student_firstnames.size(); j++)
{
cout << "Student " << num2 <<" first name: ";
getline (cin, student_firstnames[j]);
num2++;
}
for (int k = 0; k <= student_score.size(); k++)
{
cout << "Student " << num3 <<" score: ";
cin >> student_score[k];
num3++;
}
// 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();
}
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:
};
The problem is the way you are using the vectors.
When you create then they have a size of zero.
In the For loops to input the data you are using i <= vector.size() as the comparitor - this should be i < numstudents
To add a new entry to the vector use vector.push_back(x) This will add the new entry to the vector and increase its size by one.
So the first loop should be something like
1 2 3 4 5 6 7
string name; //Temp string for input
for (int i = 0; i <= numStudents; i++)
{
cout << "Student " << i+1 <<" last name: "; //num is not needed
getline (cin, name); //Read the name
student_lastnames.push_back(name); //Add to vector
}
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();
}
thanks for the info on using cin. 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.