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

This is a discussion on Receiving integers into an array in single line, with spaces between the integers within the C++ Programming forums, part of the General Programming Boards category; I am a C++ newbie and trying to write a program that will receive 8 integers from a user. The ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    9

    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;
    }

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    6
    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

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    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.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems Reading Line With Unknown Number of Integers
    By envec83 in forum C Programming
    Replies: 3
    Last Post: 09-27-2011, 11:20 AM
  2. Scan a line of integers but skip spaces
    By Creedy in forum C Programming
    Replies: 6
    Last Post: 05-21-2010, 08:00 PM
  3. Replies: 15
    Last Post: 11-12-2008, 05:12 PM
  4. Extracting Integers From a Single Variable
    By parx86 in forum C Programming
    Replies: 5
    Last Post: 05-02-2008, 12:27 PM
  5. inputting line of text vs. integers in STACK
    By sballew in forum C Programming
    Replies: 17
    Last Post: 11-27-2001, 10:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21