如何将新的字符串名称与txt文件中的现有字符串名称比较? [英] How to compare new string name with existing from txt file?

查看:149
本文介绍了如何将新的字符串名称与txt文件中的现有字符串名称比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在搜索姓名的投票程序中实现一个简单的功能,如果这个名字已经存在,它将显示一条消息,表明某人无法投票.但是我对txt文件很困惑.下面的代码无法正常工作,我想了解我需要做的事情. 另外,如何查找全名?我认为这只是在搜索第一个单词

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

推荐答案

如果文件中的名字与mainvoter不匹配,则返回false.代码中的注释以及建议的更改:

You return false if the first name in the file does not match mainvoter. Comments in code with the suggested changes:

bool searchname(const std::string& mainvoter) // const& instead of copying the string.
{                                             // It's not strictly needed, but is often
                                              // more effective.
    std::ifstream voter("voter.txt");

    if(voter) {                        // check that the file is in a good (and open) state
        std::string name;    
        while(std::getline(voter, name)) { // see notes
            if(mainvoter == name) {        // only return if you find a match
                return true;               // use true instead of 1
            }
        }
    } 
    // return false here, when the whole file has been parsed (or you failed to open it)
    return false;                      // and use false instead of 0
}

其他说明:

  • 在检查文件中是否存在投票人的名称之前,请先将该文件的名称放入文件中.您需要检查名称是否首先存在,并且只有在文件中存在该名称的情况下,才应将其添加到文件中.

  • You put the voter's name in the file before you check if the name exists in the file. You need to check if the name exists first and only if it does not exist in the file should you add it to the file.

您使用getline读取了选民的姓名. getline允许使用空格字符,而用于从文件读取的格式化输入voter >> name则不允许(默认).因此,如果您输入"Nastya Osipchuk",您将无法找到匹配项,因为voter >> name在第一个迭代中将读取"Nastya",而在下一个迭代中将读取"Osipchuk".

You used getline to read the name of the voter. getline allows for whitespace characters while the formatted input, voter >> name, that you used to read from the file does not (per default). So, if you enter "Nastya Osipchuk", you would not be able to find a match since voter >> name would read "Nastya" in the first iteration and "Osipchuk" in the next.

如果将searchname函数移到main上方,则可以避免前向声明.

You can avoid the forward declaration if you move the searchname function above main.

也请阅读:为什么使用命名空间标准; "被认为是不好的做法?

这篇关于如何将新的字符串名称与txt文件中的现有字符串名称比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆