multidimensional array

This is a discussion on multidimensional array within the C++ Programming forums, part of the General Programming Boards category; i want to read the following data from the input file in a towway dimensional array 2001 25 65 89 ...

  1. #1
    Unregistered
    Guest

    multidimensional array

    i want to read the following data from the input file in a towway dimensional array
    2001 25 65 89
    2002 45 69 77

    I wrote the following code:
    void read_data()
    {
    int sales[2][4];
    ifstream ifs;
    while(!ifs.eof())
    {
    for(int i=0;i<2;i++)
    for(int m=0;m<4;m++)
    ifs>>sales[i][m];
    }
    cout<<sales[i][m]<<endl;
    }

    is there anything wrong with this code please because i does not read well . thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >cout<<sales[i][m]<<endl;
    This won't work, the error is obscure, but very real. The fact of the matter is that when you declare a counter variable in a for loops, that variable only remains in scope as long as control is in the loop block, then the variable goes out of scope. So when you exit both loops, i and m do not exist anymore.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest
    would you be more clear please

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    23

    Re: multidimensional array

    Originally posted by Unregistered
    i want to read the following data from the input file in a towway dimensional array
    2001 25 65 89
    2002 45 69 77

    I wrote the following code:
    Code:
    void read_data()
    {
      int sales[2][4];
      ifstream ifs;
      while(!ifs.eof())
      {
      for(int i=0;i<2;i++)
        for(int m=0;m<4;m++)
          ifs>>sales[i][m];
      }
      cout<<sales[i][m]<<endl;
    }
    is there anything wrong with this code please because i does not read well . thanks
    use indentations to spot errors easier. Your code seems fine...its your cout statement that is wrong. Basically when you cout like that your printing out the 4th element in the 2nd array. So in your case that would print out 77.

  5. #5
    Unregistered
    Guest
    I declared the array sales[2][4] out in the header file but i put it here just to show that i declared it but it is not included int code i wrote.

  6. #6
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    couldn't he declare i and m as static outside the block, would they keep the value then?

  7. #7
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    He should just declare them before the for loops.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >couldn't he declare i and m as static outside the block, would they keep the value then?
    Of course:
    Code:
    void read_data()
    {
      int sales[2][4],
          rows, 
          cols;
      ifstream ifs; // Be sure to open it ;)
      while ( ifs.good() )
        for ( rows = 0; rows < 2; rows++ )
          for ( cols = 0; cols < 4; cols++ )
            ifs>>sales[rows][cols];
      cout<< sales[--rows][--cols] <<endl;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Help -- allocating memory in a multidimensional array
    By jonathan.plumb in forum C Programming
    Replies: 10
    Last Post: 05-20-2009, 11:04 PM
  3. Pointer to multidimensional array
    By ejohns85 in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2009, 11:17 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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