C++ cin 和 strnicmp 不工作

标签 c++ cin

void searchFlight(cust flights[] ,int row)
{    
    clrscr();
    cout << "Search for the flight you are looking for.\n";

    string airport;

    cout << "Enter Departing Flight : ";
    cin >> airport;   //error

    for (int r=0;r<row;r++)
    {
        if (strnicmp(airport, flights[r].airport[20], strlen(airport) ==0) //error  
        {
            clrscr();
            cout << flights[r].name[20] <<endl;
            cout << flights[r].airport[20] <<endl;
            cout << flights[r].destination[20] <<endl;
            cout << flights[r].ticket <<endl;
            cout << flights[r].passangers <<endl;
            cout << flights[r].opCost <<endl;
            cout << flights[r].income <<endl;
            cout << flights[r].netProfit <<endl;;
            pressKey();
        }
    }
    pressKey();
}

对于cin错误: error C2678: binary '>>' : 没有找到接受类型为 'std::istream' 的左手操作数的运算符(或者没有可接受的转换)

对于 strnicmp 错误: 错误 C2664:“strlen”:无法将参数 1 从“std::string”转换为“const char *”

我已搜索过此问题的解决方案,但无法解决。如果这里有类似的帖子可以解决我的问题,我深表歉意。

最佳答案

For the cin error: error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)

添加#include <string>到您的 CPP 文件。

For the strnicmp error: error C2664: 'strlen' : cannot convert parameter 1 from 'std::string' to 'const char *'

确认您有 #include <cstring>到您的 CPP 文件,并将您的调用替换为: strnicmp(airport.c_str(), flights[r].airport[20], strlen(airport.c_str()) ==0 .

我怀疑flights[r].airport[20]也是不正确的,但我不知道,因为你没有发布完整的程序。

如果cust::airport声明为 std::string airport; , 那么你需要 flights[r].airport.c_str() .

如果cust::airport声明为 char airport[20]; , 那么你需要 flights[r].airport .

关于C++ cin 和 strnicmp 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10192483/

相关文章:

c++ - c标准库的LuaBind绑定(bind)?

c++ - 使用 fstream 将文件中的一行读取到不同的变量中

c++ - cin.get() 获取太多

c++ - 为什么 stringstream >> 在失败时更改目标值?

c++ - 无字母循环

c++ - 为什么我们需要rbegin并撕裂?

c++ - 无法从类中访问全局交换函数

c++ - 评估字符串的 bool 函数

c++ - 快速将双指针数组转换为单指针,可能可以是连续的

c++ - 如何在不使用 getline() 的情况下接受不同行中的整数输入值?