#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
    clrscr();
    char str[3][10], search[10];
    int i, t = 0, k;
    cout << "Enter 3 Names Of Fruit" << endl;
    for (i = 0; i < 3; i++)
        cin >> str[i];
    cout << "Enter Fruit Name To Search" << endl;
    cin >> search;
    for (i = 0; i < 3; i++)
    {
        if (search == str[i])   // if statement is not giving true
        {
            t++;
            k = i;
        }
    }
    cout << "The " << search << " is found " << t <<
        " times and is at position " << k << endl;
    getch();
}
1 回答
            0        
        
		
您必须使用cstring 中的std::strcmp:
完整代码:
#include<iostream>
#include<cstring>
using std::cout;
using std::endl;
using std::cin;
using std::strcmp;
int main() {
    char str[3][10], search[10];
    int i, t = 0, k;
    cout<<"Enter 3 Names Of Fruit"<<endl;
    for( i = 0; i < 3; i++ ) 
        cin>>str[i];
    cout<<"Enter Fruit Name To Search"<<endl;
    cin>>search;
    for( i = 0; i < 3; i++ ) {
        if( strcmp(search, str[i]) == 0)
        { 
            t++;
            k=i;
        }
    }
    cout << "The " << search << " is found " << t
        << " times and is at position " << k + 1 << endl;
    cin.get();
    return 0;
}
样品运行:
Enter 3 Names Of Fruit
banana mango orange
Enter Fruit Name To Search
mango
The mango is found 1 times and is at position 2
于 2013-09-11T01:25:45.350   回答