Hello, I would like to know how do I compare two chars properly
Im making a healthcare project, its pretty rudimentary, Im trying
to make a comparison between a char that the user inputs
and the char that is in a structure that is saved in a .txt file
So far I have made two Functions for this, im using strstr to compare
but All it returns is the first input that is saved in the .txt file
doesnt matter what name I search for its always the same..:

using namespace std;
	bool I_code3(const char*spr, patient &ab); //Function for search logic
	void search_C_Interface();
	void menu();
	void saved(patient ab);
	 
	void sI_name(){
	     patient ac;    //structure patient
	     char name[50];  //char for user input
	     bool found;   //boolean for logical operations
	     int nopc;
	 
	     cout<<" -----Search by Name------- "<<endl;
	     printf(" Name for Search: ");
	     cin>>name;
	 
	     found = I_code3(name, ac);  //the input is passed as argument to the function for logic
	 
	 if(found == true)   //If the data is found display:
	  {
	  cout<<"\n\n name: "<<ac.name<<endl;
	  cout<<" Blood: "<<ac.blood<<endl;
	  cout<<"Height: "<<ac.height<<endl;
	  cout<<"Social Security: "<<ac.Ssn<<endl;
	  }
	 
	  else{   //if its not output:
	     cout<<" Not found "<<endl;
	  }
	     cout<<"    1.Search for Another Patient"<<endl;  //other functionalities
	     cout<<"    2.Search with another method"<<endl;
	     cout<<"    3.Return to main screen"<<endl;
	     cin>>nopc;
	 
	       switch(nopc){
	         case 1:
	           sI_name();
	           break;
	         case 2:
	           search_C_Interface();
	           break;
	         case 3:
	             menu();
	       }
	  }
	 
	   bool I_code3(const char*name, patient &ab) //logic for searching
	{
	    FILE*f; //file pointer
	    f=fopen("saved.txt","r");  //saved is the name of my .txt file
	    bool centi=false;
	 
	   do	  {
	    fread(&ab,sizeof(ab),1,f);
	    if(!(strstr(name, ab.name)))  //str to compare the input of the user with the strucure
	      centi=true;
	  }while(!(feof(f)||centi==true));
	 
	        fclose(f);  
	        if(centi==true)
	        return true;
	    else
	        return false;
	          };

I have tried it with other things like find according to age
and it works perfectly, but the age i did put it in char too
I honestly dont know anymore, if anyone could help me out
it would be of great appreciation, thanks

>>if(!(strstr(name, ab.name)))

line 55. strstr() does not compare two strings to see if they are the same. Instead, strstr() tries to find one string from within another larger string. What you should use here is strcmp(), not strstr(). strcmp() will return 0 if the two strings are exactly the same, including case. "John" and "john" are not the same because of capitalization. Use stricmp() to compare ignoring case.

I would really suggest not using C and C++ the way you are. You have outputs using cout and printf().

Here is a C++ convert.

#include <iostream>
#include <fstream>

using namespace std;

struct patient
{
	string name;
	string blood;
	string height;
	string Ssn;
};

	bool I_code3(string name, patient &ab); //Function for search logic
	//void search_C_Interface();
	//void menu();
	void saved(patient ab);

void sI_name()
{
	patient ac;    //structure patient
	string name;  //char for user input

	cout << " -----Search by Name------- " << endl;
	cout << " Name for Search: ";
	cin >> name;

	if(I_code3(name, ac) == true)   //If the data is found display:
	{
		cout << "\n\n name: " << ac.name << endl;
		cout << " Blood: " << ac.blood << endl;
		cout << "Height: " << ac.height << endl;
		cout << "Social Security: " << ac.Ssn << endl << endl;
	}

	else
	{   //if its not output:
		cout << endl << " Not found " << endl;
	}
	cout << "    1.Search for Another Patient" << endl;  //other functionalities
	cout << "    2.Search with another method" << endl;
	cout << "    3.Return to main screen" << endl;
}

bool I_code3( string name, patient &ab) //logic for searching
{
	ifstream f("saved.txt");  //saved is the name of my .txt file

	while( f >> ab.name >> ab.blood >> ab.height >> ab.Ssn )
	{
		if( ab.name.find(name) != string::npos )
		{
			f.close();
			return true;
		}
	}

	f.close();
	return false;
}

int main()
{
	sI_name();

	return 0;
}

I would agree but the file must be read as binary data using read() instead of as text, unless the OP wants to change the way the file was written.

ifstream f("saved.txt",ios::binary); // open in binary mode

while( f.read((char *)&ab, sizeof(patient) )
{
   if( name == ab.name)
   {

   }
}

Edited 5 Years Ago by Ancient Dragon: n/a

This article has been dead for over six months. Start a new discussion instead.