I'm trying to to find elements into an array and look for the same elements into another array and i'm trying to assign the same ones with a 'y' for yes and 'n' for no, here is the code i did, but the code only reads it index by index, i want to mix it, could you help me please ?
for example
if i want to read the text below, my code would say 'y' for the first 4 elements into the first array then 'n'for the rest, it should read 'y' for s1

c1 c1
c2 c2
c3 c3
c4 c4
c5 s1
s1 b1

ifstream infile,missingFile;

infile.open("/Users/ea0409952/Documents/COSC1337home/abebe-lab14/deck.txt");
missingFile.open("/Users/ea0409952/Documents/COSC1337home/abebe-lab14/defective.txt");

if ( !infile || !missingFile) // in case it doesn't open
{
    cout <<"Unable to open file, try again please!" << endl;
    return 1;

}

/*
if (!missingFile)
{
    cout << "unable to open defective file" << endl;
}
*/

string standard[52];
string sample[52] = {""};
int count = 0;


/*
while (infile >> standard[count])
{
   missingFile >> sample[count];
   count++;
}
*/

for (int index = 0; index < 52; index++)
{
    infile >> standard[index]; //>> sample[index];
    missingFile >> sample[index];


    //missingFile >> defective[index];
    //cout << standard[index] << "      " << sample[index] << endl;
    //cout << defective[index] << endl;
}
/*
while (infile >> standard[count])
{
    missingFile >> sample[count];
    count++;
}
*/

for (int index = 0;index < 52;index++)
{
    if (sample[index] == standard[index])
    {
        standard[index] = 'y';

    }
       else
         {
            standard[index] = 'n';
         }
    cout << standard[index] << "      " << sample[index] << endl;

}




//cout <<"show this: " << standard[10] << endl;





infile.close();
missingFile.close();
return 0;
}

What you need is a nested loop. For each element in standard, go through sample, looking for matching elements. If you find one, assign 'y' and quit the inner loop. If you get through the sample loop without finding anything, set the standard value. Do you really want to overwrite your data with 'y' and 'n'?

int index, index2;

for (index = 0; index < 52; index++)
{
    for (index2 = 0; index2 < 52; ++index2)
    {
        if (sample[index2] == standard[index])
        {
            standard[index] = "y";
            break;
        }
    }
    if (index2 == 52)
        standard[index] = "n";
    cout << standard[index] << "      " << sample[index] << endl;
}

I pulled the index definition out of the for loops so the variable is not being redefined all the time and so that I can reference the variable outside the loop. Notice the inner loop which looks through the sample array each time; it has its own control variable. If it finds it, standard[index] is changed to "y" and break gets out of the inner loop. At the bottom of the loop, I check if index2 got to the end. If so, we did not find a match, so set standard[index] to "n". Both standard and sample are defined as arrays of strings, so I use string literals instead of character literals to assign to standard.

This article has been dead for over six months. Start a new discussion instead.