1

I'm taking a list of numbers that are strings and comparing them to a list of numbers that are strings in an array. When a number in the list matches a number in the array, "Match Found" is displayed. For the matching numbers nothing else should be displayed. For number 6, "Match Not Found" is displayed.

List: 12346

Array: 12345

Note:

while(not end of file)
{
  for(i = 0; i < arraycount; i++)
  {
    if(strcmp(numberlist.numbers,array[i].numbers) == 0)
      //Display "Match Found"
  }
}

I'm not sure what to do after this point or if I'm even approaching this right. If I put an else statement that displays "Not Found" after the if statement then this is displayed for the "Match Found" numbers.

Example of what I do not want:

1 Match Found Not Found Not Found Not Found Not Found
2 Not Found Match Found Not Found Not Found Not Found
3 Not Found Not Found Match Found Not Found Not Found
4 Not Found Not Found Not Found Match Found Not Found
6 Not Found Not Found Not Found Not Found Not Found

Example of what I want:

1 Match Found
2 Match Found
3 Match Found
4 Match Found
6 Not Found

This is for a beginners class, so I have to keep the code at a beginners level.

Edit:

Solution

while(not end of file)
{
  for(i = 0; i < arraycount; i++)
  {
    found = 0;
    if(strcmp(numberlist.numbers,array[i].numbers) == 0)
    found = 1;
    //Display "Match Found"
  }
  if (found != 1)
  //Display "Not Found"
}
markerpower
  • 345
  • 1
  • 4
  • 13
  • 2
    I think you need to compare it character by character. `strcmp` compares whole string. – haccks Feb 28 '15 at 06:26
  • 1
    Show the rest of your program. The whole source code. – Tarik Feb 28 '15 at 06:28
  • 2
    See [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) for a discussion of why your pseudo-code is undesirable. – Jonathan Leffler Feb 28 '15 at 07:09
  • 1
    Have you learned about the `break` statement yet? If so, use it. If not, then life is messier, but it can be done with a flag variable which is cleared before the inner loop starts and set in the inner loop if a match is found. After the inner loop, you can test whether a match was found and print 'Not found' if there was no match. You probably need the flag even with the break statement, but your code does fewer comparisons on average if you use break after finding a match. – Jonathan Leffler Feb 28 '15 at 07:13
  • 1
    Posting a [Minimal Complete Verifiable Example](http://stackoverflow.com/help/mcve) would make this question a lot easier to answer. – user3386109 Feb 28 '15 at 07:38
  • Thank you Jonathan Leffler. Your suggestion worked. – markerpower Feb 28 '15 at 08:33

1 Answers1

0

Solution

while(not end of file)
{
  for(i = 0; i < arraycount; i++)
  {
    found = 0;
    if(strcmp(numberlist.numbers,array[i].numbers) == 0)
    found = 1;
    //Display "Match Found"
  }
  if (found != 1)
  //Display "Not Found"
}
Armali
  • 18,255
  • 14
  • 57
  • 171