944,137 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4600
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 9th, 2004
0

No idea how to do this problem...

Expand Post »
hey i was wondering if someone could help me with this problem. i am so incredibly and utterly confused. here it is...

write a simple phone directory program that looks up phone numbers in a file containing a list of names and phone numbers (called input.txt). the user should be prompted to enter a first and last name and the program then outputs the corresponding number, or indicates that the name isn't in the directory. after each lookup, the program should ask the user whether they want to look up another number, and then either repeat the process or exit the program. the data on the file should be organized so that each line contains a first name, last name and a phone number seperated by blanks.
use functional decomposition to solve th problem and code the solution using functions as appropriate (use a function to get the name from the user and another to check the list to locate the phone number). use appropriate comments.

if someone would help me, i would be GREATLY indebted!!!! please!!!!!

~Megan
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fivetoesonefoot is offline Offline
7 posts
since Nov 2004
Nov 9th, 2004
0

Re: No idea how to do this problem...

What part are you stuck on? Do you need help designing it?

Most people here won't write your code for you, but can help you get started with some suggestions. Let us know what you're stuck on, and we'll try to give you a nudge in the right direction...
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Nov 9th, 2004
0

Re: No idea how to do this problem...

well i dont know how to make it look @ the names in the list, then find out if they match what's on the list. that's a big problem. i think if i get this figured out i can do the rest....thankies!
if someone would help me, i would be GREATLY indebted!!!! please!!!!!

~Megan[/QUOTE]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fivetoesonefoot is offline Offline
7 posts
since Nov 2004
Nov 10th, 2004
-1

Re: No idea how to do this problem...

here is an example for u!

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <iomanip.h>
#include <conio.h>

class phoneBook{
char name[20],phno[6];
public:
void getdata();
void showdata();
char *getname(){ return name; }
char *getphno(){ return phno; }
void update(char *nm,char *telno){
strcpy(name,nm);
strcpy(phno,telno);
}
};

void phoneBook :: getdata(){
cout<<"\nEnter Name : ";
cin>>name;
cout<<"Enter Phone No. : ";
cin>>phno;
}

void phoneBook :: showdata(){
cout<<"\n";
cout<<setw(15)<<name;
cout<<setw(8)<<phno;
}


void main(){
phoneBook rec;
fstream file;
file.open("phone.dat", ios::ate | ios::in | ios::out | ios::binary);
char ch,nm[20],telno[6];
int choice,found=0;
while(1){
clrscr();
cout<<"\n*****Phone Book*****\n";
cout<<"1) Add New Record\n";
cout<<"2) Display All Records\n";
cout<<"3) Search Telephone No.\n";
cout<<"4) Search Person Name\n";
cout<<"5) Update Telephone No.\n";
cout<<"6) Exit\n";
cout<<"Choose your choice : ";
cin>>choice;
switch(choice){
case 1 : //New Record
rec.getdata();
cin.get(ch);
file.write((char *) &rec, sizeof(rec));
break;

case 2 : //Display All Records
file.seekg(0,ios::beg);
cout<<"\n\nRecords in Phone Book\n";
while(file){
file.read((char *) &rec, sizeof(rec));
if(!file.eof())
rec.showdata();
}
file.clear();
getch();
break;

case 3 : //Search Tel. no. when person name is known.
cout<<"\n\nEnter Name : ";
cin>>nm;
file.seekg(0,ios::beg);
found=0;
while(file.read((char *) &rec, sizeof(rec)))
{
if(strcmp(nm,rec.getname())==0)
{
found=1;
rec.showdata();
}
}
file.clear();
if(found==0)
cout<<"\n\n---Record Not found---\n";
getch();
break;

case 4 : //Search name on basis of tel. no
cout<<"\n\nEnter Telephone No : ";
cin>>telno;
file.seekg(0,ios::beg);
found=0;
while(file.read((char *) &rec, sizeof(rec)))
{
if(strcmp(telno,rec.getphno())==0)
{
found=1;
rec.showdata();
}
}
file.clear();
if(found==0)
cout<<"\n\n---Record Not found---\n";
getch();
break;

case 5 : //Update Telephone No.
cout<<"\n\nEnter Name : ";
cin>>nm;
file.seekg(0,ios::beg);
found=0;
int cnt=0;
while(file.read((char *) &rec, sizeof(rec)))
{
cnt++;
if(strcmp(nm,rec.getname())==0)
{
found=1;
break;
}
}
file.clear();
if(found==0)
cout<<"\n\n---Record Not found---\n";
else
{
int location = (cnt-1) * sizeof(rec);
cin.get(ch);
if(file.eof())
file.clear();

cout<<"Enter New Telephone No : ";
cin>>telno;
file.seekp(location);
rec.update(nm,telno);
file.write((char *) &rec, sizeof(rec));
file.flush();
}
break;
case 6 ://Exit
goto out;
}
}
out:
file.close();
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tresa is offline Offline
14 posts
since Nov 2004
Nov 10th, 2004
0

Re: No idea how to do this problem...

holy crap....how long did that take you? THANKS SOOOO MUCH!!! i owe you big time, if there is anything you need, ANYTHING ask!! thankies again
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fivetoesonefoot is offline Offline
7 posts
since Nov 2004
Nov 11th, 2004
0

Re: No idea how to do this problem...

>how long did that take you?
The real question is how long will it take him to realize that doing your homework for you was a mistake.

>i owe you big time
No, you don't. The code you were given would get me fired, and it'll probably get you a bad grade if your teacher has any brain at all.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 11th, 2004
0

Re: No idea how to do this problem...

now why did you alert him to the fact that he shouldn't just submit that code as his assigment?

Don't we want people who're too lazy to do their own work to get burned?
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Nov 11th, 2004
0

Re: No idea how to do this problem...

>Don't we want people who're too lazy to do their own work to get burned?
I try to avoid such things whenever possible. But you might be a sadistic prick while I'm not. Everyone is different.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 11th, 2004
0

Re: No idea how to do this problem...

let them burn once so they never burn again. One bad grade won't hurt anything but his pride but he may learn from it to think for himself.

Not sadistic, just realism.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Nov 11th, 2004
0

Re: No idea how to do this problem...

>but he may learn from it to think for himself.
Then again, he may never come back because all we helped him do is get bad grades. Then he'll probably try again on another forum with similar success. I'd rather have someone here, where I can guide their progress, rather than elsewhere, screwing up and not learning the right lessons from it.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: percentage
Next Thread in C++ Forum Timeline: need help with simple overloading operator+ for adding two object data.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC