2 Replies - 1068 Views - Last Post: 28 May 2011 - 08:29 PM Rate Topic: -----

#1 shattered.likeness  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-May 11

Can not find if element in an array is NULL

Posted 28 May 2011 - 07:59 PM

Working on a homework assignment for my C++ class, and have encountered unexpected behavior. The assignment is:

Quote

Write a program that has an array of at least 10 string objects that hold people's names and phone numbers.The program should ask the user to enter a name or partial name to search for in the array. Display any entries in the array that match.


However, I would like to add the message: "No matching names or numbers were found." if there are no matches found.

Here is my code thus far:

#include <iostream>
#include <string>

using namespace std;

int main ()
{
	const int SIZE = 10,   //Maximum Number of items
			  S_LENGTH = 26; // Maximum Length of name and number

	//Array of Names and Phone Numbers
	char phone[SIZE][S_LENGTH] = 
	{
		"Beckey Warren, 678-1233",
		"Joe Looney, 586-0097",
		"James Duckworth, 294-1871",
		"Geri Palmer, 223-8787",
		"Lynn Presnell, 887-1212",
		"Roy Bonnett, 694-1855",
		"Celeste Oprean, 694-1773",
		"Blain Jones, 694-1772",
		"Bob Kain, 586-8712",
		"Jean James, 678-4939"
	};

	char lookUp[S_LENGTH]; // For user input
	char *strPtr = NULL;   // Result from strstr
	int myArray[SIZE]; //Array to hold the found items
	
	// Get user input.
	cout << "Phone Directory\n\n";
	cout << "Enter a (partial) name or phone number to look up:\n\n";
	cin.getline(lookUp, S_LENGTH);
	cout << "\n\n";

	// Search for the String.
	int i = 0; // index
	int num = 0;
	while (i < SIZE)
	{
		strPtr = strstr(phone[i], lookUp);
		if (strPtr != NULL)
		{
			num++;
			myArray[num] = i;
		}
		i++;
	}

	// Output the result of the search.
	for (int i = 0; i < num; i++)
	{
		cout << phone[(myArray[i])] << endl;
	}


	//Pauses program to view results
	cout << "\n\nPlease press ENTER to continue...\n";
	cin.ignore();

	return 0;
}


This much of the code works flawlessly. However, I am unaware as to how I should test if myArray has not had any information stored in the first element of the array. I have tried this:

	// Output the result of the search.
	if (myArray[0] == NULL)
	{
		cout << "No matching names or numbers were found.";
	}
	else if(myArray[0] != NULL)
	{
		for (int i = 0; i < num; i++)
		{
			cout << phone[(myArray[i])] << endl;
		}
	}



Unfortunately, the above method did not work. I have reread the chapter on arrays in the text book, and searched through google for an answer to this issue, and have not yet been able to find an answer.

Thanks in advance for help you can offer.

Shattered.likeness

Is This A Good Question/Topic? 0
  • +

Replies To: Can not find if element in an array is NULL

#2 kaa  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 29
  • Joined: 15-April 11

Re: Can not find if element in an array is NULL

Posted 28 May 2011 - 08:21 PM

myArray is an int array so why don't you initialize myArray[0] with some value that can't be found in anyone's name or phone number, like 65536. Then after you finish searching you can check to see if that value is still there or if it changed.

Like
if (myArray[0] != 65536)
{
// do something
}

Was This Post Helpful? 1
  • +
  • -

#3 shattered.likeness  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-May 11

Re: Can not find if element in an array is NULL

Posted 28 May 2011 - 08:29 PM

Thanks for that!

Sometimes it is the simple solutions that we completely overlook.

Initialized myArray[0] to -1, and now it functions as intended.

EDIT: Also realized that I put the code into the first post incorrectly, but am unable to find the button to edit the Original Post. Incremented myArray before modifying it.

This post has been edited by shattered.likeness: 28 May 2011 - 08:33 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1