Looping through an array with unexpected results.

I couldn't find anything online so..


arr is an array of a struct called key. Each struct has an ID and a .one which is something like "8ANNLP".

When the letter of my password is == to the letter of the ID.
eg. my password is onetwothree
If "o" == .ID it will place the .one of that struct into the .txt file. but my problem is it will keep going and place the .one of "o" and all the following id's in the array until it reaches the last one "Z"


I've debugged to make sure that each bit of the struct is what they should be. (I'm using NETBEANS IDE)




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  if (file.is_open())
    {
        for (int i = 0; i < (length - 1); i++)
        {
            for (int j = 0; j < 51; j++)
            {  
                if (pass[i] == arr[j].id)
                {
                    file << arr[j].one << "\r\n";
                    
                    cout << arr[j].one << "\r\n";
                }
            }
        }
        file.close();
        cout << "It was successful!... Maybe." << endl;

    } else {
        cout << "Could not open file" << endl;
    }


What I expect is that when pass[i] == arr[j].id it outputs arr[j].id and moves on in the array but for some reason it outputs all of the ones after.




If anyone would be kind enough to help then thanks.
I can post any additional code if required.
Hi,

Post the smallest piece of compilable code that demonstrates the problem. We need to know exactly what all the types are, and the input.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <cstdlib>
#include <stdlib.h> 
#include <iostream>
#include <windows.h>
#include <fstream>

using namespace std;
int const seq = 7;

struct key {
    char id;
    char one[6];
    char two[6];
    char three[6];
};

void cypher(key arr[],int size) {
    
    
    ifstream readfile;
    //key keyArrI[52];
    
    readfile.open("cypher.txt");
    if (readfile.is_open())
    {
        for (int i = 0; i < size; i++) {
            if (readfile.eof()) break;
            readfile.get(arr[i].id);
            readfile.get();
            readfile.get();
            if (readfile.eof()) break;
            readfile.get(arr[i].one, seq);
            readfile.get();
            readfile.get();
            if (readfile.eof()) break;
            readfile.get(arr[i].two, seq);
            readfile.get();
            readfile.get();
            if (readfile.eof()) break;
            readfile.get(arr[i].three, seq);
            readfile.get();
            readfile.get();
            if (readfile.eof()) break;
            
        }                
    } else {
        cout << "Couldn't Read File" << endl;
    }   
    
}

void cypherin(key arr[],int size) {

    char filename[20];
    int length;
    
    cout << "How long is your password?: " << endl;
    cin >> length;
    char pass[length];
    
    
    cout << "Enter your password: " << endl;
    cin >>  pass;
    cout << "Enter the text document name: " << endl;
    cin >> filename;
    

    //char pass[] = "passw";
    //char filename[] = "pass.txt";
    
    
    ofstream file;
    file.open(filename);
    
    
    if (file.is_open())
    {
        for (int i = 0; i < (length - 1); i++)
        {
            for (int j = 0; j < 51; j++)
            {  
                if (pass[i] == arr[j].id)
                {
                    file << arr[j].one << "\r\n";
                    
                    cout << arr[j].one << "\r\n";
                }
            }
        }
        file.close();
        cout << "It was successful!... Maybe." << endl;

    } else {
        cout << "Could not open file" << endl;
    }
}

int main() {
    key keyArr[52];
    cypher(keyArr, 52);
    cypherin(keyArr, 52);
    return 0;
}



I put this together which displays the current problem with the only code it needs.
However it requires a file called cypher.txt that is filled with the different strings like this:
a
JKE2AE
8JANNE
9ANENE
b
OOXN2A
LKAEI3
EMAOE3
c
0SNENA
SNES4L
NSKE3A




If the output is required then I can give an example.
Whatever array variable j is going through will be finished first because of your for loop. Your nested for loop will finish before going to the outside array. Im not sure if this answers your question because it was a little unclear to me.
For every I it loops through it goes through 51 for J right? Because it goes through the array and when the char and the id are equal it inputs the right string but all the strings after that.
An array of size 6 is not enough to store a string of length 6 because an extra null character is appended to the end in order to mark the end of the string.
Topic archived. No new replies allowed.