相关疑难解决方法(0)

如何正确比较字符串?

我试图让一个程序让用户输入一个单词或字符,存储它,然后打印它,直到用户再次键入它,退出程序.我的代码看起来像这样:

#include <stdio.h>

int main()
{
    char input[40];
    char check[40];
    int i=0;
    printf("Hello!\nPlease enter a word or character:\n");
    gets(input);
    printf("I will now repeat this until you type it back to me.\n");

    while (check != input)
    {
        printf("%s\n", input);
        gets(check); 
    }

    printf("Good bye!");


    return 0;
}
Run Code Online (Sandbox Code Playgroud)

问题是我不断打印输入字符串,即使用户输入(检查)与原始(输入)匹配.我比较错误吗?

c string strcmp

168
推荐指数
6
解决办法
41万
查看次数

为什么我的两个元组包含以相同方式创建的字符串,而不是相等的?

我正在使用 Microsoft Visual C++ 将以下程序编译为 C++20 程序:

#include <iostream>
#include <tuple>

int main()
{
    auto t1 = std::make_tuple("one", "two", "three");
    auto t2 = std::make_tuple("one", "two", "three");
    
    std::cout << "(t1 == t2) is " << std::boolalpha << (t1 == t2) << "\n";
    std::cout << "(t1 != t2) is " << std::boolalpha << (t1 != t2) << "\n";

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我看到以下输出:

(t1 == t2) is false
(t1 != t2) is true
Run Code Online (Sandbox Code Playgroud)

元组是相同的,那么为什么它的比较结果是错误的呢?我该如何解决?

c++ tuples string-literals visual-c++

56
推荐指数
4
解决办法
3627
查看次数

标签 统计

c ×1

c++ ×1

strcmp ×1

string ×1

string-literals ×1

tuples ×1

visual-c++ ×1