Help finding the error

This is a discussion on Help finding the error within the C++ Programming forums, part of the General Programming Boards category; I am trying to create the code for a program using a for loop that will add the elements of ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    8

    Help finding the error

    I am trying to create the code for a program using a for loop that will add the elements of an array and display "sum = (sum of the array)". I think I am pretty close but I am getting 1 error. Can anyone give me a nudge in the right direction?


    Code:
    #include <iostream>
    
    #include <iomanip>
    
    using namespace std;
    
    int main()
    
    {
    
    int myarray[] = {3,5,22,4,17,6,14};
    
    for (int i = 0; i < 7; i++)
    {
    sum += myarray[i];
    }
    
    
    cout << "sum = " << sum;
    
    cout << endl;
    
    return 0;
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    It looks like you forgot to declare sum and initialise it to 0.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    You didn't create the variable sum.
    Also, do indent properly.
    And, don't put a row between each line. It only makes it harder to read.
    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.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    Ok. Here's where I am at now. I've been struggling with this for 2 days now. Can somebody please tell me what I'm doing wrong?

    Code:
    #include <iostream>
    
    #include <iomanip>
    
    using namespace std;
    
    int main()
    int sum = 0
    {
    
    int myarray[] = {3,5,22,4,17,6,14};
    
    for (int i = 0; i < 7; i++)
    {
    sum += myarray[i];
    }
    
    cout << "sum = " << sum;
    
    cout << endl;
    
    return 0;
    
    }
    Thanks!!!

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    sum must be defined inside the body of main...
    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.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    Your placement of the definition of sum is incorrect. You probably intended to write:
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
        int sum = 0;
        int myarray[] = {3,5,22,4,17,6,14};
    
        for (int i = 0; i < 7; i++)
        {
            sum += myarray[i];
        }
    
        cout << "sum = " << sum << endl;
    
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    Ok that worked. Thank you so much! I would have spent all weekend pulling my hair out over this!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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