简体   繁体   中英

Strings comparison in C++

Somebody helped me that i should change the char[] to string , but I am still having problem comparing the strings imputed by the user. I am not sure if I have to use fstream or something else. getline did not really help me since it shows errors depending on what it is inside the getline.

void search (competitor competitors[], int broi)    
{
string country;
string name;
char choice;
bool flag;

do{ 

    cout << "\n\n Input Country: " << endl; 
    fstream country;    

    cout << " Input name: " << endl;

    fstream name;
    flag = false; 

    for(int i=0; i<count; i++) 
    {
        if( country==competitors[i].country && name==competitors[i].name)
        {
            cout << "found one" << endl;
            flag = true; 
        }
    }
    if (flag == false)  

    cout << " New search (Y/N)?: ";
    cin >> izbor;

}while(choice == 'Y' || choice == 'y');

}

fstream country;    

This declares a new variable named country of type fstream . Probably not what you want.

You should try:

getline(std::cin, country);

instead. And you have the same issue with name .

Also:

flag = true; 

better call break directly.

And:

cin >> izbor;

should probably be: cin >> choice;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM