Agenda with only structs...

Hi, I need some advice how can I read a data of my file.txt and compare with
a string entered by user... I just made a 1st and 2nd option (funtion LOOK_FOR) of menu...The problem is in 2nd option...when I need to look for a contact in the file.txt...
I don't know how to compare. and print the information contact... Thanks a lot if someone can help me with this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*_____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::ios;       // ios::app ==> saved and keep the information
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;

  // ios::app ==>> To saved and keep the information on the .txt file

  ofstream out_data("CONTACTS.txt"); //Out-file information in CONTACTS.txt

  out_data << "\t ***************** MY CONTACTS *****************\n\n";

  out_data << setw(5); out_data  << "Name";
  out_data << setw(30); out_data << "Phone";
  out_data << setw(30); out_data << "E-mail";
  out_data << setw(30); out_data << "Address" << endl;

  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;

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

Because you are using a std::string, why don't you just use the == operator? For example, line 174 can just be this:
if (NN == people[i].Naame)
I tried with that and I couldn't fix the problem....I need to compare and if the strings are the same print all data about the contact....
Topic archived. No new replies allowed.