What is wrong with this 2d array?

This is a discussion on What is wrong with this 2d array? within the C++ Programming forums, part of the General Programming Boards category; The goal is to print a array w/either "1" , nothing " ", but I'm trying to make it into ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    10

    Unhappy What is wrong with this 2d array?

    The goal is to print a array w/either "1" , nothing " ", but I'm trying to make it into and If statement so it will do one or the other not both......becuase regardless of what the file says is always prints the array w/1's and spaces even when the file is all 2's or all 1's or nothing :?

    So I'm kind of lost and yes this was intended as a part of my turtle assignment thing, but more as a test to see if i could get the 2d array to actually work and well I'm not having success


    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    
    using namespace std;
    
    const int SIZE = 30;
    const int SIZE2 = 30; 
    void checkPen(int);
    void checkPen2(int);
    int main()
    {
        ifstream infile;
        int fileInput[SIZE][SIZE2], 
            position;
        
        infile.open("turtle.dat");
        
       infile >> fileInput[SIZE][SIZE2];
       for (int i =0; i < SIZE; i++){
           for(int j =0; j < SIZE2; j++){ 
        if (fileInput[SIZE][SIZE2] = 1){
           checkPen(position);
           }
            if (fileInput[SIZE][SIZE2] = 2){
           checkPen2(position);
           }
           }
           }
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    void checkPen(int position){
         cout << "1";
       
         }
    void checkPen2(int position){
         cout << " ";
         
         }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    20
    check your if . must be if(something==1) and not if(something=1). The first if will check if something is equal but the second if will make something equal to 1 and then will check if something is true or false.
    So your first if statement makes fileInput[SIZE][SIZE2] equal to 1 and then because 1 means true all the statement is true and you print allways 1.
    Last edited by xmariux; 04-10-2009 at 01:00 PM.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    so if i change the
    Code:
    if (fileInput[SIZE][SIZE2] = 1){
           checkPen(position);
           }
    Code:
    if (fileInput[SIZE][SIZE2] ==1){
           checkPen(position);
           }
    it should work:?....

    I'm a littl lost but somewhat get what you are saying about checking if something is false, but could you write a piece of example code showing this with the code I provided :?

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    20
    sorry man i don't have enough time but the first thing that i saw was your if statements. just change the '=' to '==' on your if's, to make them work and not changing fileInput[SIZE][SIZE2] values. 0 is false 1 and bigger than 1 is true.in your case you allways will be true because allways fileInput[SIZE][SIZE2] will be 1(true). Don't think about true or false just think if your if works or not well

    you have 2 if statements with this fault

    if (fileInput[SIZE][SIZE2] = 1){
    checkPen(position);
    }
    if (fileInput[SIZE][SIZE2] = 2){
    checkPen2(position);
    }


    change both
    Last edited by xmariux; 04-10-2009 at 01:12 PM.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You are consistently accessing memory outside of your array. Your array is defined as:
    Code:
    int fileInput[SIZE][SIZE2]
    which means that you can access up to
    Code:
    fileInput[SIZE-1][SIZE2-1]
    Also in your loop you have:
    Code:
    if (fileInput[SIZE][SIZE2] = 1){
           checkPen(position);
    }
    Don't you mean for that to be the following?
    Code:
    if (fileInput[i][j] == 1){
           checkPen(position);
    }

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    well what u put seems to make some sense to me...and after i tried modifying the code I got:

    111111111111 Press any key to continue . . .

    for output instead of:
    11111111111111111111111111111111111111111111111111 111111111111111111111111111111
    11111111111111111111111111111111111111111111111111 111111111111111111111111111111
    11111111111111111111111111111111111111111111111111 111111111111111111111111111111
    11111111111111111111111111111111111111111111111111 111111111111111111111111111111
    11111111111111111111111111111111111111111111111111 111111111111111111111111111111
    11111111111111111111111111111111111111111111111111 111111111111111111111111111111
    11111111111111111111111111111111111111111111111111 111111111111111111111111111111
    11111111111111111111111111111111111111111111111111 111111111111111111111111111111
    11111111111111111111111111111111111111111111111111 111111111111111111111111111111
    11111111111111111111111111111111111111111111111111 111111111111111111111111111111
    11111111111111111111111111111111111111111111111111 111111111111111111111111111111
    11111111111111111111Press any key to continue . . .

    but thats with the file reading : 1 2 2 2 1 1 2 and well it should just print: "1 11 "
    shouldn't it:?...or something wrong with my loop in the way it read the file x amount of times:?

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Is your code to load the 2D array from the file still the same as it is up above? Because that won't work.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    yes and y wont it work:? is it because i need and else for the if's by chance?
    Last edited by FoxeySoulSLayer; 04-10-2009 at 03:30 PM.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Here's a example on how to read integers from a file:
    Code:
    int i;
    while(infile >> i)
        cout << "Read integer: " << i << endl;
    Now you could expand that to:
    Code:
    int i;
    while(infile >> i)
    {
        fileInput[x][y] = i;
    }

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    20
    In your first post for runs ok but your if(both) check every time the same
    fileInput[SIZE][SIZE2]. To check every value of the array change to fileInput[i][j]
    The values SIZE &SIZE2 are the same for every i & j. So you make a check with no meaning

    bithub wrote that but i found it just right now sorry
    Last edited by xmariux; 04-10-2009 at 04:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  2. Copying from one 2d array to another....with a twist
    By Zildjian in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2004, 07:39 PM
  3. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM

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