Thread: Need help with my addressbook

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    2

    Need help with my addressbook

    Hi. I´m taking Borland C++ classes in school, and I´m making a project there. I´m going to make an adressbook, but I´m having some trubbel. Hope that someone could help me.
    I want to be able to do these things:
    *Enter persons and write them to a text file
    *show the persons in the text file
    *search for persons

    I´ve managed to make a structure of the persons, each person have it´s attribute (name, address, age, id?) and I want to send these to the text file. But when I do this they are in Binary, how could I fix this?
    I can´t find a good code for reading the persons, because I do not know how to give them a correct ID. So I need help with giving them an ID, and reading from the file.
    Hope you understand and could help me with some of my problems.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <conio>
    #include <conio.h>
    #include <stdlib>
    #include <stdlib.h>
    #include <stdio.h>
    #include <fstream>
    #include <string>
    
    struct person
    	{
       string name, adress;
       int age;
    
       }
       ;
    
    menu();
    person_data();
    info();
    
    int main()
    {
    
        menu();
        getch();
    
    }    
    
    menu()
    {
    	char erase;
    	int choice,choice2;
       clrscr();
    
    	cout <<"====----Adressbok----====";
       cout <<endl <<endl <<"1...New friend";
       cout <<endl <<"2...Show adressbook";
       cout <<endl <<"3...Find friend";
       cout <<endl <<"4...Erase adressbook";
       cout <<endl <<"5...Info";
       cout <<endl <<"6...Quit";
    
       main_choice:                               //I´m I allowed to do this?
       cout <<endl <<endl <<"? ";                   //or should I use loops
       cin >>choice;
    
       if (choice==1)
       	{person_data();}
       else if (choice==2)
          {/*goto show_adbook(); */ info();;}                //not finished
       else if (choice==3)
       	{/*goto search(); */ info();;}               //not finished
       else if (choice==4)
       	{
          clrscr();
          cout <<"Erase adressbook? <Y/N> "; cin >>erase;
          	if (erase=='y' || 'Y')
             	{
                ofstream emptyfile("person.txt", ios::trunc); emptyfile.close();
                clrscr();
                cout <<endl <<"Adressbok erased.";
                cout <<endl <<"Back to menu <1>, quit <2> ? "; cin >>choice2;
                    back:
                    if (choice2==1)
                    	{menu();}
                    else if (choice2==2)
                    	{exit(1);}
                    else
                    	{
                      cout <<endl <<"Choose again: ";
                      cin >>choice2; goto back;
          				}
    
                }
             else
                {
                cout <<endl <<"Back to menu <1>, quit <2> ? "; cin >>choice2;
    
                }
          }
       else if (choice==5)
       	{info();}
       else if (choice==6)
       	{exit(1);}
       else
       	{
          cout <<endl <<"Choose again: " ;
          goto main_choice;
          }
    
    	return 0;
    
    }
    
    
    person_data()
    {
    
    	char name[30], adress[30];
       int age, amount, nr=0, choice;
    	clrscr();
    
       cout <<"How many friends do you want to enter? ";     //enklaste sättet för att
    	cin >>amount;                                      //skriva sist i filen
    
    	while (++nr<=amount)
        {
          person p[20];
            {
             clrscr();
          	cout <<"Person " <<nr <<endl <<"--------" <<endl  ;
          	cin.ignore(100,'\n');
       		cout <<"Name: " ;
       		cin.getline(name,30);
       		cout <<"Adress: ";
       		cin.getline(adress,30);
       		cout <<"Age: ";
       		cin >>age;
             cout <<endl;
            }
    
          p[1].name = name;
    	   p[1].adress = adress;
      		p[1].age = age;
    
    
          ofstream writefile("person.txt", ios::app);  //open to write and goto end
          if(!writefile)
       		{
       		cerr <<"Could not write to file";
         	   exit(1);
         	   }
          writefile.write((char *)&p[1], sizeof(person));   //write data of person
          writefile.close();
        }
    
    
       ifstream readfile("person.txt");              //open to read
       if(!readfile)
       	{
       	cerr <<"Could not read from file";
          exit(1);
          }
       readfile.close();
    
    
    	cout <<endl <<endl <<"Back to menu <1>, quit <2> ? ";
       back3:
       cin >>choice;
    
       if (choice==1)
       	{menu();}
       else if(choice==2)
       	{exit(1);}
       else
       	{cout <<endl <<"Choose again ?";
                goto back3;;}
    
       return 0;
    
    }
    
    
    
    info()
    {
    
    	int choice;
       clrscr();
    	cout <<"Adressbook by Erik Iveroth, 2002. v1.8";
    	cout <<endl <<endl <<"Back to menu <1>, quit <2> ? ";
       back4:
       cin >>choice;
    
       if (choice==1)
       	{menu();}
       else if(choice==2)
       	{exit(1);}
       else
       	{cout <<endl <<"Chooce again ?";
                 goto back4;;}
    
       return 0;
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. menu();
    If you really mean
    void menu( void );
    Then say so

    2. You should think really hard about all your use of goto. It's only a small program, and already, its getting hard to follow

    3. Indentation
    Code:
    	char erase;
    	int choice,choice2;
       clrscr();
    Try to keep all this aligned. It's very off-putting to readers of your code if the indentation is off.

    4. writefile.write((char *)&p[1], sizeof(person));
    This should be (for a text file)
    writefile << p[1].name << p[1].age;

    But you will need to add spaces/newlines so that you can read it back in again

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Still ...

    It needs more to go with ....

    just a minute ,....

    Before you wrote your code... Draw it and then you will be having less mistakes
    C++
    The best

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    2
    ok, thanx. I can see now that I have a lot of misstakes, because i rewrote everything into english, and didn´t look it through before I posted it sorry.

    anyway, what I want some help with is how to write each "attribute" (name, adress, age etc) to the text file and then read them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function not working correctly
    By mackieinva in forum C Programming
    Replies: 0
    Last Post: 09-29-2007, 08:22 PM
  2. Loop Query
    By (TNT) in forum C# Programming
    Replies: 5
    Last Post: 02-13-2006, 06:16 PM