Hello guys,
I'm making a C++ project on periodic table.In that,i want to search for elements on the basis of their names in such a way that if i give an alphabet or more than 1 alphabets as the search value then all the elements starting from that letter should be displayed.
Suppose the search value is Lit,
then Lithium should be displayed.
Or if i give C as the search value then all the elements starting from C should be displayed..
Carbon
Calcium
Cerium
etc..
I've been able to develop the following function for this but it finds the element if the search value exactly matches the element name.


//The function starts here

void search()
{
ifstream o1;
o1.open("file"ios::binary);
int flag=0;
char name[10];//SEARCH VALUE
cout<<"Enter the element's name to be searched...";
cin>>name;
while(o1.read((char*)&object,sizeof(object)))
{
if(strcmpi(name,object.retname())==0)
//object.retname() is a function defined in class  to return element's name 
{
flag=1;
cout<<"\nElement found..."<<object.retname();
break;
}
}
if(flag==0)
cout<<"\nElement not found...";
o1.close();
}

Could anyone write the function for me to solve my problem......
PLEASE....
I NEED URGENT HELP//////

Recommended Answers

All 2 Replies

You haven't given enough detail in your post, and in future use code-tags.

>PLEASE.... I NEED URGENT HELP//////
No need for the upper-case characters, this may be urgent to you, but it's not for us.

Here is the code well formatted, with your error highlighted.

//The function starts here
void search() {
  ifstream o1;
  o1.open("file"[B],[/B] ios::binary); // Comma missing here

  int flag = 0;
  char name[10]; //SEARCH VALUE

  cout << "Enter the element's name to be searched...";
  cin >> name;

  while ( o1.read((char*)&object, sizeof(object)) ) {
    if ( strcmpi(name, object.retname()) == 0 ) {
      //object.retname() is a function defined in class to return element's name
      flag = 1;
      cout << "\nElement found..." << object.retname();
      break;
    }
  }

  if (flag == 0)
    cout << "\nElement not found...";

  o1.close();
}

You are using strcmpi to compare the strings, by using this it will check for an exact match. To do what you want, you will have to manually read the name, to only show elements which match the first few letters you typed in.

Hope this helps.

commented: Good stuff +25
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.