gpt4 book ai didi

c++ - strcmp() 的问题没有正确比较字符串

转载 作者:太空宇宙 更新时间:2023-11-04 15:44:20 25 4
gpt4 key购买 nike

这是实际的代码,因为它似乎特定于此处的某些内容。

#include <iostream>
#include <string.h>

using namespace std;

int main()

cout << " Just say \"Ready\" when you want to start.";
char tempReady[20];
cin >> tempReady;
length = strlen(tempReady);
char* ready = new char[length+1];
strcpy(ready, tempReady);
while((strcmp(ready, "Ready")||strcmp(ready, "ready"))!=0)
{
cout << "Try again.";
cin >> tempReady;
length = strlen(tempReady);
delete[] ready;
ready = new char[length+1];
strcpy(ready, tempReady);
}
cout << "Success";

有人看到什么不对吗?

最佳答案

C 风格的方法:

char str[256];
if (scanf("%255s", str) == 1 && strcmp(str, "hello") == 0) {
printf("success");
}

C++ 方法:

std::string str;
if (std::cin >> str && str == "hello") {
std::cout << "success";
}

现在决定是用 C 还是 C++ 编写代码,只是不要混用

关于c++ - strcmp() 的问题没有正确比较字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19072188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com