Problem about search

Erm...don't know how to make the search function work correctly...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    string pname;
    char ans;
    do{
    cout << "\nPlease Enter A Player Name To Search: ";
    cin >> pname;
    for(unsigned int i =0 ; i < vplayer.size();i++){
    if(pname == vplayer[i].name){
    cout << "\nPlayer Name: " << vplayer[i].name  << endl;
    cout << "\nPlayer Age: " << vplayer[i].age << endl;
    cout << "\nPlayer Current Level: " << vplayer[i].level_op << endl;
    cout << "\nAirplane: " << vplayer[i].ach << endl;
    cout << "\nPlayer Current Result: " << vplayer[i].result << endl;
}

     else if(pname != vplayer[i].name)
        cout << "\nRecord not Found.";
    }
    cout << "Continue?(y/n)";
     cin >> ans;
     ans = tolower(ans);
    }while(ans == 'y');
    system("pause");
    }


actually this will work..but i don't know why the else if function will recall many times..
well, because that is what you told the compiler.

I think a proper indentation will help you figure out more easily what is wrong with your logic:

1
2
3
4
5
6
7
8
9
10
11
12
13
    for(unsigned int i =0 ; i < vplayer.size();i++){
        // there are only two possible excecution path
        if(pname == vplayer[i].name){
            cout << "\nPlayer Name: " << vplayer[i].name  << endl;
            cout << "\nPlayer Age: " << vplayer[i].age << endl;
            cout << "\nPlayer Current Level: " << vplayer[i].level_op << endl;
            cout << "\nAirplane: " << vplayer[i].ach << endl;
            cout << "\nPlayer Current Result: " << vplayer[i].result << endl;
        }

        else if(pname != vplayer[i].name)
            cout << "\nRecord not Found.";
    }
ya,after you advise me about my logic error...

1
2
3
4
5
6
7
8
9
10
11
12
13
  
    for(unsigned int i =0 ; i < vplayer.size();i++){
    if(pname == vplayer[i].name){
    cout << "\nPlayer Name: " << vplayer[i].name  << endl;
    cout << "\nPlayer Age: " << vplayer[i].age << endl;
    cout << "\nPlayer Current Level: " << vplayer[i].level_op << endl;
    cout << "\nAirplane: " << vplayer[i].ach << endl;
    cout << "\nPlayer Current Result: " << vplayer[i].result << endl;
}
}
     if(vplayer.empty())
        cout << "\nRecord not Found.";


i try to compiler again,but now the compiler skip the if(vplayer.empty()) function..
Last edited on
Topic archived. No new replies allowed.