if statement with files

I am attempting to make a program where a person enters a name of a person located on a line in the text file. If the name is in the program, the program will say " name is found" If not, it says " name is not found" and gives the person another try to enter a name.

My issue is that I have no idea on how to make the program search the file for the name on the line but this is what I have...

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
#include <iostream>
#include <fstream>
#include <string>


using namespace std;

int main()
{
    ifstream Namefile("Name.txt");
    int name;
    char answer;

    cout << "Enter name of person" << endl;
    cin >> name;
   // if  (name != Namefile)
   if (name == 1)
   cout << "is 1 the name you want? \n";
   cout << "enter 'y' or 'n' for yes or no \n";
   cin >> answer;
   if (answer == 'y')
   cout << "example name" << endl;

   else
   cout << "wrong answer\n close program and start again";









    return 0;
}



My Name.txt file has
1
2
3
worm joy
rider mom
joyful person


How do I tell the program to search for a name on x line?
you can upload the file to an array/vector or make a pointer. those are just two ways i can think of right off the back.
for example

while(!filename.eof()){// .eof() is kind of buggy so try .good() if you have problems
filename >> myvector.pushback(name);
}

You have to read from the file.

The quickest way I can think is this:

1
2
3
4
5
6
7
8
9
10
const int SIZE = 256;
int count = 0;
ifstream Namefile("Name.txt");
string names[SIZE]

while (getline.(Namefile, names[count])
   count++;

// names is now an array filled with all of the names of the file
// count is the amount of names 

getline will get all chars including white space until a new line char is read. are u looking for just one of the words or a string of words. that will determine which of the functions you use.
Topic archived. No new replies allowed.