Click here to Skip to main content
14,641,288 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi friends, please help me out. I need to read a text file that contains names along with another text(include,exclude) separated by a comma. I need to compare it with a user input for example name in the text file and name provided as user input should match. If they both match a bock of code to be executed else another block of code to be executed. I am posting my sample code.

What I have tried:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{

    fstream newfile;

    string query,user;
    cout<< "enter query\n";
    getline(cin,query);

    cout<< "enter username\n";
    getline(cin,user);

    newfile.open("users.txt",ios::in); 
    if (newfile.is_open()){ 
    string tp;
    while(getline(newfile, tp)){ 
         //cout << tp << "\n"; 
       if (tp.compare(user)!= string::npos)
         {
             cout << query<<"\n";
          //   found = true;
          break;
         }
         else
            {
                cout << "print something";
         }

      }
   }
      newfile.close();



My text file contains data as below
user1 , include
user2 , exclude
user3 , include

Note: When a user is provided as input and the file has the username along with include the if block should execute or else block should execute
Posted
Updated 20-Jul-20 10:40am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

I would use a std::set to store 'included' users:
#include <iostream>
#include <fstream>
#include <string>
#include <set>

using namespace std;

int main()
{
    set<string> included_user;

    string query,user;

    cout<< "enter query\n";
    getline(cin,query);

    cout<< "enter username\n";
    getline(cin,user);

    { // read the file and fill the set of 'included' users
      ifstream newfile( "users.txt");
      do
      {
        string name, comma, inclusion;
        newfile >> name >> comma >> inclusion;
        if ( inclusion == "include")
          included_user.insert(name);
      } while (newfile);
    }

    // conditiona execution here
    if (included_user.count(user) > 0)
    {
      cout << query<<"\n";
    }
    else
    {
      cout << "print something\n";
    }
}
   
Comments
Espen Harlinn 20-Jul-20 18:10pm
   
Nice and simple :-)
CPallini 21-Jul-20 2:13am
   
Thank you!
pooher 21-Jul-20 3:17am
   
This is printing only the else block and not the if block, also the set has no value inside. Could you plz help me on this.
CPallini 21-Jul-20 3:51am
   
The set should not be empty (and it should execute the 'if' block, with appropriate input). I've tested it with your input file.
pooher 21-Jul-20 7:16am
   
actually what happens is name has first rows of text file, comma has second row of text file and inclusion has third of text file. For every input it print else block content :(.
Rate this:
Please Sign up or sign in to vote.

Solution 1

The string::compare function returns one of three values as described at basic_string Class | Microsoft Docs[^]. That is what you should be testing against.

[edit]
Also you need to extract the username from the text read from the file. If you compare "user1" to "user1 , include" it will never match.
[/edit]
   
v3
Comments
pooher 21-Jul-20 3:16am
   
Could you please let me post some code if possible as I am new in C++ and trying to find out answers

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100