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



LinkBack URL
About LinkBacks


