another array exercise

This is a discussion on another array exercise within the C++ Programming forums, part of the General Programming Boards category; Hi.. I have another array exercise and it has this debug error again but this time it doesn't give the ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91

    Smile another array exercise

    Hi.. I have another array exercise and it has this debug error again but this time it doesn't give the sum output.

    The code is as follows:


    Code:
    //array initialization and sum output based on user input
    
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    	int ar[7] = {ar[0], ar[1], ar[2], ar[3], ar[4], ar[5], ar[6]};
    	int sumArray = 0;
    	int i = 0;
    
    	cout << "Enter an integer for value 1: ";
    	cin >> ar[0];
    	cout << "Enter an integer for value 2: ";
    	cin >> ar[1];
    	cout << "Enter an integer for value 3: ";
    	cin >> ar[2];
    	cout << "Enter an integer for value 4: ";
    	cin >> ar[3];
    	cout << "Enter an integer for value 5: ";
    	cin >> ar[4];
    	cout << "Enter an integer for value 6: ";
    	cin >> ar[5];
    	cout << "Enter an integer for value 7: ";
    	cin >> ar[6];
    
    		for (i = 0; i < 7; i++);
    	{
    		sumArray += ar[i];
    	}
    	cout << endl;
    	cout << "Total is: ";
    	cout << sumArray << endl;
    }
    Any ideas will be great! Thanks..

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Code:
    int ar[7] = {ar[0], ar[1], ar[2], ar[3], ar[4], ar[5], ar[6]};
    In some languages, this doesn't even compile, and for good reason. Just what do you think this should achieve?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Code:
    for (i = 0; i < 7; i++);
    {
        sumArray += ar[i];
    }
    The semicolon at the end of the for loop is wrong.

    Try:
    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
        int ar[7];
        int sumArray = 0;
        int i;
    
        for (i = 0; i < 7; i++)
        {
            cout << "Enter an integer for value " << i+1 << ": ";
            cin >> ar[i];
            sumArray += ar[i];
        }
    
        cout << endl << "Total is: " << sumArray << endl;
    
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Or even, since you don't ever use the array:
    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
        int sum = 0;
    
        for (int i = 0; i < 7; i++)
        {
            cout << "Enter an integer for value " << i+1 << ": ";
            int tmp;
            cin >> tmp;
            sum += tmp;
        }
    
        cout << endl << "Total is: " << sum << endl;
    
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91

    Smile

    Thanks CornedBee and hk for pointing out my mistakes..

    It's working fine now.. I chose to stick with the array since it's an array exercise although CornedBee's program also works!

    Code:
    //array initialization and sum output based on user input
    
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    	
    	int ar[7];
    	int sumArray = 0;
    	int i = 0;
    
    		for (i = 0; i < 7; i++)
    	{
    	    cout << "Enter an value for " << i + 1 << " : ";
    		cin >> ar[i];
    		sumArray += ar[i];
    	}
    	cout << endl;
    	cout << "Total is: ";
    	cout << sumArray << endl;
    }
    Again, thanks for the help, guys!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 7
    Last Post: 11-25-2008, 12:50 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. (!)Simple array exercise
    By Fizz in forum C++ Programming
    Replies: 8
    Last Post: 05-13-2004, 12:45 PM

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