我不能为我的生活弄清楚为什么这不起作用。这是我在问题中的函数代码:
int searchDisplay()
{
vector<Client> store;
char nameSearch[SIZE];
string input;
Client foo;
int i = 0;
int k = 0;
bool flag = false;
fstream customer("customer.dat", ios::in | ios::out | ios::binary);
if (!customer)
{
cout << "Error opening file. Program aborting." << endl;
return 0;
}
while (!customer.eof())
{
store.push_back(foo);
customer.read(reinterpret_cast<char *>(&store[i]),
sizeof (store[i]));
i++;
}
cin.ignore();
cout << "User, please enter the full name of the client you wish to search for:
";
getline(cin, input);
strcpy(nameSearch, input.c_str());
if (strcmp(nameSearch, store[0].name))
{
cout << "FINALLY!" << endl << endl;
}
customer.close();
return 1;
enter code here
}
我的问题在于
if (strcmp(nameSearch, store[0].name))
{ cout << "FINALLY!" << endl << endl; }
不会将nameSearch
与store[0].name
中的名称进行比较。
我正在使用此方法,但要注意if (nameSearch == store[0].name)
也不起作用。
如果“Frank Palmasani”通过用户输入存储在nameSearch
中,并且“Frank Palmasani”通过读取文件存储在store[0].name
中,它仍然不会以{{1}的形式返回值。
我尝试使用=
来获取cin
的输入,这也没什么好运。我似乎无法让这个工作。我不确定它是否与二进制有关,因为我使用二进制作为我的写作和阅读方法。
我对c字符串的练习很少,所以我想知道是否有一些关键的东西我要离开这个程序?
谢谢,如果你能提供帮助!这实际上是我和这个项目之间唯一的事情。
答案 0 :(得分:0)
ALRIGHT!得到它了!好像我错过了strcmp()
返回0
的情况,如果为真,1
如果为假。所以为了解决这个问题,我对此进行了比较:
end = strcmp(nameSearch, store[0].name);
if (end == 0)
{
cout << "They are equivalent." << endl;
}
希望这有助于遇到类似问题的任何人!