My names Rob and I'm a newbee to programming so I apologize if my problem is extremely apparent and I am failing to see it. I am trying to have the program accept one test score per line and save each one to a double variable. Then transfer the double variable into it's own element in a vector, also a double. I am using push_back() inside a loop so I can add elements dynamically to the vector and the program will compile without errors. When I debug the program in visual studio 2017, it never seems to put any elements into the vector. Before it does so it throws an exception:
"Exception thrown at 0x76915608 in vectors1.exe: Microsoft C++ exception: Range_error at memory location 0x0068F740."
I was under the impression that push_back() creates an element at the end of the list of elements in the vector so that even if the vector that was created without any elements it should add them on-the-fly.?
The actual program is just for me to learn on so if anyone has a different/better way to store the scores dynamically that would be helpful as well. Thanks to everyone in advance.
int main()
{
vector <double> scores; //creates a vector wth the name "scores"
double temporary = 0.0; //creates a temp spot to save an integer before adding it to the vector's elements
cout << "Thank you for using Rupert's TestMaster Pro\n" //just some instructions
<< "Please enter each test score followed by the ENTER key.\n"
<< "When you are finished, type DONE and press enter for your results!\n";
if(cin >> temporary); // this will repeat the if-block until cin gets an error, then it will skip the block
{
scores.push_back(temporary); //every time the if-block repeats it adds an element on the end of the vector
//and puts the integer that the user typed into that
}
cin.clear(); //this clears the cin buffer from the error generated before
cin.ignore(numeric_limits<streamsize>::max(), '\n');
double totalSum = 0.0, averageScore = 0.0, highestScore = 0.0, lowestScore = 0.0; //creates floating-point(decimal) variables for everything we need
int i = 0;
constint sizeFile = static_cast<int>(scores.size());
for (i; i < sizeFile; ++i); //this for-loop will repeat until it goes through all of the vector's elements
//(when "i" reaches the size of the vector which is represented by "scores.size()")
{
totalSum = totalSum + scores[i]; //every time the loop repeats this adds the new number to the variable "totalSum"
if (scores[i] > highestScore); //this if-loop will run,
//replacing the last number saved in "highestNumber" with the new number from the vector,
//if the new numer is greater then the old one
{
highestScore = scores[i];
}
if (scores[i] < lowestScore); //similar to last loop except checks the new number to see if it is lower
{
lowestScore = scores[i];
}
}
averageScore = totalSum/sizeFile; //divides the sum of all the test scores by the number of elements in the vector
cout << "Average test score: " << averageScore << "\n" //these output the variables stored as the average, highest, and lowest test scores
<< "Highest test score: " << highestScore << "\n"
<< "Lowest test score: " << lowestScore << "\n";
return 0;
}
On line 12 the ; prevents the insertion of temporary into the vector.
On line 29 you try to read from an empty vector.
Pay heed to the compiler warnings:
main.cpp 12 Warning C4390 ';': empty controlled statement found; is this the intent?
main.cpp 26 Warning C4390 ';': empty controlled statement found; is this the intent?
main.cpp 31 Warning C4390 ';': empty controlled statement found; is this the intent?
There are 2 bugs on line 12. First, remove the semicolon. As written the code does this:
1 2 3 4 5 6 7
if(cin >> temporary)
; // do nothing
// Start a new block of code. This executes once, regardless of whether the "if" above succeeds or fails.
{
scores.push_back(temporary);
}
Second, you want this to execute over and over again until it's false, that means you need a while() instead of an if().
1 2 3 4
while (cin >> temporary)
{
scores.push_back(temporary);
}