在文本文件中查找电话号码 [英] Phone number lookup within text file

查看:176
本文介绍了在文本文件中查找电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我的计划目标是我有一个包含4个电话号码的文件 看起来像这样

Basically My Program Goal is that i have a file containing 4 telephone numbers it would Look Like this

Harry Keeling (555)123-4567
Frank James (555)123-8901
Arthur Paul (555)987-4567
Todd Shurn (555)987-8901

我的程序当前正在执行的操作是提示用户输入名称和姓氏,并遍历文件以查看它们是否匹配,如果匹配则将电话号码保存在变量电话号码中(如果不是)匹配,程序输出错误,现在我的程序正在做应做的事情,但是在找到每个匹配项后,该程序会提示您是否要继续查找数字y或n(如果是,则应循环查找)再次通过文件,但是基本上我的程序无法正常工作,我也不知道为什么我的代码

What my program is doing right now is prompting the user for the name and last name and iterates through the file to see if their is a match if their is a match the phone number is saved in the variable phone number if their isnt a match the program outputs error right now my program is doing what is susposed to do but after each match is found the program is susposed to prompt do you want to continue looking up numbers y or n if it is a yes it is susposed to loop through the file again but basically my program isnt working and i have no idea why my code

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void lookup_name(ifstream&, string&);  // prototype
int main()
{
    ifstream myfile;
    string phonenumber;
    string choice;
    lookup_name(myfile, phonenumber);
    if (phonenumber == " ") {
        cout << "Error" << endl;
    }
    else {
        cout << "The Telephone Number you Requested is" << phonenumber <<    endl;
        cout << "Do you Want to look up another name in the directory?" << " " << "<Y/N" << endl;
        cin >> choice;
        if (choice == "Y")
            lookup_name(myfile, phonenumber);
    }
}
void lookup_name(ifstream& myfile, string& phonenumber)
{
    string fname;
    string lname;
    string name1, name2, dummy, choice;
    myfile.open("infile.txt");
    cout << "What is your first name" << endl;
    cin >> fname;
    cout << "What is your last name" << endl;
    cin >> lname;
    for (int i = 0; i < 4; i++) {
        myfile >> name1 >> name2;
        if (fname + lname == name1 + name2) {
            myfile >> phonenumber;
            myfile.close();
            if (choice == "Y")
            {
                continue;
            }
            else {
                myfile >> dummy;
            }
    }


    }
}

推荐答案

您需要在main()本身内部添加一个循环,以提示用户继续,并且需要修复lookup_name()函数中的错误.

You need to add a loop inside of main() itself to prompt the user to continue, and you need to fix the mistakes in your lookup_name() function.

请尝试以下类似操作:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <limits>

using namespace std;

bool lookup_name(istream&, const string&, string&);  // prototype

int main()
{
    ifstream myfile("infile.txt");

    string name, phonenumber, choice;

    do
    {
        cout << "What is the name? ";
        getline(cin, name);

        if (!lookup_name(myfile, name, phonenumber)) {
            cout << "Error" << endl;
        }
        else {
            cout << "The Telephone Number you Requested is '" << phonenumber << "'" << endl;
        }

        cout << "Do you want to look up another name in the directory (Y/N)? ";
        cin >> choice;
        cin.ignore(numeric_limits<streamsize_t>::max(), '\n');

        if ((choice != "Y") && (choice != "y"))
            break;

        myfile.seekg(0);
    }
    while (true);

    return 0;
}

bool lookup_name(istream& myfile, const string& name, string& phonenumber)
{
    string line, fname, lname;

    while (getline(myfile, line))
    {
        istringstream iss(line);
        if (iss >> fname1 >> lname)
        {
            if (name == (fname + " " + lname))
                return getline(iss, phonenumber);
        }
    }

    return false;
}

这篇关于在文本文件中查找电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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