lundi 18 janvier 2021

How to compare two strings? Here is my code , it is not giving desired results. It is not comparing properly [duplicate]

My code is all about finding that a particular name exists in names.txt or not

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    ifstream file("names.txt");
    string names[6];
    string test;
    cin>>test;
    for(int i=0;i<6;i++)
    {
        getline(file, names[i]);
    }
    for(int i=0;i<6;i++)
    {
        if(names[i]==test)
        {
            cout<<"found";
            return 0;
        }

    }
    cout<<"not found";
    return 0;
}

here, in my code names.txt contains 6 names as:

john walker
rick jo
steaven fedrer
anil kumar
raju rastogi
priyanka raj

but when i enter a name present in name.txt then also it says "not found"(i am entering full name). Why? Where i am going wrong?

Aucun commentaire:

Enregistrer un commentaire