Thread: Help finding the error

  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
    28,413
    It looks like you forgot to declare sum and initialise it to 0.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,413
    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;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 01:10 AM