### Question about executing a step within a loop

Hi guys.
I'm trying to make a program where if more values were added then initially declared in then I want to make it the case that the last value is ignored.

So I have my input coming in from a data file.
Within the first line of the input it will declare the points per assignment as well as the amount of assignments.
Like so:
 5 10 5 20 15 10 20 15 -1

The -1 represents a sentinel.
So in this case there are 8 assignments and the total possible points are 100.

So the next line would state the score a student received on the assignments.
Like so:
Student A:
 5 10 5 20 15 10 20 15 -1

Student A recived a %100.

But we might have a case like Student B:
 5 10 5 20 15 10 20 15 50

Student scored 150/100. This cannot be allowed.

so I'm thinking I make a counter for the first line feed like so:

 ``123456`` `````` while (total_points != -1) // While the total points are not 1 then add up all the assignment points within the data set { sum = sum + total_points; cin >> total_points; assignment_count++; }``````

and then something like this:
 ``12345678`` `````` cin >> first_letter; // Read the first value from the new line while (cin) { cin >> first_number >> second_number >> second_letter; cin >> student_points; student_sum = 0.0; student_assignment++;``````

But then I'm a little confused.

Should I implement a if statement within the second while loop?
like
 ``12`` ``````if (student_assignment > assignment_count) then ignore the last value in student count, or whatever extra values there are.``````

How can I implement that if statement within my second while loop?
You could put the values into a vector with `push_back`, then you have them stored, so you can do whatever analysis you like and erase items if you wish.

 http://www.cplusplus.com/reference/stl/vector/erase/

Just one little trick:

`sum = sum + total_points; ` can be written `sum += total_points; `

Hope all goes well
Topic archived. No new replies allowed.