-1

構造に関する課題のプログラムを作成しようとしています。アイデアは、姓名、電話番号、電子メール アドレスの変数を含む構造を作成することです。私のコードのほとんどは問題ないと思います。おそらく、C の最新のコーディング標準に比べて初歩的なものですが、クラス内で私がいるところです。

とにかく、電子メールアドレスフィールドを初期化しようとすると、5 行でコンパイルエラーが発生し、割り当てに互換性のない型があるというメッセージが表示されます。ただし、姓または名のフィールドでこれらのエラーは発生しません。理由もわかりません。

なぜこれが起こっているのか、またはプログラムの残りのエラーについての考えは大歓迎です! このコンパイル エラーが修正されるまで、残りの部分を実際に自分でデバッグすることはできないため、他に何が問題なのかはまだわかりません。

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

/*****************************************
Structure declaration, creating type cont
*****************************************/

typedef struct contact {
  char fname[20];
  char lname[20];
  int number[10];
  char email[30];
} cont;

/*****************************************
Start of main function
*****************************************/
int main() {

int iMenu; //variable required for the menu
int iStorage; //variable used to store array entry chosen by the user
int iEntry1, iEntry2, iEntry3, iEntry4, iEntry5 = 0; //variables used for flagging assigned entries
/*******************************************
because of the typedef declaration, the struct command 
isn't necessary in creating an instance of the structure.
*******************************************/
cont myContact[4]; 

/*******************************************
we initialize the arrays contained within the structures
*******************************************/

  strcpy(myContact[0].fname, "\0");
  strcpy(myContact[0].lname, "\0");
  myContact[0].number = 0;
  strcpy(myContact[0].email, "\0");
  strcpy(myContact[1].fname, "\0");
  strcpy(myContact[1].lname, "\0");
  myContact[1].number = 0;
  strcpy(myContact[1].email, "\0");
  strcpy(myContact[2].fname, "\0");
  strcpy(myContact[2].lname, "\0");
  myContact[2].number = 0;
  strcpy(myContact[2].email, "\0");
  strcpy(myContact[3].fname, "\0");
  strcpy(myContact[3].lname, "\0");
  myContact[3].number = 0;
  strcpy(myContact[3].email, "\0");
  strcpy(myContact[4].fname, "\0");
  strcpy(myContact[4].lname, "\0");
  myContact[4].number = 0;
  strcpy(myContact[4].email, "\0");


/*****************************************
Creation of the menu to allow the users
to add entries or view them
*****************************************/
while (iMenu != 3) {
printf("Please select one of the following menu options: \n");
printf("\n1. Input new entries into the phonebook");
printf("\n2. View entries stored in the phonebook");
printf("\n3. Exit the Program\n");
scanf("%d", &iMenu);

/*******************************************
First menu option allows the selection of which
entry, and the storage of phonebook data
********************************************/
  if (iMenu == 1) {
    printf("Please input the entry in the phonebook you wish to change (0-4): \n");
    scanf("%d", iStorage);
    printf("Please input the first name of your new contact: \n");
    scanf("%s", myContact[iStorage].fname);
    printf("Please input the last name of your new contact: \n");
    scanf("%s", myContact[iStorage].lname);
    printf("Please input the phone number of your new contact: \n");
    scanf("%d", myContact[iStorage].number);
    printf("Please input the e-mail address of your new contact: \n");
    scanf("%s", myContact[iStorage].email);

    /**************************************
    Nested if statement sets the variable to
    flag if an entry has been made
    **************************************/
    if (iStorage == 0)
      iEntry1 = 1;
    else if (iStorage == 1)
      iEntry2 = 1;
    else if (iStorage == 2)
      iEntry3 = 1;
    else if (iStorage == 3)
      iEntry4 = 1;
    else if (iStorage == 4)
      iEntry5 = 1;
  }

  /***************************************
  Menu option 2 allows the user to display
  stored phonebook entries, using the iEntry
  variables as flags to determine which ones
  to display
  ***************************************/
  else if (iMenu == 2) {
    if (iEntry1 == 1)
      printf("%s %s phone number: %d e-mail address: %s", myContact[0].fname, myContact[0].lname, myContact[0].number, myContact[0].email);
    if (iEntry2 == 1)
      printf("%s %s phone number: %d e-mail address: %s", myContact[1].fname, myContact[1].lname, myContact[1].number, myContact[1].email);
    if (iEntry3 == 1)
      printf("%s %s phone number: %d e-mail address: %s", myContact[2].fname, myContact[2].lname, myContact[2].number, myContact[2].email);
    if (iEntry4 == 1)
      printf("%s %s phone number: %d e-mail address: %s", myContact[3].fname, myContact[3].lname, myContact[3].number, myContact[3].email);
    if (iEntry5 == 1)
      printf("%s %s phone number: %d e-mail address: %s", myContact[4].fname, myContact[4].lname, myContact[4].number, myContact[4].email);
  }
  else if (iMenu > 3) {
    printf("Invalid Entry.");
  }
}


return 0;
}
4

2 に答える 2

3

あなたのコンパイラはほぼ確実にこれらの行について不平を言っています:

myContact[0].number = 0;
myContact[1].number = 0;
...

これらではありません:

strcpy(myContact[0].email, "\0");
strcpy(myContact[1].email, "\0");
...

struct contactnumberはそのフィールドがタイプであると宣言してint[10]いますが、それにシングルを割り当てようとしてintいます。

その他の一方的なアドバイス:

myContact次のように、配列をもっと簡単に初期化できます。

cont myContact[4] = { { { 0 } } };

集約型の一部 (配列、 astructなど) を初期化すると、コンパイラは残りのすべてのメンバーを自動的にゼロ初期化します。たとえば、次の場合:

char s[100] = "hello";

次に、 の最初の 5 バイトはs'h''e''l''l'となり'o'、残りの 95 バイトはそれぞれ値 0 になります。

int iEntry1, iEntry2, iEntry3, iEntry4, iEntry5 = 0;

これは初期化のみを行いiEntry5 = 0ます。 iEntry1..iEntry4初期化されていないままになっています。これはおそらく意図したものではありません。

入力プロンプトを出力するときは、後でを呼び出す必要がありますfflush(stdout)

また、使用しないでくださいscanfエラーが発生しやすく、正しく使用するのは困難です。特にバッファ オーバーフローに注意する必要があります。

于 2013-03-16T05:26:48.837 に答える
2

各連絡先には 10 個の番号 ( int number[10]、0 から 9 まで) があり、単純な のように割り当てますint number

myContact[0].number = 0;

また、これをしないでください: strcpy(myContact[0].fname, "\0");

文字列の末尾には常に暗黙の \0 があるため、おそらく "\0" ではなく "" が必要です。

(fname と lname が演習としてあることを願っています。世界中の多くの人は、「名 - 姓」のパラダイムに合わない名前を持っています)

于 2013-03-16T05:26:37.310 に答える