| | |
No idea how to do this problem...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2004
Posts: 7
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Nov 2004
Posts: 14
Reputation:
Solved Threads: 0
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();
}
#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();
}
>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.
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.
New members chased away this month: 3
>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.
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.
New members chased away this month: 3
![]() |
Similar Threads
- Do you have any Idea about this problem? Very Interesting (Troubleshooting Dead Machines)
- A severe problem with programs trying to connect to internet (Web Browsers)
- Internet Explorer problem (Web Browsers)
- Problem with objects having objects as attributes (C++)
- modem drivers error installing and another problem (*nix Hardware Configuration)
- Save Picture As .... problem (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: percentage
- Next Thread: need help with simple overloading operator+ for adding two object data.
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






