Function that prints only records containing B or b?

Hey guys,

I have a sample review test I am working on for an exam tomorrow, and came across a question that I am unsure on how to implement.

It is asking me to Implement the function called Print_Records_that_contain_B, that prints all the fields of a patient.

I am given:
void Print_Records_that_contain_B(); //prints all the patient records that contain a "b" or "B" in the name field.

I assume it is something similar to my search function:

1
2
3
4
5
6
7
8
9
10
11
12
int patient_info::Search(string Patient_name){
   int i;
   for(i=0;i<count;i++){
     if(patient_info[i].Patient_name == name){ //this somehow needs to find B/b
        cout << i << patient_info[i].Patient_name
             <<"\t"<<patient_info[i].age
             <<"\t"<<patient_info[i].service_charge;
     return i; //would have to increment count or i instead?
     }
   }
return (-1);
}


Please help. <3 I have not been taught this subject and would gladly appreciate seeing and understanding some code in which I can study for tomorrow.

Last edited on
Something along these lines:

if(patient_info[i].Patient_name.strFind('b') != string::npos || patient_info[i].Patient_name.strFind('B') != string::npos) { stuff }

2nd character before the end of your for() loop should be a ")" and not "_"
Last edited on
Ah yea, sorry about the _ I was retyping it from my notebook and I must of hit the wrong key.

And thank you very much,
The returns would stay the same then? No incrementing?
The question doesn't make a lot of sense to me. So you want a function that loops on the array and finds all patients with 'b' or "B" anywhere in the name?

Boris Yeltsin, Linda Bowers, Rob Roy, Kareem Abdul Jabar would all be found and their records printed correct? Sounds like a weird requirement but it shouldn't be too hard to adapt the search function to do that. It could be done in many ways. Take a shot at it and let us know what you are confused about.
I think you could use std::string::find_first_of()

1
2
3
4
if(patient_info[i].Patient_name.find_first_of("Bb") != std::string::npos)
{
    // print record
}


http://www.cplusplus.com/reference/string/string/find_first_of/
Last edited on
Okay sounds like this is what it should be.

1
2
3
4
5
6
7
8
9
10
void patient_info::Print_Records_that_contain_B(){
   int i;
   for(i=0;i<count;i++){
    if(patient_info[i].Patient_name.find_first_of("Bb") != std::string::npos){
        cout << i << patient_info[i].Patient_name
             <<"\t"<<patient_info[i].age
             <<"\t"<<patient_info[i].service_charge;
     }
   }
}


Thank you again. :)
Topic archived. No new replies allowed.