【问题标题】:Searching for elements of a struct using pointers C++使用指针 C++ 搜索结构的元素
【发布时间】:2017-04-10 06:41:33
【问题描述】:

好的,所以标题是不言自明的,但最好的办法是查看代码;顺便说一句,忽略变量的名称。

我的创建和列表功能运行良好,它们是:

struct obras {
std::string nome[10];
std::string autor[10];
std::string ano[10];
std::string price[10];
}OBRA[10];
int counter=0;
struct obras *ptr=OBRA;



void inserir(struct obras *ptr) {   //CREATE
    system("CLS");

    printf("Insira o nome do autor da obra:\n");
    scanf("%s", ptr->autor);

    printf("\nInsira o nome da obra:\n");
    scanf("%s",ptr->nome);

    printf("\nInsira o ano em que foi feita a obra:\n");
    scanf("%s",ptr->ano);

    printf("\nInsira o preco da obra:\n");
    scanf("%s",ptr->price);

    return;
}



void inserir(struct obras *ptr) {
    system("CLS");

    printf("Insira o nome do autor da obra:\n");
    scanf("%s", ptr->autor);

    printf("\nInsira o nome da obra:\n");
    scanf("%s",ptr->nome);

    printf("\nInsira o ano em que foi feita a obra:\n");
    scanf("%s",ptr->ano);

    printf("\nInsira o preco da obra:\n");
    scanf("%s",ptr->price);

    return;
}

这是我的搜索尝试:

void pesquisar_autor(struct obras *ptr) {
            int k;
            std::string proc[10];
            system("CLS");
            printf("Insira o nome do autor:\n");
            scanf("%s\n");

            for (k=0;k<counter;k++) {
                if (ptr[k].autor==proc[10]) {
                    printf("\nOBRA %i:\n",k);
                    printf("Autor: %s\n", ptr[k].autor);
                    printf("Nome da obra: %s\n", ptr[k].nome);
                    printf("Ano em que foi feita a obra: %s\n", ptr[k].ano);
                    printf("Preco da obra: %s\n\n", ptr[k].price);
                }
            }
            system("PAUSE");
}    

请帮忙,我要睡觉了!

编辑::OKAY 所以我需要睡觉...这是搜索功能的声明,我知道这是错误的,但我不知道下一步该做什么:

                                    if (option==1) {
                                            pesquisar_obra(OBRA);
                                            system("PAUSE");
                                    }

【问题讨论】:

  • 您的结构变量都是 std::string 的数组,这可能不是您想要的。我怀疑您要么只想制作它们std::string,要么制作char数组,如:char nome[10]
  • if (ptr[k].autor==proc[10]) 当我尝试比较这两者时(我将所有结构变量更改为 char example[10] 和变量 proc ) ,我收到以下错误:ISO C++ forbids comparison between pointer and integer [-fpermissive]|
  • 标题不言自明。

标签: c++ pointers struct stdstring


【解决方案1】:

为什么你认为if (ptr[k].autor==proc[10]) 会起作用?您声明了 std::string proc[10]; 并且从未分配它。

编辑:

if (ptr[k].autor==proc[10])中,autorchar[],而proc[10]char,这不同,char[]char的数组。

在C中可以声明char proc[10],用scanf读取数据,最后用strcmp比较这两个字符串,不能用==

在 C++ 中,更好的方法是将结构中的所有变量声明为std::string,并使用cin 读取输入数据。另外proc也应该是std::string(使用cin),最后可以使用==比较这两个字符串。

很高兴为您提供帮助。

【讨论】:

  • 我的错... scanf("%s\n",proc[10]);
  • 而且,如果你声明了std::string proc[10],则意味着你可以使用proc[0]proc[9],你不能使用proc[10],它超出了数组索引。
  • 错误:不能通过'...'传递非平凡可复制类型'std::string {aka class std::basic_string}'的对象|
  • |error: no match for 'operator==' (operand types are 'std::string [10] {aka std::basic_string [10]}' and 'std::字符串 {aka std::basic_string}')|
  • 我认为你应该先了解charstd::string之间的区别,就像上面的评论所说的那样。
猜你喜欢
  • 1970-01-01
  • 2021-02-02
  • 2019-03-22
  • 2017-01-04
  • 1970-01-01
  • 2011-06-04
  • 1970-01-01
  • 2020-11-05
  • 2023-02-16
相关资源
最近更新 更多