# Receiving integers into an array in single line, with spaces between the integers

• 03-26-2012
IPthereforIam
Receiving integers into an array in single line, with spaces between the integers
I am a C++ newbie and trying to write a program that will receive 8 integers from a user. The integers will be separated by spaces. The program will then display the integers in reverse order.

I also need to derive the average of the input integers

In the output of the code below, I am asked for the integers and enter them, but I don't think the values are being received. The "sum" results in the value of 131 no matter what integers I use.

Any help would be greatly appreciated. Sorry about not using tags.

Code:

```#include <iostream> using namespace std; int main() {     int list[8];     int i;     int ave, sum;     cout << "Please enter 8 positive integers:";     for (i = 0; i < 8; i++ )       {           cin >> list[i];       }     cout << endl << "The values in reverse order are:" << endl;     for (i = 7; i >= 0; i--)     {         sum += list[i];         cout << list[i] << " ";     }     ave = sum/8;     cout << "The average is " << sum << "/8 " << "= " << ave;     return 0; }```
• 03-26-2012
aquatorrent
you haven't give 'sum' a value. so when the first time 'sum' does a calculation for the 'list[7]', it won't work
try assigning 'sum' a value ;)
• 03-26-2012
IPthereforIam
Thank you aquatorrent, that worked. I never would have guessed that was the problem. I assumed that if I didn't assign sum a value it would be 0.
• 03-26-2012
Elysia
Primitive data types (such as int; classes are an exception) are not initialized by default. You need to explicitly initialize them unless they are global.
Also, consider upping the warnings levels on your compiler (or if you already got warnings, fix them). Most compilers I should say will warn about such things.