C ++使用Getline

时间:2017-03-25 23:54:44

标签: c++ getline

我正在创建一个地址簿程序,但我无法正确阅读该文件。它只读取文件中第一个人的信息,并且它具有无限循环。我知道我的GetRecord功能有问题。关于如何修复我的代码的任何建议将不胜感激。

测试文件

Susan, Smith, (123) 456-7890
101 Main Street
Bob, King, (567) 345-9076
456 Market Street

程序代码

void SearchFirstName(ifstream& inData)
{
    Person p;

    string firstName, lastName, phone, address;

    cout << "Enter the first name of the person: ";
    cin >> p.firstname;

    string upperFirst = NormalizeString(p.firstname);
    string upperSearchFirst = NormalizeString(firstName);
    while (GetRecord(inData, firstName, lastName, phone, address))
    {

        if (upperFirst == upperSearchFirst)
        break;

       if(inData){
        cout << "Person found: " << endl;
        PrintRecord(firstName,lastName, phone, address);
    }
    else
    {
    cout << p.firstname << " not found!" << endl << endl;
    }

    inData.clear();
    inData.seekg(0);
  }
 }


bool GetRecord(ifstream& inData,
           string& firstName, string& lastName,
           string& phone, string& address) 
{
    getline(inData, firstName, ',');

    getline(inData, lastName, ',');

    getline(inData, phone, '\n');

    getline(inData, address);
    return inData;

 }

void PrintRecord(string firstName, string lastName,string phone, string address)
 {
     cout << "Name: " << firstName << " " << lastName << endl;
     cout << "Phone: " << phone << endl;
     cout << "Address: " << address << endl;
 }

输出应该是: 如果用户按名字和bob中的类型搜索

Person found:
Name: Bob King
Phone: (567) 345-9076
456 Market Street

1 个答案:

答案 0 :(得分:0)

由于我没有你的某些类和函数,所以我评论了额外的代码。您可以在代码上轻松应用此方法。它会起作用。

#include <iostream>
#include <string>
#include<fstream>
using namespace std;

void GetRecord(ifstream& inData,
    string& firstName, string& lastName,
    string& phone, string& address)
{
    getline(inData, firstName, ',');

    getline(inData, lastName, ',');

    getline(inData, phone, '\n');

    getline(inData, address);
}

void PrintRecord(string firstName, string lastName, string phone, string address)
{
    cout << "Name: " << firstName << " " << lastName << endl;
    cout << "Phone: " << phone << endl;
    cout << "Address: " << address << endl;
}
void SearchFirstName(ifstream& inData)
{
    string firstName, firstname, lastName, phone, address;
    cout << "Enter the first name of the person: ";
    cin >> firstname;
    bool flag = false;
    //string upperFirst = NormalizeString(p.firstname);
    //string upperSearchFirst = NormalizeString(firstName);
    while (!inData.eof())
    {
        GetRecord(inData, firstName, lastName, phone, address);
        //if (upperFirst == upperSearchFirst)
            //break;
        if (firstName == firstname)
        {
            flag = true;
        }
        if (flag){
            cout << "Person found: " << endl;
            PrintRecord(firstName,lastName,phone,address);
        }
    }
    if (!flag)
    {
        cout << "RECORD NOT FOUND";
    }
}
int main()
{
    ifstream in("File.txt");
    SearchFirstName(in);
    system("pause");
    return 0;

}