2013-10-18 100 views
1

我正在嘗試做一些練習編程,並且對於我來說,這是一個難題。檢查結構,如果不在結構中添加項目。 C

問題是我應該編寫一個程序,將採取「進入」並將其放置在結構中的汽車的品牌和型號,如果製造模型不在那裏,否則它什麼也不做。

這是我到目前爲止,我不斷收到錯誤:

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

在結構汽車定義後移動addCar函數的函數聲明 – 999k

回答

0

以下是我在查看代碼時發現的一些問題。

在這種情況下,我認爲最好是使用while循環而不是循環。

其次,如果你想確保你要添加的車並不在列表中存在,我建議你使用這個

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)++; 
    } 
} 
1

addCar你的函數原型的定義和用途「struct car」定義「struct car」之前。嘗試將結構定義移到addCar的原型上方。

+0

這是直接的主要問題。那麼會有另一個問題...在他[回答](http://stackoverflow.com/a/19441672/15168)中由[lightbringer](http://stackoverflow.com/users/2651994/lightbringer)診斷。 –

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

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

這是汽車結構的陣列,因此,你需要聲明你addCar功能如下

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

*製造和*模型代表2串。你有一個錯誤,當你有char * make [],它聲明一個字符串數組。

+0

你說得對,這個改變也是需要的。不過,這並不是正式問題所在。 –

+0

嗨,因爲他在主代碼中沒有寫任何東西,例如addCar函數。我只能幫助他消除他現在所犯的錯誤,以便他可以繼續。 – lightbringer

+0

@lightbringer我發佈了其餘的代碼,你認爲你能幫我弄清楚它現在有什麼問題嗎? –