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

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...

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

Edited 3 Years Ago by Dani: Formatting fixed

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();
}

Edited 3 Years Ago by Dani: Formatting fixed

Comments
Use code tags.

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

>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.

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?

>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.

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.

>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.

OK, done some formatting on that code to see how it looks...

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

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;
}


int main()
{
	phoneBook rec;
	fstream file;
	file.open("phone.dat", ios::ate | ios::in | ios:ut | 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();
}

Now let's disect this and see why it's bad code without giving code to replace it (leaving the actual fixing to the OP)...

  1. use of goto
  2. way too much code inside the main method
  3. use of \n instead of iomanip functions
  4. no comments anywhere
  5. style errors (C style braces instead of C++ style), I did fix that to improve readability

There will probably be more if I really dive into it, but this should keep you occupied for a while.

>done some formatting on that code
You also made logical changes as well. By the way, conio.h isn't a standard header, so the .h removal doesn't apply to it.

>use of goto
It's either that or a flag if you want to break out of a loop from a switch, I fail to see why this is a bad thing unless you follow the "goto is evil because somebody said so" school of thought.

>way too much code inside the main method
This is a style issue, but the code could easily be modularized so that each part is easier to follow.

>use of \n instead of iomanip functions
I can only assume that you mean endl, but it's not declared in <iomanip>, it's declared in <ostream>, and using \n instead of endl is typically considered better style except for when an explicit flush is needed.

>no comments anywhere
Well written code may not need comments. It really depends on the application in question, the target audience, and personal style.

>style errors (C style braces instead of C++ style)
There's no such thing as a style "error". And saying that K&R bracing is C and Allman is C++ is just silly. The only bracing problem I see with the original code is lack of consistency.

You completely missed the real issues, I'm afraid. Maybe if I can develop enough interest I'll post the actual problems of the original code.

>I made no logical changes in the code.
BS. What do you call changing this

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

To this?

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

Not only are the standard C++ headers different in many subtle ways from the pre-standard headers, you've introduced two errors right away by removing .h from conio.h and neglecting to qualify for the std namespace. These change the program in ways other than whitespace, which is what formatting is. So let me be a little more blunt: Your "formatting" has broken the code. At least before it might have worked, now it's sure not to.

ok first of all-i am a girl
second-i just wanted HELP. i am not submitting that code b/c it doesn't work and we haven't learned some of the stuff. and i am not dumb enough to turn it in...i just wanted HELP on a little part, not even the code, just a guide to what i need to do. so quit being so mean (please?)

Hi There! Seems u are pretty upset by that behaviour. But never mind. Here i Am giving you the code for searching by name and no. The update part wont work as it needs some time which i am short of. HOPE THIS WILL BE OF SOME HELP.

/*Q-4	Write an interactive ,menu driver program that will access the file containing the list 	of telephone number and implement the following tasks.

		A>. Determine the telephone number of hte specified person.
		B>. Determine the name if a telno. is known.
		C>. Update the telno, whenever there is change.

Ans. */
#include<iostream.h>
	#include<conio.h>
	#include<fstream.h>
	#include<string.h>

	void person();
	void update();
	void tel();

	class dir
	{
		char name[10];
		int no;
		public:
			void getdata()
			{
				cout<<"Enter Name:";
				cin>>name;
				cout<<"Enter Telno."    ;
				cin>>no;
			}
			int t()
			{
				return no;
			}
			char *n()
			{
				return name;
			}
			void puttel()
			{
				cout<<"Telephone No. is:"<<no;
			}
			void putname()
			{
				cout<<"Name is"<<name;
			}
			void gettel()
			{
				cin>>no;
			}
			void getname(char *name)
			{
				strcpy(name,name);
			}
	};

	void main()
	{
		dir d;
		fstream out;
		out.open("directory.txt",ios::out|ios::in|ios::ate|ios::binary);
		d.getdata();
		out.write((char*)&d,sizeof(d));
		out.seekg(0);
		out.close();
		cout<<"1. Search telephone number by person\n";
		cout<<"2. Search person by telephone number\n";
		cout<<"Update telephone number\n";
		int ch;
		cin>>ch;
		switch(ch)
		{
			case 1:person();
			       break;
			case 2:tel();
			       break;
			case 3:update();
			       break;
		}
		getch();
	}
	void person()
	{
		dir d;
		fstream out;
		char name[20];
		out.open("dir.txt",ios::out/ios::in/ios::ate/ios::binary);
		cout<<"Enter Person Name:";
		cin>>name;
		out.seekg(0);
		while(out.read((char*)&d,sizeof(d)))
		{
			if (strcmp(name,d.n())==0)
			{
				d.puttel();
				break;
			}
		}
		out.close();
	}

	void tel()
	{

		dir d;
		fstream out;
		int no;
		out.open("dir.txt",ios::out/ios::in/ios::ate/ios::binary);
		cout<<"Enter telephone number:";
		cin>>no;
		out.seekg(0);
		while(out.read((char*)&d,sizeof(d))  )
		{
			if (no==d.t())
			{
				d.putname();
				break;
			 }
		}
		out.close();
	  }

	void update()
	{
		dir d;
		fstream out;
		char name[20];
		out.open("dir.txt",ios::out/ios::in/ios::ate/ios::binary);
		cout<<"Enter Person Name:";
		cin>>name;
		out.seekg(0);
		while(out.read((char*)&d,sizeof(d))    )
		{
			if (strcmp(name,d.n())==0)
			{
				int last;//=d.gettel();
				int n=last/sizeof(d);
			       //	int loc=(n-1)^n sizeof(d);
			       //	out.seekp(loc);
				cout<<"Enter telephone number:";
				d.gettel();
				d.getname(name);
				out.write((char*)&d,sizeof(d))  ;
				break;
			}
		}

	out.close();
	
	}

>ok first of all-i am a girl
Sorry. Like everyone else, I assume that everyone is a guy to give myself the best chance of being correct with gender specific grammar. No offense was intended.

>so quit being so mean (please?)
I don't recall being mean to you.

>just a guide to what i need to do.
Ask a specific question and you'll get a specific answer. Broad questions like "where do I start?" or "how do I do this?" are far too vague to give a decent answer to.

>HOPE THIS WILL BE OF SOME HELP.
Bad code doesn't help anyone.

ok first of all-i am a girl
second-i just wanted HELP. i am not submitting that code b/c it doesn't work and we haven't learned some of the stuff. and i am not dumb enough to turn it in...i just wanted HELP on a little part, not even the code, just a guide to what i need to do. so quit being so mean (please?)

sorry about it not working. it will work if u initialise the variables outside the switchcase. for eg:- initialise location outside sswitchcase - in the main function itself. and never mind that narue. i have seen him nice to anybody. hope i didn't so much of a trouble for u.

This question has already been answered. Start a new discussion instead.