Adding array contents

This is a discussion on Adding array contents within the C++ Programming forums, part of the General Programming Boards category; I am doing this array program, and I keep getting an error up when I try to sum the elements ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479

    Adding array contents

    I am doing this array program, and I keep getting an error up when I try to sum the elements of the array

    .cpp invalid conversion from `int*' to `int'
    .cpp name lookup of `i' changed for new ISO `for' scoping
    .cpp using obsolete binding at `i'

    Here is the code:

    Code:
    #include <iostream>	
    
    using std::cout;		
    using std::endl;		
    using std::cin;			
    
    int main ( void )
    {
       int ta = 0;
       int tc = 0;
     		
       int survey1[11] = { 1, 5, 10, 8, 4, 4, 2, 10, 7, 6 };
     		
     		
       char survey2[21] = { 'm', 'f', 'f', 'm', 'f', 'f', 'f', 'm', 'm', 'f',
                            'm', 'f', 'm', 'm', 'm', 'm', 'm', 'f', 'f', 'm' };
    		 								 	   
       int survey3[51] = { 1, 2, 1, 2, 2, 2, 1, 1, 1, 1, 
    	   	       2, 1, 2, 1, 1, 1, 2, 2, 1, 2,
    	   	       1, 2, 1, 1, 1, 2, 2, 2, 2, 2,
    	               1, 2, 1, 2, 1, 1, 1, 1, 1, 2,
    	  	       1, 2, 1, 2, 1, 2, 1, 1, 2, 2 };
    	   										  
       cout << "\tStudent survey result program:\n\n";
    		 
       for ( int i = 0; i <= 10; ++i )
    
      cout << "Survey 1[] = " << survey1[i] << endl;
    		 
      ta += survey1;  // ERROR LINE
    		 
      cout << "\n\n\n";
    					 	   
      for ( int i = 0; i <= 20; ++i )
       
      cout << "Survey 2[] = " << survey2[i] << endl;
         
      cout << "\n\n\n";
         
      for ( int i = 0; i <= 50; ++i )
         
      cout << "Survey 3[] = " << survey3[i] << endl;
         
      cout << "\n\n\n";
         
      cout << "Total of survey 1 is: " << ta << endl
             << "\nTotal of survey 3 is: " << tc << endl;
    		 	 
        cin.get();	
        
        return 0;
    }
    I do not know what the new look up error is, I have never seen
    it before.. Anly help appreiciated!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    > ta += survey1;
    1. You need to index the array - say ta += survey1 [ i ];
    2. You need some braces to group all the relevant statements with the for loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Thanks salem that did the trick! I knew I was on the right track, thanks for the advice, I really shouid of thought of that as I was coding it, maybe I need to slow the pace a little! lol

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    when you have a for loop containing only one statement, you don't have to have braces, but you should keep it tabbed and right underneath the loop (or next to if short enough) so it is readable.

    When you have two or more statments, you keep them in hte braces.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Why don't you just use
    Code:
    using namespace std;
    ???

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Quote Originally Posted by bumfluff
    Why don't you just use
    Code:
    using namespace std;
    ???
    it doesn't hurt to have using for the relevant operations, It's good practice to know the standard library (I'm told), using namespace std kind of eliminates that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding up the rows of a 2D array
    By youngvito in forum C Programming
    Replies: 31
    Last Post: 06-11-2009, 01:06 PM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. copy contents of array to single value?
    By mapunk in forum C Programming
    Replies: 3
    Last Post: 12-02-2005, 08:28 PM
  4. Rearranging contents of an array
    By jlf029 in forum C++ Programming
    Replies: 9
    Last Post: 11-07-2005, 11:34 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM

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