Código
/*_____LIBRARIES______*/ #include <iostream> #include <iomanip> // setw() #include <fstream> // read and write outfile .txt #include <string> using std::setw; // tab using std::ofstream; // write outfile .txt using std::ifstream; // read outfile .txt using std::cout; using std::cin; using std::endl; using std::string; /*_____GLOBAL VARIABLS______*/ const int LEN = 2; /*_____FUNTIONS______*/ void menu(); void ADD_friend (struct FRIENDS people[], int); void LOOK_FOR (struct FRIENDS people[], int); /*_____STRUCTS________*/ struct FRIENDS { string Naame, email, Adress, phone; }; /*_____MAIN FUNTION_____*/ int main() { cout << "\t *************---------------****************\n"; cout << "\t ************* MY CONTACTS ****************\n"; cout << "\t *************---------------****************\n\n"; int option; // options of switch case loop int index = 0; //Position of each contact. struct FRIENDS Data[LEN]; // STRUCT // do { menu(); // call funtion => show the MENU while(true) // CHOOSE OPTION // { cout << "Enter your option: "; cin >> option; cin.ignore(); if(option >= 1 && option <= 7) { break; } else { cout << "ERROR, Enter correct value.\n\n"; } } switch(option) // OPTIONS FUNTIONS // { case 1: ADD_friend (Data,index); break; case 2: cout << "Find a contact \n"; LOOK_FOR(Data,index); break; case 3: cout << "Edit funtion"; break; case 4: cout << "Clear funtion"; break; case 5: cout << "show all contacts"; break; case 6: cout << "GOOD BYE" << endl; break; } }while(option != 6); cin.get(); return 0; } /// MENU FUNTION /// void menu() { cout << "\n///////// MENU //////////" << endl; cout << "---------------------------" << endl; cout << "Choose an option:" << endl << endl; cout << "1. Add friend." << endl; cout << "2. find a friend." << endl; cout << "3. Edit." << endl; cout << "4. Clear." << endl; cout << "5. Show all friends." << endl; cout << "6. Exit." << endl << endl; } /// ADD_FRIEND_outFile.txt FUNTION /// void ADD_friend (FRIENDS people[], int index) { cout << "Name: "; getline(cin,people[index].Naame); cout << "Phone: "; getline(cin,people[index].phone); cout << "E-mail: "; getline(cin,people[index].email); cout << "Address: "; getline(cin,people[index].Adress); index++; // increment the index for the next position to add a contact cout << endl; ofstream out_data("CONTACTS.txt"); //Out-file information in CONTACTS.txt out_data << "\t ***************** MY CONTACTS *****************\n\n"; for(int i=0 ; i<index; i++) { out_data << setw(5); out_data << people[i].Naame; out_data << setw(30); out_data << people[i].phone; out_data << setw(30); out_data << people[i].email; out_data << setw(30); out_data << people[i].Adress << endl; } out_data.close(); } /// LOOK_FOR a FRIEND FUNTION //// void LOOK_FOR (struct FRIENDS people[], int index) { ifstream read_data("CONTACTS.txt"); if(read_data.is_open()) { string NN; // Nombre ingresado por el usuario para comprar con datos del archivo .txt cout << "Enter a name: "; getline(cin,NN); for(int i=0 ; i<index ; i++) { if(NN.compare(people[i].Naame) == 0) { cout << "Name: " << people[i].Naame << endl; cout << "Phone" << people[i].phone << endl; cout << "E-mail: " << people[i].email << endl; cout << "Address: "<< people[i].Adress<< endl; } } } read_data.close(); }