954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Search array in string

hye..its kinda easy..but i still cant solve it..

1.Write a program that has an array of at least 10 string objects that hold student’s ids and names, you may make-up your own strings or use the following:

“1000614921, Abdulkadir Musa Jibrin” “1000717859, Abdulwahab Ali Esmail Al-Nawah” “1000716597, Ahmed Ebrahim Mohammed Al-Moay” “1000820803, Arash Moeenjahromi” “1000715628, Azaribe Antionette Doris” “1000717577, Fahmi Abdulqader Ahmed Ali” “1000818032, Fares Mohammed Mohammed Askn” “1000818087, Khairalah Fadhl Qasem Al-Galal” “1000715803, Liman Sadiq Musa” “1000820441, Lin Joo Foong”

The program should ask the user to enter name, partial name or student id to search for in the array. Any entries in the array that match the string entered should be displayed. For example, if the user enters “Mohammed” the program should display the following names from the list.

1000716597, Ahmed Ebrahim Mohammed Al-Moay 1000818032, Fares Mohammed Mohammed Askn


this is what I have done

#include <iostream>

#include <string>





using namespace std;

void search(string data[], string ayat)
{
    int j;
    
    for(j=0; j < 10; j++){
        if(data[j] == ayat)
            cout<<"output:";
            cout<<data[j] << endl;

        }
}




int main(){
string ayat;

 string data[10] =  {"1000614921, Abdul kadir Musa Jibrin",
             "1000717859, Abdul wahab Ali Esmail Al-Nawah",
             "1000716597, Ahmed Ebrahim Mohammed Al-Moay",
             "1000820803, Arash Moeenjahromi",
             "1000715628, Azaribe Antionette Doris",
             "1000717577, Fahmi Abdul qader Ahmed Ali",
             "1000818032, Fares Mohammed Mohammed Askn",
             "1000818087, Khairalah Fadhl Qasem Al-Galal",
             "1000715803, Liman Sadiq Musa",
             "1000820441, Lin Joo Foong"};


cout<<"Enter string: "<<endl;
getline(cin,ayat);


search(data, ayat);

return 0;
}
ekin5683
Newbie Poster
9 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 
if(data[j] == ayat)


This comparison will only work if the user enters the exact string that is in the data array. This is not what the question asks, you need to come up with a way to compare parts of(the entire student ID, or part of the student name) the data[j] string with ayat.

thelamb
Posting Pro in Training
426 posts since Aug 2008
Reputation Points: 193
Solved Threads: 75
 

can u give me more hint?im blank T_T

ekin5683
Newbie Poster
9 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

http://www.cplusplus.com/reference/string/string/

Scroll down to 'String operations:'.

You can use functions like find, compare etc.

thelamb
Posting Pro in Training
426 posts since Aug 2008
Reputation Points: 193
Solved Threads: 75
 

awwww..thank you..
im already solve it!Thanks thanks..

btw, im using find function :)

ekin5683
Newbie Poster
9 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

why i put the input "Mohammed", the output is the list of the names???

NurSakinah
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

sakina, mai i see your coding?

ekin5683
Newbie Poster
9 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: