Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Struct value becoming garbage when leaving for loop
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
coltson
n00b
n00b


Joined: 15 Oct 2005
Posts: 51

PostPosted: Sun Jun 23, 2013 5:16 am    Post subject: Struct value becoming garbage when leaving for loop Reply with quote

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:
Quote:

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
Quote:
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
Back to top
View user's profile Send private message
Hu
Watchman
Watchman


Joined: 06 Mar 2007
Posts: 8602

PostPosted: Sun Jun 23, 2013 3:21 pm    Post subject: Reply with quote

You placed j inside the loop, so it is always zero when it is used as a subscript.
Back to top
View user's profile Send private message
Navar
Apprentice
Apprentice


Joined: 20 Aug 2012
Posts: 222

PostPosted: Mon Jun 24, 2013 2:36 am    Post subject: Reply with quote

Adding further to this, you could relocate the declaration for j into the initialization portion of the for loop. Multiple declarations of the same type, while allowed there, some will frown on this from a style standpoint.

You could add a static qualifier to your statement declaring j. If you do this, you may as well move that declaration closer to the statement block j is actually referenced (inside the beginning of your if {} block within the for loop). The reasoning being to put local variables as close to limited in scope as possible and their declaration easier noticed.

Finally you could do it pre-C99 format if you wish, declared at the top portion of a function block or post C99 and C++ just by moving the j declaration you have to before your for loop.

E.g. of all four:

Code:

#include <iostream>
#include <iomanip>

using std::cout;
using std::setw;
using std::endl;

int main(int argc, char *argv[]) {

                            // we could have extra statements prior
  int f0 = 0, f1 = 1, f=1;  // or older C method, on stack, scope is main() { }

  for(int i = 0, t = 0; i < 10; ++i) {

    int foo = 0;    // initialize everytime, stack variable usually allocated
                    // once even if it appears to be popped off and pushed onto
                    // in other words, performance should be the same.

    int static j = 1;  // initialize once, data portion of executable
                       // still limited into this { } scope.
    j += j;

    cout << setw(4) << j   \
         << setw(4) << f   \
         << setw(4) << foo \
         << setw(4) << t << endl;

    f = f0 + f1;
    f0 = f1;
    f1 = f;

    ++foo;
    ++t;
  }
  // j is undefined in scope here, just like i, foo and t

  return 0;
}
Back to top
View user's profile Send private message
Genone
Retired Dev
Retired Dev


Joined: 14 Mar 2003
Posts: 8993
Location: beyond the rim

PostPosted: Tue Jun 25, 2013 6:10 am    Post subject: Reply with quote

As others have said, you redefine j within your for-loop, so any modifications to it are lost. You should probably define it just before the for-loop (if you do it outside the while-loop you still need to clear it's value before the for-loop). As you're constantly (re)setting lineNumbers[0] it will have the last value assigned after the loop. All the other indices will have uninitialized (so more or less random) values.
Your second problem is most likely triggered by an empty trailing line in your input file (it will skip the for-loop).
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum