print outside if statement and second function issue

both arr1 and arr2 are separate char arrays where if the letters dont match it prints them out. how can i make both of them print out outside on the if statement? i was thinking through a second function but it highlights the array i send out with a red line. any thoughts on how to do this or if theres a different way?
1
2
3
4
5
6
7
8
9
10
11
for(int x=0; x<SIZE;x++)
{
 if(arr1[a] != arr2[b])
 { 
  cout << arr1[a] << " " << arr2[b] << endl;// prints out unmatching letters
  cout << a << " " << b << endl; // prints out index of unmatch letters
 }
 function2(arr1[],?,?); // not sure how to do this part
}

void function2()
Last edited on
Sym8 wrote:
how can i make both of them print out outside on the if statement?
I don't understand what you mean.
i want it to print out below a statement such as:
1
2
3
4
5
6
7
8
9
10
11
12
13
for(int x=0; x<SIZE;x++)
{
 if(arr1[a] != arr2[b])
 { 
  cout << arr1[a] << " " << arr2[b] << endl;// prints out unmatching letters
  cout << a << " " << b << endl; // prints out index of unmatch letters
 }

 cout<< "unmatch letters: " << endl;
 cout << arr1[a]<< " " arr2[b] << endl; // where i want it to print out
 cout <<a << " " << b << endl; // where i want it to print out
 
}


that way it wont print out cout<< "unmatch letters: " << endl;
for each single one. it does need to stay inside the for loop though. any thoughts?
Last edited on
cout<< "unmatch letters: " << endl; must come before the for-loop.

If you don't want to print anything if there are no matches, then you need some additional logic:
1
2
3
4
5
6
7
8
//before for-loop:
bool mismatch = false;
//inside if statement:
if(!mismatch)
{
    cout<< "unmatch letters: " << endl;
    mismatch = true;
}
First, I think there's something wrong with your loop:
1
2
for(int x=0; x<SIZE;x++) {
 if(arr1[a] != arr2[b])
what is a? What is b? These aren't set in the loop, and x, the index of the loop, is never used.

It sounds like you want to print out the first mismatch only. To do that, just break out of the loop when you find the problem. Assuming that x is the index of the items, the code would be:
1
2
3
4
5
6
7
for(int x=0; x<SIZE;x++) {
    if(arr1[x] != arr2[x]) { 
        cout << arr1[x] << " " << arr2[x];// prints out unmatching letters
        cout << " at index " << x << endl; // prints out index of unmatch letters
        break;
   }
}
Last edited on
Topic archived. No new replies allowed.