Agenda en c++ con estructura

Iniciado por Ja_90, 28 Octubre 2014, 20:52 PM

0 Miembros y 1 Visitante están viendo este tema.

Ja_90

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 (cpp) [Seleccionar]

/*_____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();
}
:D  ::::Ja_90::::   :D