为什么此代码不允许我在文件中搜索?

时间:2019-12-29 20:17:21

标签: c++ search text-files dev-c++

我是一个完整的初学者,希望有更多知识的人可以看一下这段代码,并告诉我我要去哪里了?当我尝试搜索时,它说即使输入文件中存在的名称也找不到记录。除此之外,我在文件中输入的名称将被保存而没有首字母。例如:哈里就是哈里。这是2000多行程序的摘录。渴望有人帮助。谢谢。

#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<istream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;
struct details
{
    string name;
    string phone_no;
    int age;
}customer;
void searchbyname();
int main()
{
    int search;
    cout<<"\n\n\t\t\tName: ";
    cin.ignore();
    getline(cin,(customer.name));
    cout<<"\n\t\t\tPhone Number: ";
    cin>>customer.phone_no;
    cout<<"\n\t\t\tAge: ";
    cin>>customer.age;
    ofstream file("data.txt",ios::app);
    file<<customer.name<<"\n"<<customer.phone_no<<"\n"<<customer.age<<"\n\n";
    file.close();
    cout<<"Press 1 to search";
    cin>>search;
    if(search==1)
        searchbyname();



}


void searchbyname()
{
    int temp=0;
    string name1;
    ifstream check("data.txt");
    cout<<"\t\tEnter Your Name: ";
    cin.ignore();
    getline(cin,name1);
    while(check>>customer.name>>customer.phone_no>>customer.age)
    {
        if(name1==customer.name)
        {
            system("cls");
            cout<<"\n\tCustomer Name: "<<customer.name;
            cout<<"\n\tPhone Number: "<<customer.phone_no;
            cout<<"\n\tAge: "<<customer.age;
            temp++;
            getch();
            continue;
        }
        if(temp==0)
        {
            system("CLS");
            cout<<"\n\n\n\n\t\tNo Record(s) Found.\n\t Press Any Key to be Redirected to the Main Menu.\n\n\n\n";
            getch();
            main();
        }       
    }
    check.close();
}

0 个答案:

没有答案