Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is the code:

C++
int main()
{
	
	const int numSubjects = 3;

	vector <string> prefix { "first", "second", "third" };
	vector <string> subjects(3);
	vector <float> grades(3);

	for (int i = 0; i < 3; i++) {
		cout << "Type in your " << prefix[i] << " subject: ";
		cin >> subjects[i];
		cout << "Type in your grade for " << subjects[i] << ": ";
		cin >> grades[i];
	}

		float sum = grades[1] + grades[2] + grades[3];
		float average = (sum / 3);


		cout << "AVERAGE GRADE";
		cout << "------------------------------------" << endl;
		cout << subjects[1] << grades[1] << endl;
		cout << subjects[2] << grades[2] << endl;
		cout << subjects[3] << grades[3] << endl;
		cout << "------------------------------------" << endl;
		cout << "Average: " << average << endl;

	
}
Then I get this error:
ErrorPhoto[^]
Please keep in mind I started learning c++ yesterday so I really don't know enough.

What I have tried:

If i delete everything below the "float sum" row, the upper part of the code works
Posted
Updated 7-Mar-19 22:30pm

1 solution

C++
vector <float> grades(3);

C++ is zero based: it means that first element is 0.
A vector of size 3 have elements 0, 1 and 2.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
Member 14175306 8-Mar-19 4:42am    
Thank you! I simply changed out all the 1, 2 and 3 with 0, 1, 2 and it works!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900