1. ## Excercise in book

Hi. I've been reading C++ Primer, 4th Edition lately and have been attempting the excercises as I go along. I've done most of them. I've came across one which I don't really understand:

"Read a set of integers into a vector. Calculate and print the sum of each pair of adjacent elements in the vector. If there is an odd number, tell the user and print the value of the last element without summing it. Now change your program so that it prints the sum of the first and last elements, followed by the sum of the second and second-to-last and so on."

Right, I get the first sentence. But what does it mean with the second part (in bold)?

This is what I have so far:
Code:
```#include <iostream>
#include <vector>

int main()
{
std::vector<int> Container;
int ContainerSize = 10;
int Temp = 0;

for(int i = 0; i <= ContainerSize; i++)
{
std::cout << "Element " << i << ": ";
std::cin >> Temp;
Container.push_back(Temp);
}
}```
Sorry for asking it here, but there is no answer book and you guys/girls are very smart.

2. Suppose the vector contains: 2, 3, 4, 5, 6, 7
Then you should print: 5 9 13
i.e., 2+3 4+5 6+7

Suppose the vector contains: 5, 4, 3, 2, 1
Then you should print: 9 5 1 (odd number of elements)

Of course, the exact format of the output is up to you.

3. Note that your code specifies a size of 10 (or actually 11 since your loop is set up to run 11 times). The exercise doesn't seem to want you to limit the number of entries. You can ask the user for the number of entries first, or you can tell them to input 0 or -1 or something like that to show that they are done entering numbers.

4. Thank you very much. And I'll ask the user for input Daved.

5. I'm done!

Code:
```#include <iostream>
#include <string>
#include <vector>

int main()
{
std::vector<int> Container;
std::vector<int> FinalValues;
int ContainerSize;

std::cout << "Container size: ";
std::cin >> ContainerSize;

int Temp = 0;

for(int i = 0; i < ContainerSize; i++)
{
std::cout << "Element " << i << ": ";
std::cin >> Temp;
Container.push_back(Temp);
}
for(int i = 0; i < ContainerSize; i++)
{
//Once all values have been inserted into the vector

if(!(i &#37; 2)) //Element number is even
{
if((i + 1) < ContainerSize)
{
FinalValues.push_back(Container[i] + Container[i + 1]);
}
else
{
FinalValues.push_back(Container[i]);
}
}
}
for(std::vector<int>::size_type i = 0; i < FinalValues.size(); i++)
{
std::cout << FinalValues[i] << " ";
}
}```
Anyone give me suggestions? Pretty big but it works

6. Looks good to me.

Has your book mentioned checking for or handling input errors? If the user types a character instead of a number you'll have problems.

One solution is to use a while loop to get input and keep asking until they input a valid number:
Code:
```while (!(std::cin >> Temp))
{
// clear the fail bit
std::cin.clear();

// empty any and all bad characters from stream
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

// give error message and new prompt here:
std::cout << "Invalid element, try again: ";
}```
You'll need to #include <limits> for numeric_limits and <ios> (I think) for streamsize.

7. Nope, it hasn't talked about any error handling... yet. Thanks for the code, I'll have a mess about with it.