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...
#include <iostream>
#include <fstream>
#include <string>
usingnamespace 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?
constint 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.