2016-04-10 21 views
-2

これは、曲とその情報をデータベースに入力するために作成する必要がある大きなプログラムの一部です。私はstructsを使うのが初めてで、私の教授のhasntは構造体を関数に渡し、mainの内部でそれらを使う方法をまだ教えてくれませんでした。私はまだポインタを使用するはずです。コンパイル時に多くのエラーが発生し、どこから開始するのか不明です。構造体と配列をC言語で使用する

#include <stdio.h> 

typedef struct mp3song_struct { 
    char title[40]; 
    char artist1[20]; 
    char artist2[20]; 
    char artist3[20]; 
    int datemonth; 
    int dateday; 
    int dateyear; 
    char genre[10]; 

}mp3song; 

void populate(mp3song totalsongs[30]); 

int main() { 

struct mp3song totalsongs[30]; 

populate(mp3song totalsongs); 


} 

void populate(mp3song totalsongs[30]){ 

int i = 0; 

for(i = 0; i < 5; ++i){ 
    printf("Enter song title: \n"); 
    scanf("%c", &totalsongs[i].title); 
    printf("Enter Artists(If no more than 1 enter \"none\")"); 
    printf("Enter artist: \n"); 
    scanf("%c", &totalsongs[i].artist1); 
    printf("Enter artist: \n"); 
    scanf("%c", &totalsongs[i].artist2); 
    printf("Enter artist: \n"); 
    scanf("%c", &totalsongs[i].artist3); 
    printf("Ente date mm/dd/yyyy\n"); 
    printf("Enter month: \n"); 
    scanf("%d", &totalsongs[i].datemonth); 
    printf("Enter day: \n"); 
    scanf("%d", &totalsongs[i].dateday); 
    printf("Enter year: \n"); 
    scanf("%d", &totalsongs[i].dateyear); 
    printf("Enter genre: \n"); 
    scanf("%c", &totalsongs[i].genre); 
} 
} 
+0

溶液を用いて問題のコードを変更しないでください、それが答えとシンであること、そしてません。あなたの質問の説明 –

答えて

1

コードにはいくつかの問題があります。整数を返さないので、int populate(struct mp3song, struct mp3song totalsongs[30]);void populate(struct mp3song, struct mp3song totalsongs[30]);と宣言できます。

mp3songは配列ではないので、& mp3song [i]のように添え字を付けることはできません。実際に値を設定する関数は、値ではないタイプのため、struct mp3songを受け取ることはできません。したがって、populate関数行int populate(struct mp3song totalsongs[30]);を変更し、&mp3song[i]の出現をすべて&totalsongs[i]に置き換えると、配列内の入力を取ることができます。

+0

私は、コードを編集し、夫婦、他の修正を行って[OK]をエラーメッセージに基づいて、上記を参照してください。しかし、まだ残っている$ gcc hw8.c hw8.c:関数 'メイン'で: hw8.c:19:16:エラー:配列型に不完全な要素型があります 構造体mp3song totalsongs [30]; ^ hw8.c:21:10:エラー: 'mp3song'の前に予期される式 populate(mp3song totalsongs); ^ –

+0

'populate(struct mp3song totalsongs); – user902384

0
  1. mp3songあなたが構造を渡すために、その構造型の変数を宣言する必要がstructure nameないstructure variable
  2. です。

例:あなたのケース あなたは

struct mp3song totalsongs[30]; 
populate(totalsongs[30]); 

関数定義

<return_type int or void> populate(struct mp3song totalsongs[30]); 

をstruct_nameだけではなく変数を渡すことができて

struct definition 
struct <struct_name>{ 

Struct elements; 
}<struct variable>; 


struct <struct_name> <struct_variable>; 

今、あなたは変数と構造要素にアクセスすることができます〜への名前関数内のタルソン(talsongs)。

あなたの

modifedコード

#include <stdio.h> 

typedef struct mp3song { 
    char title[40]; 
    char artist1[20]; 
    char artist2[20]; 
    char artist3[20]; 
    int datemonth; 
    int dateday; 
    int dateyear; 
    char genre[10]; 

}; 

void populate(struct mp3song totalsongs[30]); 

int main() { 

struct mp3song totalsongs[30]; 

populate(totalsongs); 


} 
void populate(struct mp3song totalsongs[30]){ 

int i = 0; 

for(i = 0; i < 5; ++i){ 
    printf("Enter song title: \n"); 
    scanf("%s", &totalsongs[i].title); 
    printf("Enter Artists(If no more than 1 enter \"none\")"); 
    printf("Enter artist: \n"); 
    printf("TITLE: : %s \n",totalsongs[i].title); 
    scanf("%c", &totalsongs[i].artist1); 
    printf("Enter artist: \n"); 
    scanf("%c", &totalsongs[i].artist2); 
    printf("Enter artist: \n"); 
    scanf("%c", &totalsongs[i].artist3); 
    printf("Ente date mm/dd/yyyy\n"); 
    printf("Enter month: \n"); 
    scanf("%d", &totalsongs[i].datemonth); 
    printf("Enter day: \n"); 
    scanf("%d", &totalsongs[i].dateday); 
    printf("Enter year: \n"); 
    scanf("%d", &totalsongs[i].dateyear); 
    printf("Enter genre: \n"); 
    scanf("%c", &totalsongs[i].genre); 
} 
} 
+0

コンパイラは、mp3song totalsongs [30]がvoid populate(struct mp3song totalsongs [30])の構造体として宣言されているようなことはしません。また、エラーのエラーが続いています:配列型に不完全な要素型があります 構造体mp3song totalsongs [30]; ^ –

+0

@JoeRiefskiは自分の答えで共有したコードを使用して試してみます。 –

+0

OKですが、データを入力すると日付の入力がスキップされ、ループが再び開始されます。 –

関連する問題