0

我想编写一个程序,从用户那里获取大约 4 个字符串,然后要求用户键入一个字符串,以查看该字符串是否存在于已输入的这 4 个字符串之一中。请您纠正我的错误。谢谢。 (我使用开发 C++)

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int main()
{   
    int x,y,i;
    string z[40],a;
    for (int i=0;i<4;i++)
    {
        cout<<"type 4 strings";
        cin>>z[i];
     }
        cout<<"type a string to search ";
        cin>>a;
    for (int i=0;i<4;i++)
    {   if(strcmp(z[i],a)==0)
        cout<<z[i];
        else
        cout<<"error";
    }
    getch ();   
    return 0;   
}
4

1 回答 1

5

您不能将 C 风格的字符串函数 ( strcmp) 与 C++混合使用std::string- 将 C++std::string类型与常规类型进行比较可以正常工作if (str1 == str2) ...,因此无需在strcmp此处使用。

于 2013-07-18T00:46:45.787 回答