im supposed to create an address book and so far have gone as far as getting the input..my problem now is the highlighted section..i have to return bak the info put in initially by the user (first, last name and address), based upon a question to the user to input either the last name or the first and last name of the persons he wants to pull up. would appreciate some help
thnx

#include <iostream>

const int maxpeople = 50;

struct person
{
	char firstname[20];

	char lastname[20];

	char address[20];
};

void addpeople(person holdspeople[], int size);

void getperson(person holdspeople[maxpeople], int size, int stringsize);

using namespace std;

person people[10];

int main()
{
	addpeople(people, 10);

	getperson(people, 10, 20);

	return 0;
}

void addpeople(person holdspeople[maxpeople], int size)
{
	for(int i = 0;i < size; i++)
	{

		cout<<"Enter your first name, last name and address respectively"<<endl;
	
		cin>>holdspeople[i].firstname>>holdspeople[i].lastname>>holdspeople[i].address;
	
	}
}


void getperson(person holdspeople[maxpeople], int size, int stringsize)
{	
		char firstname[20];

		char lastname[20];

	[B]for (int i = 0; i < stringsize; i++)
    {
		firstname[i] = holdspeople[i].firstname;
		
		lastname[i] = holdspeople[i].lastname;
    }[/B]
}

Recommended Answers

All 26 Replies

Since you have the coice of entering either last name, or first and last name, you will probably have to ask for which they want to enter then create an if condition
Example:

...
int type;
char firstname[20] = {0};
char lastname[20] = {0};

cout << "Enter 1) Last Name\n2) First Name and Last Name\n";
cin >> type;
if( type == 1)
{
    // last name only
    cout << "Enter Last Name\n";
    cin >> lastname;
    // now search the array for last name only
}
else if( type == 2)
{
    cout << "Enter First Name\n";
    cin >> firstname;
    cout << "Enter Last Name\n";
    cin >> lastname;
   // now search the array for both first name and last name
}
else
{
    // display error message
}

ok so i worked on it but its not exactly coming out like i hoped.

my loop is off and i cant see why, and im getting weird signs. the thing is im supposed to keep asking the user if he wants to find the initial input info, based on an input off person's last name or first name and last name until he decides to exit.
plz help...
thnx

#include <iostream>

const int maxpeople = 50;

struct person
{
	char firstname[20];

	char lastname[20];

	char address[20];
};

void addpeople(person people[], int size);

void getperson(person people[maxpeople], int size);

using namespace std;

person people[10];

int main()
{
	addpeople(people, 10);

	while(true)
	{
	
	getperson(people, 10);

	}

	return 0;
}

void addpeople(person people[maxpeople], int size)
{
	for(int i = 0;i < size; i++)
	{

		cout<<"Enter your first name, last name and address respectively"<<endl;

		cin>>people[i].firstname>>people[i].lastname>>people[i].address;

	}
}


void getperson(person people[maxpeople], int size)
{	
	char type;
	
	char firstname[20] = {0};
	
	char lastname[20] = {0};

	while(true)
	{
	cout << "Enter 1) to find the person by their Last Name\n OR\n 2) to find the person by their First Name and Last Name\n";
	cin >> type;
	
	if( type == '1')
	{
		
		cout << "Enter Last Name\n";
		cin >> lastname;
		
			for(int i = 0; ;i++)
			{

			if(people[i].lastname == lastname)
		{
			cout<<"The person's first name is"<<people[i].firstname<<"\n The persons last name is "<<people[i].lastname<<"\n The person's address is "<<people[i].address;
		}
	
			}
	}
	
	else if( type == '2')
	{
		cout << "Enter First Name\n";
		cin >> firstname;
		cout << "Enter Last Name\n";
		cin >> lastname;
		
		for(int i = 0; ;i++)
		{
		
			if(people[i].lastname == lastname && people[i].firstname == firstname )
		{
			cout<<"The person's first name is"<<people[i].firstname<<"\n The persons last name is "<<people[i].lastname<<"\n The person's address is "<<people[i].address;
		}

	
		}

	}
	
	else if(type == '1' && type == '2')
	{
		while (type == 1 && type == 2)
		{
		cout << "Enter 1) to find the person by their Last Name\n OR\n 2) to find the person by their First Name and Last Name\n";
		cin >> type;
		}
	}

	else if{type = 'exit')
		break;

	}

}

else if{type = 'exit')

First, there's some syntax error, but that's different story.
type is char variable. How can it store 'exit'? Besides, what is 'exit'? (answer: nothing)

Try to change it like, if user types 'x', or 'e'... or simply else (not 1 or 2)

yeah..sorry about that..just realized it myself..so whats that command to make it jump to the press any key to continue part??

#include <iostream>

const int maxpeople = 50;

struct person
{
	char firstname[20];

	char lastname[20];

	char address[20];
};

void addpeople(person people[], int size);

void getperson(person people[maxpeople], int size);

using namespace std;

person people[10];

int main()
{
	addpeople(people, 10);

	while(true)
	{
	
	getperson(people, 10);

	}

	return 0;
}

void addpeople(person people[maxpeople], int size)
{
	for(int i = 0;i < size; i++)
	{

		cout<<"Enter your first name, last name and address respectively"<<endl;

		cin>>people[i].firstname>>people[i].lastname>>people[i].address;

	}
}


void getperson(person people[maxpeople], int size)
{	
	char type;
	
	char firstname[20] = {0};
	
	char lastname[20] = {0};


	
	cout << "Enter 1) to find the person by their Last Name\n OR\n 2) to find the person by their First Name and Last Name\n";
	cin >> type;
	
	if( type == '1')
	{
		
		cout << "Enter Last Name\n";
		cin >> lastname;
		
			for(int i = 0; ;i++)
			{

			if(people[i].lastname == lastname)
		{
			cout<<"The person's first name is"<<people[i].firstname<<"\n The persons last name is "<<people[i].lastname<<"\n The person's address is "<<people[i].address;
		}
	
			}
	}
	
	else if( type == '2')
	{
		cout << "Enter First Name\n";
		cin >> firstname;
		cout << "Enter Last Name\n";
		cin >> lastname;
		
		for(int i = 0; ;i++)
		{
		
			if(people[i].lastname == lastname && people[i].firstname == firstname )
		{
			cout<<"The person's first name is"<<people[i].firstname<<"\n The persons last name is "<<people[i].lastname<<"\n The person's address is "<<people[i].address;
		}

	
		}

	}
	
	else if(type == '1' && type == '2')
	{
		while (type == 1 && type == 2)
		{
		cout << "Enter 1) to find the person by their Last Name\n OR\n 2) to find the person by their First Name and Last Name\n";
		cin >> type;
		}
	}

	else if(type == 'exit')
	

	

}

why dont these 'for(int i = 0; ;i++)' loops have any comparison condition? when will it stop? I think before you complete this assignment you need to understand loops and arrays once more.. dont even try to solve it before you re-read those topics, ur basics are not correct and your just trying to somehow get the output without understanding what your doing here .. and you might just make yourself learn something wrong ... hit the books and then attempt it again... all the best

ok..i think i figured that out

for(int i = 0;i < size ;i++)

but now jst asks the last name or first name question when i type in a number?

You are using C style strings for the names, which is fine, but you can't compare C style strings using the == operator. You can compare stl string objects using the == operator. To compare C style strings for equality you need to use strcmp() and evaluate the return value. If the two strings are the same the return value should be zero.
----------------------------------------------
I'd tell the user how to exit the loop explicitly.

cout << "enter the appropriate number to select the described option" << endl;
cout << "1) search by last name only" << endl;
cout << "2) search by first and last name" << endl;
cout << "3) stop searching" << endl;
char type;
cin >> type;

I would declare type as an int rather than a char, too, but that's personal choice.
-----------------------------------------
else if(type == '1' && type == '2')

How can type be both '1' and '2'? Given the context I'd use != instead of ==.

There is another problem with your code if(people[i].lastname == lastname) You cannot compare two char arrays with the == operator. You either need to define them as "string" or you need to use strcmp() to compare them.

>>if(people.lastname == lastname)
You can't compare two character arrays with the == operator like you do two std::strings. To compare character arrays call strcmp() functions. if( strcmp(people[i].lastname == lastname) == 0)

commented: yup, I just realized that myself +2

alrite..so hopefully this should be my last problem...based on my recent understanding i had to create a menu to ask the user what he wanted to do at any point in time (add person or get person) and i have to do this untill either the address book is filled with 10 people or the user enters 2..so the problem now is trying to to add the people to my 10 slots

#include <iostream>

const int maxpeople = 50;

struct person
{
	char firstname[20];

	char lastname[20];

	char address[20];
};

void addpeople(person people[10]);

void getperson(person people[maxpeople], int size);

using namespace std;

person people[10];

int main()
{
	int userinput;

	int userinputcounter = 0;

	while(true)
	{
		cout<<"Do you want to:\n 1) add a person \n 2) find a person"<<endl<<"Enter 1 or 2 respectively:"<<endl;

		cin>>userinput;

		userinputcounter++;

		if(userinput == 1 && userinputcounter <10)
		{
			addpeople(people);

			cout<<endl;

		}

		else if(userinput == 2)
		{
			getperson(people, 10);
		}
		else if(userinput == 2 && userinput > 10)
		{
			getperson(people, 10);
		}

		else
		{
			cout<<"Do you want to:\n 1) add a person \n 2) find a person"<<endl<<"Enter 1 or 2 respectively"<<endl;

			cin>>userinput;
		}

	}
	return 0;
}

void addpeople(person people[maxpeople], int size)
{
	cout<<"Enter your first name, last name and address respectively:"<<endl;

	cout<<endl;

	int i = 0;

	while(i < size)
	{

		cin>>people[i].firstname>>people[i].lastname>>people[i].address;

		i++;

		cout<<endl;
	}
}


void getperson(person people[maxpeople], int size)
{	
	char type;

	char firstname[20] = {0};

	char lastname[20] = {0};



	cout << "Enter\n 1) to find the person by their last name\n OR\n 2) to find the person by their first name and last name\n";
	cin >> type;

	cout<<endl;

	if( type == '1')
	{

		cout << "Enter last name\n";
		cin >> lastname;

		for(int i = 0;i < size ;i++)
		{

			if(strcmp(people[i].lastname, lastname) == 0)
			{
				cout<<"The person's first name is "<<people[i].firstname<<"\n The persons last name is "<<people[i].lastname<<"\n The person's address is "<<people[i].address;

				cout<<endl;
			}

			else if(strcmp(people[i].lastname, lastname) != 0)
			{
				cout<<"There is no such person in the address book."<<endl;

				cout<<endl;
			}

		}
	}

	else if( type == '2')
	{
		cout << "Enter first name\n";
		cin >> firstname;
		cout << "Enter last name\n";
		cin >> lastname;

		for(int i = 0;i <size ;i++)
		{

			if( strcmp(people[i].firstname, firstname) == 0 && strcmp(people[i].lastname, lastname) == 0)
			{
				cout<<"The person's first name is "<<people[i].firstname<<"\n The persons last name is "<<people[i].lastname<<"\n The person's address is "<<people[i].address;

				cout<<endl;
			}

			else if( strcmp(people[i].firstname, firstname) != 0 && strcmp(people[i].lastname, lastname) != 0)
			{
				cout<<"There is no such person in the address book."<<endl;

				cout<<endl;
			}
		}

	}

	else if(type != '1' && type != '2')
	{
		while (type == 1 && type == 2)
		{
			cout << "Enter\n 1) to find the person by their last name\n OR\n 2) to find the person by their First Name and Last Name\n";
			cin >> type;

			cout<<endl;
		}
	}

}

You probably want to write something like this. I am assuming here that you only want to enter the information for 1 person at a time, and you want to exit once you have called getperson. If you want to call getperson mutliple times, you will need another option to specify your exit. If you want to add mutiple users at the same time then you'll need to pass in the current index you are at and the number of users you want to add and accordingly increment userinputcounter.

int main()
{
	int userinput;
	int userinputcounter = 0;
  int done = false; 

	while(!done)
	{
		cout<<"Do you want to:  \n" 
        <<"1) add a person  \n"
        <<"2) find a person \n"
        <<"Enter 1 or 2 or 3 respectively:"<<endl;
		cin>>userinput;

    switch(userinput){
      case 1:
        if (userinputcounter < 10){
          addpeople(people,userinputcounter);
          userinputcounter++; 
          break;
        }
      case 2:
        getperson(people,10);
        done = true; 
        break;
      default:
        cout << "Incorrect option " << endl;
    }//switch ends 
	}
	return 0;
}

void addpeople(person people[maxpeople], int index)
{

  cout<<"Enter your first name, last name and address respectively:"<<endl;
  cin>>people[index].firstname>>people[index].lastname>>people[index].address;
}

yeah i do want to add the info 1 at a time..but lets say the user adds a person and gets a person..he should still be able to add or get once hes done any of the 2...ill deal with the exit option later

thnx 4 ur help..really appreciate..but im gona stick with my version of main since our mains dont make much of a difference in terms of functionality..

only problem now is when it tries to get a person it displays the person does not exist even if it does...and if a person actually does not exist it shows it increasingly with each try

#include <iostream>

const int maxpeople = 50;

struct person
{
	char firstname[20];

	char lastname[20];

	char address[20];
};

void addpeople(person people[maxpeople], int size);

void getperson(person people[maxpeople], int size);

using namespace std;

person people[10];

int main()
{
	int userinput;

	int userinputcounter = 0;

	while(true)
	{
		cout<<"Do you want to:\n 1) add a person \n 2) find a person"<<endl<<"Enter 1 or 2 respectively:"<<endl;

		cin>>userinput;

		userinputcounter++;

		if(userinput == 1 && userinputcounter <10)
		{
			addpeople(people, userinputcounter);

			cout<<endl;

		}

		else if(userinput == 2)
		{
			getperson(people, userinputcounter);
		}
		
		else if(userinput == 2 && userinput > 10)
		{
			cout<<"The address book now has more than 10 people so you can only get a person"<<endl;

			getperson(people, userinputcounter);
		}

		else
		{
			cout<<"Do you want to:\n 1) add a person \n 2) find a person"<<endl<<"Enter 1 or 2 respectively"<<endl;

			cin>>userinput;
		}

	}
	return 0;
}

void addpeople(person people[maxpeople], int size)
{
	cout<<"Enter your first name, last name and address respectively:"<<endl;

	cout<<endl;

		cin>>people[size].firstname>>people[size].lastname>>people[size].address;

		cout<<endl;
}


void getperson(person people[maxpeople], int size)
{	
	int type;

	char firstname[20] = {0};

	char lastname[20] = {0};



	cout << "Enter\n 1) to find the person by their last name\n OR\n 2) to find the person by their first name and last name\n";
	cin >> type;

	cout<<endl;

	if( type == 1)
	{

		cout << "Enter last name\n";
		cin >> lastname;

		for(int i = 0;i < size ;i++)
		{

			if(strcmp(people[i].lastname, lastname) == 0)
			{
				cout<<"The person's first name is "<<people[i].firstname<<"\n The person's last name is "<<people[i].lastname<<"\n The person's address is "<<people[i].address;

				cout<<endl;
			}

			else if(strcmp(people[i].lastname, lastname) != 0)
			{
				cout<<"There is no such person in the address book."<<endl;

				cout<<endl;
			}

		}
	}

	else if( type == 2)
	{
		cout << "Enter first name\n";
		
		cin >> firstname;
		
		cout << "Enter last name\n";
		
		cin >> lastname;

		for(int i = 0; i <size ;i++)
		{

			if(strcmp(people[i].firstname, firstname) == 0 && strcmp(people[i].lastname, lastname) == 0)
			{
				cout<<"The person's first name is "<<people[i].firstname<<"\n The person's last name is "<<people[i].lastname<<"\n The person's address is "<<people[i].address;

				cout<<endl;
			}

			else if(strcmp(people[i].firstname, firstname) != 0 && strcmp(people[i].lastname, lastname) != 0)
			{
				cout<<"There is no such person in the address book."<<endl;

				cout<<endl;
			}
		}

	}

	else if(type != 1 && type != 2)
	{
		while (type == 1 && type == 2)
		{
			cout << "Enter\n 1) to find the person by their last name\n 2) to find the person by their first name and last name\n";
			cin >> type;

			cout<<endl;
		}
	}

}

Well you have to figure out where to stop, else you will stay in an infinite loop. You could add an option 3 for exit, and when that is entered, set done to true. Else keep looping. It will keep adding upto 10 names and then keep calling getname until you exit.

well thats the idea...im supposed to keep getting names until the user wishes to exit... and ive added that now

#include <iostream>

const int maxpeople = 50;

struct person
{
	char firstname[20];

	char lastname[20];

	char address[20];
};

void addpeople(person people[maxpeople], int size);

void getperson(person people[maxpeople], int size);

using namespace std;

person people[10];

int main()
{
	int userinput;

	int userinputcounter = 0;

	while(true)
	{
		cout<<"Do you want to:\n 1) add a person \n 2) find a person \n 3) exit"<<endl<<"Enter 1 or 2 or 3 respectively:"<<endl;

		cin>>userinput;

		userinputcounter++;

		if(userinput == 1 && userinputcounter <10)
		{
			addpeople(people, userinputcounter);

			cout<<endl;

		}

		else if(userinput == 2)
		{
			getperson(people, userinputcounter);
		}
		
		else if(userinput == 2 && userinput > 10)
		{
			cout<<"The address book now has more than 10 people so you can only get a person"<<endl;

			getperson(people, userinputcounter);
		}

		else if(userinput == 3)
		{
			break;
		}

		else
		{
			cout<<"Do you want to:\n 1) add a person \n 2) find a person"<<endl<<"Enter 1 or 2 respectively"<<endl;

			cin>>userinput;
		}

	}
	return 0;
}

void addpeople(person people[maxpeople], int size)
{
	cout<<"Enter your first name, last name and address respectively:"<<endl;

	cout<<endl;

		cin>>people[size].firstname>>people[size].lastname>>people[size].address;

		cout<<endl;
}


void getperson(person people[maxpeople], int size)
{	
	int type;

	char firstname[20] = {0};

	char lastname[20] = {0};



	cout << "Enter\n 1) to find the person by their last name\n OR\n 2) to find the person by their first name and last name\n";
	cin >> type;

	cout<<endl;

	if( type == 1)
	{

		cout << "Enter last name\n";
		cin >> lastname;

		for(int i = 0;i < size ;i++)
		{

			if(strcmp(people[i].lastname, lastname) == 0)
			{
				cout<<"The person's first name is "<<people[i].firstname<<"\n The person's last name is "<<people[i].lastname<<"\n The person's address is "<<people[i].address;

				cout<<endl;
			}

			else if(strcmp(people[i].lastname, lastname) != 0)
			{
				cout<<"There is no such person in the address book."<<endl;

				cout<<endl;
			}

		}
	}

	else if( type == 2)
	{
		cout << "Enter first name\n";
		
		cin >> firstname;
		
		cout << "Enter last name\n";
		
		cin >> lastname;

		for(int i = 0; i <size ; i++)
		{

			if(strcmp(people[i].firstname, firstname) == 0 && strcmp(people[i].lastname, lastname) == 0)
			{
				cout<<"The person's first name is "<<people[i].firstname<<"\n The person's last name is "<<people[i].lastname<<"\n The person's address is "<<people[i].address;

				cout<<endl;
			}

			else if(strcmp(people[i].firstname, firstname) != 0 && strcmp(people[i].lastname, lastname) != 0)
			{
				cout<<"There is no such person in the address book."<<endl;

				cout<<endl;
			}
		}

	}

	else if(type != 1 && type != 2)
	{
		while (type == 1 && type == 2)
		{
			cout << "Enter\n 1) to find the person by their last name\n 2) to find the person by their first name and last name\n";
			cin >> type;

			cout<<endl;
		}
	}

}

it seems like everytime i fix my previous error i end up with sumthin new..anywayz now once my addressbookfull is past 10 i cant see to run that else if...

#include <iostream>

const int maxpeople = 50;

struct person
{
	char firstname[20];

	char lastname[20];

	char address[20];
};

void addpeople(person people[maxpeople], int size);

void getperson(person people[maxpeople], int size);

using namespace std;

person people[10];

int main()
{
	int userinput;

	int userinputcounter = 0;

	int addressbookfull = 0;
	
	while(true)
	{
		cout<<"Do you want to:\n 1) add a person \n 2) find a person \n 3) exit"<<endl<<"Enter 1 or 2 or 3 respectively:"<<endl;

		cin>>userinput;

		int addressbookfull = userinputcounter;

		if(userinput == 1 && userinputcounter < 10)
		{
			addpeople(people, userinputcounter);

			cout<<endl;

			userinputcounter++;
		}

		else if(addressbookfull > 10)
		{
			cout<<"The address book now has more than 10 people so you can only get a person"<<endl;

			getperson(people, userinputcounter);
		}

		else if(userinput == 2)
		{
			getperson(people, userinputcounter);
		}

		else if(userinput == 3)
		{
			break;
		}

		else
		{
			cout<<"Do you want to:\n 1) add a person \n 2) find a person \n 3) exit"<<endl<<"Enter 1 or 2 or 3 respectively:"<<endl;

			cin>>userinput;
		}

	}
	return 0;
}

void addpeople(person people[maxpeople], int size)
{
	cout<<"Enter your first name, last name and address respectively:"<<endl;

	cout<<endl;

		cin>>people[size].firstname>>people[size].lastname>>people[size].address;

		cout<<endl;
}


void getperson(person people[maxpeople], int size)
{	
	int type;

	char firstname[20] = {0};

	char lastname[20] = {0};



	cout << "Enter\n 1) to find the person by their last name\n OR\n 2) to find the person by their first name and last name\n";
	cin >> type;

	cout<<endl;

	if(type == 1)
	{
		cout << "Enter last name\n";
		
		cin >> lastname;

		for(int i = 0;i < size ;i++)
		{
			
			 if(strcmp(people[i].lastname, lastname) == 0)
			{
				cout<<"The person's first name is "<<people[i].firstname<<"\nThe person's last name is "<<people[i].lastname<<"\nThe person's address is "<<people[i].address;

				cout<<endl;
			}	
		
			 else if(strcmp(people[i].lastname, lastname) != 0)
			{
			cout<<"There is no such person in the address book."<<endl;

			cout<<endl;
			}

		}
	}

	else if( type == 2)
	{
		cout << "Enter first name\n";
		
		cin >> firstname;
		
		cout << "Enter last name\n";
		
		cin >> lastname;

		for(int i = 0; i <size ; i++)
		{

			if(strcmp(people[i].firstname, firstname) == 0 && strcmp(people[i].lastname, lastname) == 0)
			{
				cout<<"The person's first name is "<<people[i].firstname<<"\nThe person's last name is "<<people[i].lastname<<"\nThe person's address is "<<people[i].address;

				cout<<endl;
			}
			
		
			
			else if(strcmp(people[i].firstname, firstname) == 0 && strcmp(people[i].lastname, lastname) != 0)
			{
				cout<<"There is no such person in the address book."<<endl;
				
				cout<<endl;
			}
		
		
		
		}

	}

	else if(type != 1 && type != 2)
	{
		while (type == 1 && type == 2)
		{
			cout << "Enter\n 1) to find the person by their last name\n 2) to find the person by their first name and last name\n";
			cin >> type;

			cout<<endl;
		}
	}

}

A block such as this will get your job done.

else if(addressbookfull > 10 || userinput == 2)
    {
      if (addressbookfull > 10 && userinput == 1){
        cout<<"The address book now has more than 10 people so you can only get a person"<<endl;
      }
      getperson(people, userinputcounter);
    }

Also instead of doing the

while (true){

.... break;
}

do something like

bool done = false;
while (!done){

.. done = true;
}

yeah that block is pretty much what i had but in 2 different else ifs..even still yours doesnt work

A block such as this will get your job done.

else if(addressbookfull > 10 || userinput == 2)
    {
      if (addressbookfull > 10 && userinput == 1){
        cout<<"The address book now has more than 10 people so you can only get a person"<<endl;
      }
      getperson(people, userinputcounter);
    }

i have a final problem when i call on the get person func and the person does not exist...it does not display the cout..can anyone show me why??

#include <iostream>

#include <cstring>

const int maxpeople = 50;

int personumber;

int personumberstorer=0;

struct person
{
	char firstname[20];

	char lastname[20];

	char address[20];
};

void addpeople(person people[maxpeople], int size);

void findperson(person people[maxpeople], int size);

void getperson(person people[maxpeople], int size);

using namespace std;

person people[10];

int main()
{
	int userinput;

	int userinputcounter = 0;

	int addressbookfull = 0;
	
	while(true)
	{
		cout<<"Do you want to:\n 1) add a person \n 2) find a person \n 3) get a person \n 4) exit"<<endl<<"Enter 1, 2, 3 or 4 respectively:"<<endl;

		cin>>userinput;

		int addressbookfull = userinputcounter;

		if(userinput == 1 && userinputcounter < 10)
		{
			addpeople(people, userinputcounter);

			cout<<endl;

			userinputcounter++;
		}

		else if(addressbookfull >= 10 && userinput == 1)
		{
        cout<<"The address book now has more than 10 people so you can only find or get a person"<<endl;
		
		continue;
		
		}

		else if(userinput == 2)
		{

		findperson(people, userinputcounter);
		
		}

		else if(userinput == 3)
		{

		getperson(people, 10);
		
		}


		else if(userinput == 4)
		{
			break;
		}

		else
		{
			cout<<"Do you want to:\n 1) add a person \n 2) find a person \n 3) get a person \n 4) exit"<<endl<<"Enter 1, 2, 3 or 4 respectively:"<<endl;

			cin>>userinput;
		}

	}
	return 0;
}

void addpeople(person people[maxpeople], int size)
{
	cout<<"Enter your first name, last name and address respectively:"<<endl;

	cout<<endl;

		cin>>people[size].firstname>>people[size].lastname>>people[size].address;

		cout<<endl;
}


void findperson(person people[maxpeople], int size)
{	
	int type;

	char firstname[20] = {0};

	char lastname[20] = {0};



	cout << "Enter\n 1) to find the person by their last name\n OR\n 2) to find the person by their first name and last name\n";
	cin >> type;

	cout<<endl;

	if(type == 1)
	{
		cout << "Enter last name\n";
		
		cin >> lastname;

		for(int i = 0;i < size ;i++)
		{
			
			 if(strcmp(people[i].lastname, lastname) == 0)
			{
				cout<<"The person's first name is "<<people[i].firstname<<"\nThe person's last name is "<<people[i].lastname<<"\nThe person's address is "<<people[i].address;

				cout<<endl;
			}	
		
			 else if(strcmp(people[i].lastname, lastname) != 0)
			{
			cout<<"There is no such person in the address book."<<endl;

			cout<<endl;
			}

		}
	}

	else if(type == 2)
	{
		cout << "Enter first name\n";
		
		cin >> firstname;
		
		cout << "Enter last name\n";
		
		cin >> lastname;

		for(int i = 0; i <size ; i++)
		{

			if(strcmp(people[i].firstname, firstname) == 0 && strcmp(people[i].lastname, lastname) == 0)
			{
				cout<<"The person's first name is "<<people[i].firstname<<"\nThe person's last name is "<<people[i].lastname<<"\nThe person's address is "<<people[i].address;

				cout<<endl;
			}
			
		
			
			else if(strcmp(people[i].firstname, firstname) != 0 && strcmp(people[i].lastname, lastname) != 0)
			{
				cout<<"There is no such person in the address book."<<endl;
				
				cout<<endl;
			}
		
		
		
		}

	}

	else if(type != 1 && type != 2)
	{
		while (type == 1 && type == 2)
		{
			cout << "Enter\n 1) to find the person by their last name\n 2) to find the person by their first name and last name\n";
			cin >> type;

			cout<<endl;
		}
	}

}

void getperson(person people[maxpeople], int size)
{

	 personumber= personumberstorer;

	 if(people[personumber].firstname != 0  && people[personumber].lastname != 0 && people[personumber].address != 0)
	 {
		cout<<"There is no one to get from the address book"<<endl;		
	 //also how can i make it return to main to reiterate my loop there?? (instead of break which i cant use)
	 
		
	 }
	while(true)
	{		
		 if(personumber ==10)
		{
			personumberstorer =0;
		
			personumber= personumberstorer;

			continue;
		 }
		

		for(; personumberstorer < size;)
		{
				cout<<"The person's first name is "<<people[personumber].firstname<<"\nThe person's last name is "<<people[personumber].lastname<<"\nThe person's address is "<<people[personumber].address;

				cout<<endl;	
				
				personumberstorer++;
				
				break;
		}

		break;

	}

}

Send the number of actual person objects stored in people at any given time to getperson(), not the maximum number of person objects that can be stored in people. Then check the number of person objects stored in people to determine if people is empty. Don't try to evaluate the value of some indexes firstname, etc to determine if people is empty. If this:

people[personumber].firstname != 0

is true that means there is probably a person object in people, but maybe not.

When you declare people to be an array with enough memory to hold 10 person objects, there are no person objects in there just because you declare the memory for the objects.

Likewise, when you declare a person object, the firstname variable isn't given the value of NULL by default. firstname doesn't have any value until it is assigned one, whether it be NULL or Jane or Bill or whatever. Same for lastname and address.

im not too sure u follow what im supposed to do with my getperson func or maybe i dint follow wht u said..i have to get a person if the user inputs 3 and i start at people element 0..now if people element 0 is empty i have to output that if statement but its not working..knowing that my address book can only have 10 people i think im just gona pass 10..but i know there is probably some error with my condition in the if statement..so how can i rectify that setting a condition if that people index is 0
thnx

Well your getperson() passes in the size, which I assume is the total number of elements you have in the address book ?

So if that is 0, then all you need to do is print your statement saying that there are no entries in your book, and then return to the main function.

if (size == 0){
  cout << " The address book is empty " << endl;
  return;
}

nope..see the user runs the program and at any point in the prog he inputs 3 which then calls on the getperson function..when it is initiated it starts from element 0..next time it gets called element 1 all the way upto 9 and then gets back to zero this is the way its supposed to work..now i jst wana setup a condition in which if that element has still not been filled say it is empty in that element..so how do i design my condition inside the if ??

#include <iostream>

#include <cstring>

const int maxpeople = 50;

int personumber;

int personumberstorer=0;

struct person
{
	char firstname[20];

	char lastname[20];

	char address[20];
};

void addpeople(person people[maxpeople], int size);

void findperson(person people[maxpeople], int size);

void getperson(person people[maxpeople], int size);

using namespace std;

person people[10];

int main()
{
	int userinput;

	int userinputcounter = 0;

	int addressbookfull = 0;
	
	while(true)
	{
		cout<<"Do you want to:\n 1) add a person \n 2) find a person \n 3) get a person \n 4) exit"<<endl<<"Enter 1, 2, 3 or 4 respectively:"<<endl;

		cin>>userinput;

		int addressbookfull = userinputcounter;

		if(userinput == 1 && userinputcounter < 10)
		{
			addpeople(people, userinputcounter);

			cout<<endl;

			userinputcounter++;
		}

		else if(addressbookfull >= 10 && userinput == 1)
		{
        cout<<"The address book now has more than 10 people so you can only find or get a person"<<endl;
		
		continue;
		
		}

		else if(userinput == 2)
		{

		findperson(people, userinputcounter);
		
		}

		else if(userinput == 3)
		{

		getperson(people, 10);
		
		}


		else if(userinput == 4)
		{
			break;
		}

		else
		{
			cout<<"Do you want to:\n 1) add a person \n 2) find a person \n 3) get a person \n 4) exit"<<endl<<"Enter 1, 2, 3 or 4 respectively:"<<endl;

			cin>>userinput;
		}

	}
	return 0;
}

void addpeople(person people[maxpeople], int size)
{
	cout<<"Enter your first name, last name and address respectively:"<<endl;

	cout<<endl;

		cin>>people[size].firstname>>people[size].lastname>>people[size].address;

		cout<<endl;
}


void findperson(person people[maxpeople], int size)
{	
	int type;

	char firstname[20] = {0};

	char lastname[20] = {0};



	cout << "Enter\n 1) to find the person by their last name\n OR\n 2) to find the person by their first name and last name\n";
	cin >> type;

	cout<<endl;

	if(type == 1)
	{
		cout << "Enter last name\n";
		
		cin >> lastname;

		for(int i = 0;i < size ;i++)
		{
			
			 if(strcmp(people[i].lastname, lastname) == 0)
			{
				cout<<"The person's first name is "<<people[i].firstname<<"\nThe person's last name is "<<people[i].lastname<<"\nThe person's address is "<<people[i].address;

				cout<<endl;
			}	
		
			 else if(strcmp(people[i].lastname, lastname) != 0)
			{
			cout<<"There is no such person in the address book."<<endl;

			cout<<endl;
			}

		}
	}

	else if(type == 2)
	{
		cout << "Enter first name\n";
		
		cin >> firstname;
		
		cout << "Enter last name\n";
		
		cin >> lastname;

		for(int i = 0; i <size ; i++)
		{

			if(strcmp(people[i].firstname, firstname) == 0 && strcmp(people[i].lastname, lastname) == 0)
			{
				cout<<"The person's first name is "<<people[i].firstname<<"\nThe person's last name is "<<people[i].lastname<<"\nThe person's address is "<<people[i].address;

				cout<<endl;
			}
			
		
			
			else if(strcmp(people[i].firstname, firstname) != 0 && strcmp(people[i].lastname, lastname) != 0)
			{
				cout<<"There is no such person in the address book."<<endl;
				
				cout<<endl;
			}
		
		
		
		}

	}

	else if(type != 1 && type != 2)
	{
		while (type == 1 && type == 2)
		{
			cout << "Enter\n 1) to find the person by their last name\n 2) to find the person by their first name and last name\n";
			cin >> type;

			cout<<endl;
		}
	}

}

void getperson(person people[maxpeople], int size)
{

	 personumber= personumberstorer;

	 [B]if(people[personumber].firstname != 0  && people[personumber].lastname != 0 && people[personumber].address != 0)
	 {
		cout<<"There is no one to get from the address book"<<endl;		
		
		return;
	 }[/B]
	while(true)
	{		
		 if(personumber ==10)
		{
			personumberstorer =0;
		
			personumber= personumberstorer;

			continue;
		 }
		

		for(; personumberstorer < size;)
		{
				cout<<"The person's first name is "<<people[personumber].firstname<<"\nThe person's last name is "<<people[personumber].lastname<<"\nThe person's address is "<<people[personumber].address;

				cout<<endl;	
				
				personumberstorer++;
				
				break;
		}

		break;

	}

}

Well you can add a flag to your structure that gets set for that particular element when its populated.

unfortunately..i dont know what that is..isnt there an easier way to do this?? i mean the only problem is just coming up with the right condition, which i think is setup right but cant think of how it sees the array when there is nothing in there

what is inside that array element if it does not contain anything?? if it is a zero, null or sumthin how can i use that as my condition??

thnx

A flag means a variable like this

struct person
{
    char firstname[20];
    char lastname[20];
    char address[20];
        bool isSet;
};

You can initialize the array to have all of the strings set to " " and isSet to "false". Then as you populate the elements you can set isSet to "true".

If you don't initialize the array elements, you will probably have gibberish in there. You need to manually initialize them before you begin processing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.