Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: Ja_90 en 28 Octubre 2014, 20:52 pm



Título: Agenda en c++ con estructura
Publicado por: Ja_90 en 28 Octubre 2014, 20:52 pm
Buenas, estoy realizando una agenda en c++ con menu....que me guarde los datos en un archivo .txt y hasta ahora llevo la primera opcion del menu...(Adicionar un contacto)...Estoy haciendo la 2da opcion del menu...(encontrar o buscar contacto)...pero tengo problemas en la funcion, cuando quiero leer los datos del archivo para comprarlo con el nombre dado por el usuario...No tengo claro como leer los datos del archivo....o si los estoy guardando de manera correcta... aca dejo el codigo quien pueda darme algun consejo le agradeceria....

Código
  1. /*_____LIBRARIES______*/
  2.  
  3. #include <iostream>
  4. #include <iomanip>   // setw()
  5. #include <fstream>   // read and write outfile .txt
  6. #include <string>
  7.  
  8. using std::setw;      // tab
  9. using std::ofstream;  // write outfile .txt
  10. using std::ifstream;  // read outfile .txt
  11. using std::cout;
  12. using std::cin;
  13. using std::endl;
  14. using std::string;
  15.  
  16.  
  17. /*_____GLOBAL VARIABLS______*/
  18.  
  19. const int LEN = 2;
  20.  
  21. /*_____FUNTIONS______*/
  22.  
  23. void menu();
  24.  
  25. void ADD_friend (struct FRIENDS people[], int);
  26.  
  27. void LOOK_FOR (struct FRIENDS people[], int);
  28.  
  29. /*_____STRUCTS________*/
  30.  
  31. struct FRIENDS
  32. {
  33.  string Naame, email, Adress, phone;
  34. };
  35.  
  36. /*_____MAIN FUNTION_____*/
  37.  
  38. int main()
  39. {
  40.  cout << "\t *************---------------****************\n";
  41.  cout << "\t *************  MY CONTACTS  ****************\n";
  42.  cout << "\t *************---------------****************\n\n";
  43.  
  44.  int option;       // options of switch case loop
  45.  int index = 0;    //Position of each contact.
  46.  
  47.  struct FRIENDS Data[LEN];     // STRUCT //
  48.  
  49.  do
  50.  {
  51.    menu(); // call funtion => show the MENU
  52.  
  53.    while(true)    // CHOOSE OPTION //
  54.    {
  55.      cout << "Enter your option: ";
  56.      cin >> option;
  57.      cin.ignore();
  58.  
  59.      if(option >= 1 && option <= 7)
  60.      {
  61.      break;
  62.      }
  63.      else
  64.      {
  65.        cout << "ERROR, Enter correct value.\n\n";
  66.      }
  67.    }
  68.  
  69.    switch(option)  // OPTIONS FUNTIONS //
  70.    {
  71.      case 1:
  72.        ADD_friend (Data,index);
  73.        break;
  74.  
  75.      case 2:
  76.        cout << "Find a contact \n";
  77.        LOOK_FOR(Data,index);
  78.        break;
  79.  
  80.      case 3:
  81.        cout << "Edit funtion";
  82.        break;
  83.  
  84.      case 4:
  85.        cout << "Clear funtion";
  86.        break;
  87.  
  88.      case 5:
  89.        cout << "show all contacts";
  90.        break;
  91.  
  92.      case 6:
  93.        cout << "GOOD BYE" << endl;
  94.        break;
  95.    }
  96.  }while(option != 6);
  97.  
  98.  cin.get();
  99.  return 0;
  100. }
  101.  
  102. /// MENU FUNTION ///
  103.  
  104. void menu()
  105. {
  106.  cout << "\n/////////  MENU  //////////" << endl;
  107.  cout << "---------------------------" << endl;
  108.  cout << "Choose an option:" << endl << endl;
  109.  
  110.  cout << "1. Add friend." << endl;
  111.  cout << "2. find a friend." << endl;
  112.  cout << "3. Edit." << endl;
  113.  cout << "4. Clear." << endl;
  114.  cout << "5. Show all friends." << endl;
  115.  cout << "6. Exit." << endl << endl;
  116. }
  117.  
  118. /// ADD_FRIEND_outFile.txt FUNTION ///
  119.  
  120. void ADD_friend (FRIENDS people[], int index)
  121. {
  122.  cout << "Name: ";
  123.  getline(cin,people[index].Naame);
  124.  
  125.  cout << "Phone: ";
  126.  getline(cin,people[index].phone);
  127.  
  128.  cout << "E-mail: ";
  129.  getline(cin,people[index].email);
  130.  
  131.  cout << "Address: ";
  132.  getline(cin,people[index].Adress);
  133.  
  134.  index++;     // increment the index for the next position to add a contact
  135.  cout << endl;
  136.  
  137.  ofstream out_data("CONTACTS.txt"); //Out-file information in CONTACTS.txt
  138.  
  139.  out_data << "\t ***************** MY CONTACTS *****************\n\n";
  140.  
  141.  for(int i=0 ; i<index; i++)
  142.  {
  143.    out_data << setw(5); out_data  << people[i].Naame;
  144.    out_data << setw(30); out_data << people[i].phone;
  145.    out_data << setw(30); out_data << people[i].email;
  146.    out_data << setw(30); out_data << people[i].Adress << endl;
  147.  }
  148.  out_data.close();
  149. }
  150.  
  151. /// LOOK_FOR a FRIEND FUNTION ////
  152.  
  153. void LOOK_FOR (struct FRIENDS people[], int index)
  154. {
  155.  ifstream read_data("CONTACTS.txt");
  156.  
  157.  if(read_data.is_open())
  158.  {
  159.    string NN;               // Nombre ingresado por el usuario para comprar con datos del archivo .txt
  160.  
  161.    cout << "Enter a name: ";
  162.    getline(cin,NN);
  163.  
  164.    for(int i=0 ; i<index ; i++)
  165.    {
  166.      if(NN.compare(people[i].Naame) == 0)
  167.      {
  168.        cout << "Name: "   << people[i].Naame << endl;
  169.        cout << "Phone"    << people[i].phone << endl;
  170.        cout << "E-mail: " << people[i].email << endl;
  171.        cout << "Address: "<< people[i].Adress<< endl;
  172.      }
  173.    }
  174.  }
  175.  read_data.close();
  176. }
  177.