Two Inputs & An Output

Hello Again Everyone!

I seem to be having an issue where when making a comparison between two values in an array, only the first of said values is recognized.

Data File 1:
CXF341 toyoTA caMRy s
rmj441 NiSSan verSA C
ZmW562 hYundai Tucson S
987abcd ford fiEsta C

Data File 2:
cxf341
987ABCD
rmzddd
rmJ441

Example code:

1
2
3
4
5
6
7
for (int r = 0; r < length; ++r)
    {
      if (yours[r].plate == mine[r].rentPlate)
        {
          outfile << "rented.\n";
        }
    }

...where 'yours[r].plate' is pulling in the license numbers from Data File 1 via a struct; and 'mine[r].rentPlate' is storing Data File 2.

The output is "rented." printed only once, non-looped.

How do I make the license numbers from each file recognize each other if and only if they are the same?

Sidenote: At this point in the program, I have already run a "transform" function to make all the license's characters capital letters--for both data files. So I would assume that it makes these values more recognizable to each other.

Thanks for any and all help!

Edit: AHA! So whats happening is it is recognizing only values stored in the same place in each array.
This makes since to me since I can see it finds the first values comparable since they are both located at array[0].
So, what should I do in that for loop to make the program recognize same values in other array positions; i.e. array[1] of data file one and array[3] of data file two?
Last edited on
If you use one loop to iterate over all the elements in one array, it makes sense for you to use another loop inside that loop to iterate over all the elements in the other array and compare them to the current element in the containing or outer loop.

In other words, use a nested loop.
@cire,

Would you be able to give me an example?

Apologies for my ignorance. This is the final assignment in my 101 c++ class.
Last edited on
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
// https://ideone.com/QrcGo8

#include <iostream>
#include <iterator>
#include <string>

std::string yours[] = {
  "cxf341",
  "rmj441",
  "zmw562",
  "987abcd"
};

auto yours_length = std::end(yours) - std::begin(yours);


std::string mine[] = {
  "cxf341",
  "987abcd",
  "rmzddd",
  "rmj441"
};

auto mine_length = std::end(mine) - std::begin(mine);


int main() {
    for (auto i = std::size_t{ 0 }; i < mine_length; ++i) 
    {
        auto matched = false;
        for (auto j = std::size_t{ 0 }; j < yours_length && !matched; ++j)
        {
            if (mine[i] == yours[j]) {
                std::cout << mine[i] << " has a match in mine[" << i << "] and yours[" << j << "]\n";
                matched = true;
            }
        }

        if (!matched) {
            std::cout << mine[i] << " has no match in yours.\n";
        }
    }
}
Topic archived. No new replies allowed.