1

我正在尝试做一些练习编程,但对我来说,我遇到了一个难题。

问题是我应该编写一个程序,它将获取“输入”的汽车的品牌和型号并将其放置在结构中,如果制造模型不存在,否则它什么也不做。

这是我到目前为止所拥有的,并且我不断收到错误:

#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[])
{

}

表示addCar(unique, &count,...“参数类型struct car'不完整”的行,最后一行表示“addCar 的冲突类型

请各位大神指点一下好吗?

编辑:

好的,这就是我现在的代码,但我仍然无法让它工作。任何其他建议将不胜感激!

#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);
}
4

3 回答 3

2
struct car unique[10] = {{"Ford", "Explorer"}, {"Honda", "Civic"},

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

这是一个 struct car 数组,因此,您需要将 addCar 函数声明如下

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

*make 和 *model 代表 2 个字符串。当你有 char *make[] 时,你有一个错误,它声明了一个字符串数组。

于 2013-10-18T04:36:05.523 回答
1

在定义“ ”之前,您的函数原型addCar已定义并使用“ ”。尝试将结构定义移到.struct carstruct caraddCar

于 2013-10-18T04:31:51.317 回答
0

这是我在查看您的代码时发现的一些事情。

在这种情况下,我认为最好使用 while 循环而不是 for 循环。

其次,如果你想确保你要添加的汽车在列表中不存在,我建议你使用这个

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

它检查整个列表中是否存在具有相同品牌和型号的元素。这是我从您的代码中修改的代码。我希望它对你有帮助。

#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)++;
    }
}
于 2013-10-25T00:32:16.103 回答