C++ Ranges

Hello,
Recently I have been writing a basic grade tracking program. Basically it read the grades and computes a sum. Then it saves the grades that way you can search and find all the grades. However my search feature can never return the last grade or name saved. The code is very messy but I am only interested in why it won't return the last item in the vector. I think it's a problem with the range.
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
#include <iostream>
#include <ios>
#include <iomanip>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

int main()
{
vector<string> name;
string iName;
string command;
string searchName;
vector<double> grades;
cout << "Please enter a command: " << endl << "[1]enter(grades)" << endl << "[2]search(name of student)" << endl << "[3]grades(find all grades)" << endl;
while(cin >> command){
	if (command == "search"){
		cout << "Enter name of student: ";
		cin >> searchName;
		cout << endl;
		for(vector<double>::size_type index = 0; index <= name.size(); ++index){
			if(name[index] == searchName){
				cout << name[index] << ": " << grades[index];
				break;
			}
			else{
				cout << "Search returned no results" << endl;
				break;
			}
		}
	}
	if(command == "grades") {
		for(int index = 0; index != name.size(); ++index) {
		cout << name[index] << ": " << grades[index]  << endl; 
		}
	}
if(command == "enter") {
while(cin >> iName) {
	if(iName == "end"){
		break;
	}
	name.push_back(iName);
}

for(int nameIndex = 0; nameIndex != name.size(); ++nameIndex)
{
	double final;
	double midterm;

	cout << name[nameIndex] << " mid-term test: ";
	cin >> midterm;

	cout << name[nameIndex] << " final test: ";
	cin >> final;

	cout << name[nameIndex] << " enter grades: ";

	double iGrades;
	double sum = 0;
	double count = 0;
	
	while(cin >> iGrades) {
		sum += iGrades;
		cout << endl;
		++count;
		if(count == 8){
			grades.push_back(0.4 * final + 0.2 * midterm + 0.4 * sum / count );
			cout << "Max Grades entered" << endl;
			break;
		}
	}
	

}
}
}
}


Part I need help with:
1
2
3
4
5
6
7
8
9
10
11
12
13
if (command == "search"){
		cout << "Enter name of student: ";
		cin >> searchName;
		cout << endl;
		for(vector<double>::size_type index = 0; index <= name.size(); ++index){
			if(name[index] == searchName){
				cout << name[index] << ": " << grades[index];
				break;
			}
			else{
				cout << "Search returned no results" << endl;
				break;
			}


-Thanks
index <= name.size()
The most common mistake - you will be accessing vector[size()] on the last loop, which of course, does not exist.

Also, why are you using vector<double>::size_type? I understand that it will probably be the same, but consistency is essence. It makes more sense to use vector<string> because you are searching for a name, not a grade. Nothing wrong, just pointing it out.

Other than that, I don't immediately see anything wrong - could you post your test data (the names of the students you are using?)
Thank you LB,

Actually my problem was:

1
2
3
4
else{
				cout << "Search returned no results" << endl;
				break;
			}


It would check only one item in the vector.
However your comments on keeping consistency were very helpful.
-Thank you so much,
Raxis
Topic archived. No new replies allowed.