I want to implement a simple function in a voting program that is searching for a name, and if this name is already existing then it will show a message that a person cannot vote. But I'm so confused with txt files. The code below does not work properly, I want to understand what I need to do. Also, how to find a full name? I think it is only searching for the first word
bool searchname(string mainvoter);
int main()
{
ofstream newvoter("voter.txt", ios::app);
string name;
cout<<"Enter your name: ";
getline(cin, name);
newvoter << name << endl;;
newvoter.close();
if(searchname(name)){
cout<<"This person already voted!"<<endl;
}
else
cout<<"Okay!"<<endl;
}
bool searchname(string mainvoter)
{
ifstream voter("voter.txt");
string name;
while (voter >> name ){
if (mainvoter == name){
return 1;
}
else
return 0;
}
}