Program keeps aborting (vectors)

So basically my program keeps aborting because subscript is out of range. What I am trying to do is get numbers from a file using vectors. It only aborts when I try to use case 3. Right now I am using m<salary.size() so that I can get all the numbers. I don't know what I am doing wrong.

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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

int main(){
	int choice = 0; // for switch statement
	int money; // user input
	vector<int> salary;
	


	ifstream dataFile;
	dataFile.open("newdata.txt");//reads file
	if (!dataFile) cout << "Error" << endl;
	std::string value;
	while (getline(dataFile, value, ',')) //storing data from istream into value
	{

	
	salary.push_back(std::stoi(value));
	}
	dataFile.close();



	int largest = salary[0];
	int lowest = salary[0];
	do {                                            //runs program again unless user chooses option 4
		cout << "Please select 1,2,3,or 4: \n";        //creates menu
		cout << "Search Salary Enter 1 \n";
		cout << "Generate stats file enter 2 \n";
		cout << "Print salaries, Enter 3\n To quit, enter 4 \n";
		cin >> choice;

		switch (choice){
		case 1:  cout << "What is salary\n"; 
			bool temp;     //sees if data from file matches user input
			cin >> money;
			for (int x = 0; x < salary.size(); x++) {
				
				if (money == salary[x])
				{
					temp = true;
				}
				
			}
			if (temp == true)
		
	{
				cout << "Matching Salary Found\n";

			}
			else cout << "You entered the wrong salary\n";  // if it doesn't match, output "wrong salary"
				
				break;
		case 2:{
				   for (unsigned int num = 0; num <= 4; num++){ //finds highest salary
					   if (salary[num] > largest){
						   largest = salary[num];
					   }
				   }
				   cout << "Highest score is " << largest << endl;

				   for (int lnum = 0; lnum <= 4; lnum++){ // finds lowest salary
					   if (salary[lnum] < lowest){
						   lowest = salary[lnum];
					   }
				   }
				   cout << "Lowest score is " << lowest << endl;
				   ofstream newFile("stats.txt");   //creates new file
				   newFile << largest << lowest;  //stores into new file
				   newFile.close();
				   cout << "Stats.txt has been generated\n The highest and lowest salaries are: " << largest << " " << lowest << endl;
		}
			break;
		case 3:cout << "The salaries are: " << endl; //outputs all salaries
			for (int m = 0; m <= salary.size(); m++){
				cout << salary[m] << endl;
			}

			break;
		case 4: exit(0); break;

		default:cout << "Choose programs 1-4\n";
		}
		cout << endl;
		cout << endl;
	} while (choice != 4);
	return 0;
}
Last edited on
So, just going off what I am looking at here, you might be running into the problem of your

salary.size()

For C++ vector the size function returns the number of elements (or actual objects) within the container. This is not the same as the storage capacity.

Take a look at the links below.

http://www.cplusplus.com/reference/vector/vector/size/

http://www.cplusplus.com/reference/vector/vector/capacity/
As with an array, a vector's first element is at index 0. So, if you have a vector with x number of elements, valid indexes are from 0 to x-1. You are trying to use x as an index. Don't do that.
Topic archived. No new replies allowed.