简体   繁体   English

检查结构,添加项目(如果尚未在结构中)。 C

[英]Check Structure, add items if not already in structure. C

I'm trying to do some practice programming and I've come to a, for me, a difficult problem. 我正在尝试做一些练习编程,对我来说,这是一个难题。

The problem is I'm supposed to write a program the will take the make and model of a car that was "entered" and place it in the structure, if the make an model are not there, otherwise it does nothing. 问题是我应该编写一个程序,它将采用“输入”的汽车的品牌和型号,并将其放置在结构中,如果制造模型不存在,否则它什么都不做。

This is what I have so far, and I keep getting errors: 这是我到目前为止,我一直在收到错误:

#include <stdio.h>
#include <string.h>

void addCar(struct car u, int* count,  char *make[], char *model[]);

struct car { char make[30]; char model[30]; };

int main(void)
{
    struct car unique[10] = {{"Ford", "Explorer"}, {"Honda", "Civic"},

        {"Honda", "Accord"}, {"Chevy", "Malibu"}};

    int i, count = 4;

    addCar(unique, &count, "Ford", "Mustang");
}

void addCar(struct car u, int* count, char *make[], char *model[])
{

}

The line that says addCar(unique, &count,... it's saying "Argument type ' struct car' is incomplete" and the last line says "conflicting types for addCar " addCar(unique, &count,...的行addCar(unique, &count,...它说“参数类型' struct car'不完整”,最后一行表示“addCar的冲突类型

Could you all give me a few pointers, please? 你能给我一些指示吗?

EDIT: 编辑:

Okay, here is what my code is now, but I still can't get it to work. 好的,这是我的代码现在,但我仍然无法让它工作。 Any other suggestions would be appreciated! 任何其他建议将不胜感激!

#include <stdio.h>
#include <string.h>

struct car { char make[30]; char model[30]; };

void addCar(struct car *u, int *count,  char *make, char *model);

int main(void)
{
    struct car unique[10] = {{"Ford", "Explorer"}, {"Honda", "Civic"},

        {"Honda", "Accord"}, {"Chevy", "Malibu"}};

    int i, count = 4;

    printf("%s", unique[0].make);

    addCar(unique, &count, "Ford", "Mustang");

}

void addCar(struct car *u, int *count, char *make, char *model)
{
    int i = 0;

    for (i; i < *count; i++)
    {
        if ((u[i].make != make) && (u[i].model != model))
        {
            strcpy(u->make, make);
            strcpy(u->model, model);
            count++;
        }
    }
    for (i = 0; i < *count; i++)
        printf("%s, %s", u[i].make, u[i].model);
}
struct car unique[10] = {{"Ford", "Explorer"}, {"Honda", "Civic"},

        {"Honda", "Accord"}, {"Chevy", "Malibu"}};

This is an array of struct car, therefore, you need to declare your addCar function as follow 这是一个struct car数组,因此,您需要声明您的addCar函数如下

void addCar(struct car *u, int *count, char *make, char *model)

*make and *model represent 2 strings. * make和* model代表2个字符串。 You have a mistake when you have char *make[], which declares an array of string. 当你有char * make []时会出错,它声明了一个字符串数组。

Your function prototype for addCar is defined and uses ' struct car ' before you define ' struct car '. 定义了addCar函数原型, addCar在定义' struct car '之前使用' struct car '。 Try moving the structure definition above the prototype for addCar . 尝试将结构定义移动到addCar的原型addCar

Here are a few things that I figured out when looking at your code. 以下是我在查看代码时想到的一些事项。

In this case, I think it's best if you use while loop rather than for loop. 在这种情况下,我认为最好使用while循环而不是循环。

Secondly, if you want to make sure that the car you want to add does not exist in the list, I suggest you use this 其次,如果你想确保你想要添加的汽车在列表中不存在,我建议你使用它

while (i < *size) {
    if ((u[i].make == make) && (u[i].model == model)) { break; }        // check if the car is already in the list
    i++;
}

It checks the whole list for the existence of an element with the same make and model. 它检查整个列表中是否存在具有相同品牌和型号的元素。 Here is the code I modified from yours. 这是我修改的代码。 I hope it helps you. 我希望它对你有所帮助。

#include <stdio.h>
#include <string.h>

struct car { char make[30]; char model[30]; };

void addCar(struct car *u, int *count,  char *make, char *model);

int main(void)
{
    struct car unique[10] = {{"Ford", "Explorer"}, {"Honda", "Civic"},

        {"Honda", "Accord"}, {"Chevy", "Malibu"}};

    int i, count = 4;

    addCar(unique, &count, "Ford", "Mustang");
    for (i = 0; i < count; i++) {
        printf("%s, %s\n", unique[i].make, unique[i].model);
    }
}

void addCar(struct car *u, int *size, char *make, char *model)
{
    int i = 0;

    while (i < *size) {
        if ((u[i].make == make) && (u[i].model == model)) { break; }        // check if the car is already in the list
        i++;
    }
    if (i == *size) {                     // car found
        struct car c;
        strcpy(c.make, make);
        strcpy(c.model, model);
        u[i] = c;
        (*size)++;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM