| | |
address book
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 63
Reputation:
Solved Threads: 0
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
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;
}
} 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:
Example:
C++ Syntax (Toggle Plain Text)
... 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 }
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.
•
•
Join Date: Sep 2008
Posts: 63
Reputation:
Solved Threads: 0
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
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
C++ Syntax (Toggle Plain Text)
#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; } }
Last edited by unk45; Oct 3rd, 2008 at 2:28 pm.
•
•
Join Date: Sep 2008
Posts: 63
Reputation:
Solved Threads: 0
yeah..sorry about that..just realized it myself..so whats that command to make it jump to the press any key to continue part??
C++ Syntax (Toggle Plain Text)
#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') }
Last edited by unk45; Oct 3rd, 2008 at 2:38 pm.
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
-chandra
•
•
Join Date: Sep 2008
Posts: 63
Reputation:
Solved Threads: 0
ok..i think i figured that out but now jst asks the last name or first name question when i type in a number?
C++ Syntax (Toggle Plain Text)
for(int i = 0;i < size ;i++)
Last edited by unk45; Oct 3rd, 2008 at 2:52 pm.
•
•
Join Date: Jul 2005
Posts: 1,681
Reputation:
Solved Threads: 264
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 ==.
----------------------------------------------
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 ==.
>>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.
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.
![]() |
Similar Threads
- A C++ Simple Address book (C++)
- Creating a Address Book using Random Access Files (Visual Basic 4 / 5 / 6)
- C++ Address Book (C++)
- Where does Address Book & email hide? (Web Browsers)
- lost address book addresses (OS X)
- Address book deleted? HELP (OS X)
- Saving Address book and Messages in Outlook Express (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Help with array parameters
- Next Thread: exponential summation help PLEASE!
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






