Program encouters a problem a needs to close everytime i run my program

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
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
 #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
    }

Last edited on
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.
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
 #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();    
    
} 
use
 
cin >> name;
Last edited on
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.


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

Because you are storing your first name, last name and score separately you will not easily be able to keep them linked when you match them.

I highly recommend you use a structure, or a class to store your student information.

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

struct Student {
 string FirstName;
 string LastName;
 int Score;
}

Student myStudent;
cin >> myStudent.firstname; // etc

vector<Student> studentList;
studentList.push_back(myStudent);


I would also change your printing loop to use "i" not "l".

You also do not have to specify the size of your vectors.
Simply using
 
vector<string> myVector

is fine.
Topic archived. No new replies allowed.