address book

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 63
Reputation: unk45 is an unknown quantity at this point 
Solved Threads: 0
unk45 unk45 is offline Offline
Junior Poster in Training

address book

 
0
  #1
Oct 3rd, 2008
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];

	for (int i = 0; i < stringsize; i++)
    {
		firstname[i] = holdspeople[i].firstname;
		
		lastname[i] = holdspeople[i].lastname;
    }
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: address book

 
0
  #2
Oct 3rd, 2008
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:
  1. ...
  2. int type;
  3. char firstname[20] = {0};
  4. char lastname[20] = {0};
  5.  
  6. cout << "Enter 1) Last Name\n2) First Name and Last Name\n";
  7. cin >> type;
  8. if( type == 1)
  9. {
  10. // last name only
  11. cout << "Enter Last Name\n";
  12. cin >> lastname;
  13. // now search the array for last name only
  14. }
  15. else if( type == 2)
  16. {
  17. cout << "Enter First Name\n";
  18. cin >> firstname;
  19. cout << "Enter Last Name\n";
  20. cin >> lastname;
  21. // now search the array for both first name and last name
  22. }
  23. else
  24. {
  25. // display error message
  26. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 63
Reputation: unk45 is an unknown quantity at this point 
Solved Threads: 0
unk45 unk45 is offline Offline
Junior Poster in Training

Re: address book

 
0
  #3
Oct 3rd, 2008
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
  1. #include <iostream>
  2.  
  3. const int maxpeople = 50;
  4.  
  5. struct person
  6. {
  7. char firstname[20];
  8.  
  9. char lastname[20];
  10.  
  11. char address[20];
  12. };
  13.  
  14. void addpeople(person people[], int size);
  15.  
  16. void getperson(person people[maxpeople], int size);
  17.  
  18. using namespace std;
  19.  
  20. person people[10];
  21.  
  22. int main()
  23. {
  24. addpeople(people, 10);
  25.  
  26. while(true)
  27. {
  28.  
  29. getperson(people, 10);
  30.  
  31. }
  32.  
  33. return 0;
  34. }
  35.  
  36. void addpeople(person people[maxpeople], int size)
  37. {
  38. for(int i = 0;i < size; i++)
  39. {
  40.  
  41. cout<<"Enter your first name, last name and address respectively"<<endl;
  42.  
  43. cin>>people[i].firstname>>people[i].lastname>>people[i].address;
  44.  
  45. }
  46. }
  47.  
  48.  
  49. void getperson(person people[maxpeople], int size)
  50. {
  51. char type;
  52.  
  53. char firstname[20] = {0};
  54.  
  55. char lastname[20] = {0};
  56.  
  57. while(true)
  58. {
  59. 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";
  60. cin >> type;
  61.  
  62. if( type == '1')
  63. {
  64.  
  65. cout << "Enter Last Name\n";
  66. cin >> lastname;
  67.  
  68. for(int i = 0; ;i++)
  69. {
  70.  
  71. if(people[i].lastname == lastname)
  72. {
  73. 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;
  74. }
  75.  
  76. }
  77. }
  78.  
  79. else if( type == '2')
  80. {
  81. cout << "Enter First Name\n";
  82. cin >> firstname;
  83. cout << "Enter Last Name\n";
  84. cin >> lastname;
  85.  
  86. for(int i = 0; ;i++)
  87. {
  88.  
  89. if(people[i].lastname == lastname && people[i].firstname == firstname )
  90. {
  91. 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;
  92. }
  93.  
  94.  
  95. }
  96.  
  97. }
  98.  
  99. else if(type == '1' && type == '2')
  100. {
  101. while (type == 1 && type == 2)
  102. {
  103. 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";
  104. cin >> type;
  105. }
  106. }
  107.  
  108. else if{type = 'exit')
  109. break;
  110.  
  111. }
  112.  
  113. }
Last edited by unk45; Oct 3rd, 2008 at 2:28 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: address book

 
0
  #4
Oct 3rd, 2008
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)
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 63
Reputation: unk45 is an unknown quantity at this point 
Solved Threads: 0
unk45 unk45 is offline Offline
Junior Poster in Training

Re: address book

 
0
  #5
Oct 3rd, 2008
yeah..sorry about that..just realized it myself..so whats that command to make it jump to the press any key to continue part??
  1. #include <iostream>
  2.  
  3. const int maxpeople = 50;
  4.  
  5. struct person
  6. {
  7. char firstname[20];
  8.  
  9. char lastname[20];
  10.  
  11. char address[20];
  12. };
  13.  
  14. void addpeople(person people[], int size);
  15.  
  16. void getperson(person people[maxpeople], int size);
  17.  
  18. using namespace std;
  19.  
  20. person people[10];
  21.  
  22. int main()
  23. {
  24. addpeople(people, 10);
  25.  
  26. while(true)
  27. {
  28.  
  29. getperson(people, 10);
  30.  
  31. }
  32.  
  33. return 0;
  34. }
  35.  
  36. void addpeople(person people[maxpeople], int size)
  37. {
  38. for(int i = 0;i < size; i++)
  39. {
  40.  
  41. cout<<"Enter your first name, last name and address respectively"<<endl;
  42.  
  43. cin>>people[i].firstname>>people[i].lastname>>people[i].address;
  44.  
  45. }
  46. }
  47.  
  48.  
  49. void getperson(person people[maxpeople], int size)
  50. {
  51. char type;
  52.  
  53. char firstname[20] = {0};
  54.  
  55. char lastname[20] = {0};
  56.  
  57.  
  58.  
  59. 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";
  60. cin >> type;
  61.  
  62. if( type == '1')
  63. {
  64.  
  65. cout << "Enter Last Name\n";
  66. cin >> lastname;
  67.  
  68. for(int i = 0; ;i++)
  69. {
  70.  
  71. if(people[i].lastname == lastname)
  72. {
  73. 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;
  74. }
  75.  
  76. }
  77. }
  78.  
  79. else if( type == '2')
  80. {
  81. cout << "Enter First Name\n";
  82. cin >> firstname;
  83. cout << "Enter Last Name\n";
  84. cin >> lastname;
  85.  
  86. for(int i = 0; ;i++)
  87. {
  88.  
  89. if(people[i].lastname == lastname && people[i].firstname == firstname )
  90. {
  91. 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;
  92. }
  93.  
  94.  
  95. }
  96.  
  97. }
  98.  
  99. else if(type == '1' && type == '2')
  100. {
  101. while (type == 1 && type == 2)
  102. {
  103. 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";
  104. cin >> type;
  105. }
  106. }
  107.  
  108. else if(type == 'exit')
  109.  
  110.  
  111.  
  112.  
  113. }
Last edited by unk45; Oct 3rd, 2008 at 2:38 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 443
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: address book

 
0
  #6
Oct 3rd, 2008
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
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 63
Reputation: unk45 is an unknown quantity at this point 
Solved Threads: 0
unk45 unk45 is offline Offline
Junior Poster in Training

Re: address book

 
0
  #7
Oct 3rd, 2008
ok..i think i figured that out
  1. for(int i = 0;i < size ;i++)
but now jst asks the last name or first name question when i type in a number?
Last edited by unk45; Oct 3rd, 2008 at 2:52 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,681
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 264
Lerner Lerner is offline Offline
Posting Virtuoso

Re: address book

 
0
  #8
Oct 3rd, 2008
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 ==.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: address book

 
0
  #9
Oct 3rd, 2008
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.
Last edited by stilllearning; Oct 3rd, 2008 at 4:38 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: address book

 
0
  #10
Oct 3rd, 2008
>>if(people[i].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)
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC