#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Posts
    8
    Rep Power
    0

    Struct value becoming garbage when leaving for loop


    I am trying a read file that contains sequence of passwords in different lines. Here is the layout

    3
    1 4 7 2 8 9
    7 5 6 8 9 2
    5 5 4 3 2 9

    The first value is the number of passwords. I read it first to a single variable. No problem with that.
    The problem it is with the other values. I have two nested loop. The first one that cares of each line (and so of each password) It creates an Password structure that has an int array to store each individual value of the password. This it is done inside a for loop. When I print the value stored in this int array inside the for loop with this code
    Code:
    cout << password.lineNumbers [j] << " Inside for loop" << endl;
    it works. Printing it outside the for loop, with this code
    Code:
    for (int k=0; k<6; k++)
          cout << password.lineNumbers [k] << " Outside for loop" << endl;
    prints on the screen a bunch of weird numbers. Here is the full code:

    Code:
    struct Password {
      int lineNumbers [6];
    };
     ..........( irrelevant stuff)
     std::vector <Password> PasswordID;
    ........... (irrelevant stuff)
     getline (fileReader,line);
      numberPasswords = line[0] - '0';
      cout << "The number os passwords it is: " << numberPasswords << endl;
    
      while (fileReader.good()) {
        getline (fileReader,line);
        //int lineNumbers [6];
        Password password; 
        cout << line << endl;
        for (int i=0; line[i] != '\0'; i++) {
          int j=0;
          if (line [i] != ' ') {
            //cout << line[i] << endl;
            password.lineNumbers[j] = line[i] - '0';
            cout << password.lineNumbers [j] << " Inside for loop" << endl;
            j++;
          }   
        }      
        cout << '\n' << endl;
        for (int k=0; k<6; k++)
          cout << password.lineNumbers [k] << " Outside for loop" << endl;
        PasswordID.push_back (password); 
        cout << '\n' << endl;
        cout << '\n' << endl;  
      }
      
      cout << "PasswordID.size value it is: " << PasswordID.size() << endl;
    I get this output:
    1 4 7 2 8 9
    1 Inside for loop
    4 Inside for loop
    7 Inside for loop
    2 Inside for loop
    8 Inside for loop
    9 Inside for loop

    9 Outside for loop
    -1217128368 Outside for loop
    6 Outside for loop
    -1217128368 Outside for loop
    0 Outside for loop
    0 Outside for loop


    7 5 6 8 9 2
    7 Inside for loop
    5 Inside for loop
    6 Inside for loop
    8 Inside for loop
    9 Inside for loop
    2 Inside for loop

    2 Outside for loop
    -1217128368 Outside for loop
    6 Outside for loop
    -1217128368 Outside for loop
    0 Outside for loop
    0 Outside for loop


    5 5 4 3 2 9
    5 Inside for loop
    5 Inside for loop
    4 Inside for loop
    3 Inside for loop
    2 Inside for loop
    9 Inside for loop

    9 Outside for loop
    -1217128368 Outside for loop
    6 Outside for loop
    -1217128368 Outside for loop
    0 Outside for loop
    0 Outside for loop


    9 Outside for loop
    -1217128368 Outside for loop
    6 Outside for loop
    -1217128368 Outside for loop
    0 Outside for loop
    0 Outside for loop

    PasswordID.size value it is: 4
    So I have no idea why
    Code:
    cout << password.lineNumbers [j] << " Inside for loop" << endl;
    is with the correct values and

    Code:
    cout << password.lineNumbers [k] << " Outside for loop" << endl;
    is with corrupted values

    The second problem it is with the final
    9 Outside for loop
    -1216403376 Outside for loop
    6 Outside for loop
    -1216403376 Outside for loop
    0 Outside for loop
    0 Outside for loop
    that it is being printed on the screen.
    I don't understand why it happens. I only have three lines of code in my file, so the
    Code:
    while (fileReader.good())
    should only execute three times. The content inside the for loop is only being printed on the screen three times, so I also have no idea how four passwords are being created and added to the vector

    Thanks for any help
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,407
    Rep Power
    1871
    Change your debug statement to this
    Code:
    cout << password.lineNumbers [j] << " Inside for loop as position " << j << endl;
    Also, good practice is to give your struct a constructor to initialise your variables, so you can tell the difference between "garbage" and "unused" variables.
    Code:
    struct Password {
      int lineNumbers [6];
      Password() {
        for ( int i = 0 ; i < 6 ; i++ ) lineNumbers[i] = 0;
      }
    };
    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

IMN logo majestic logo threadwatch logo seochat tools logo