2

これは私のプログラムの一部です。関数の呼び出しに問題があり、本当に助けが必要です。どちらかの機能を選択してデータを入力し、後でそのデータを印刷するのが基本です。何が間違っていますか?助けてください、私は取得し続けます

「[リンカ] への参照Customer_Record()'" , [Linker error] undefined reference to Car_Record()」および「ld は 1 つの終了ステータスを返しました」

 #include <stdio.h>
 #include <string.h>  
 #include <stdlib.h>
 #include <windows.h>  
 #include <conio.h>

void Customer_Record(), Car_Record();
int num;

struct Customer {
    char customer_ID[20];
    int license;
    char address[20];
    int phone;
    char email[20];
} cust;

struct car {

    int regno[20];
    char model[20];
    char colour[10];
} car;


main() {
    printf("               Enter 1 to go to Customer Record \n\n");
    printf("               Enter 2 to go to Car Record \n\n");
    scanf("%d", &num);
    if (num = 1) {
        Customer_Record();
    } else if (num = 2) {
        Car_Record();
    } else {
        printf("Invalid Number");
    }

    system("cls");

    void Customer_Record(); {
        printf("********CUSTOMER RECORD********"); /* accepts into*/
        printf("\nEnter the name of the customer ");
        scanf("%s", &cust.customer_ID);
        printf("Enter the license number of the customer ");
        scanf("%d", &cust.license);
        printf("Enter the address of the customer ");
        scanf("%s", &cust.address);
        printf("Enter the cell phone number of the customer ");
        scanf("%d", &cust.phone);
        printf("Enter the email address of the customer ");
        scanf("%s", &cust.email);
    }

    void Car_Record(); {
        printf("********CAR RECORD********");
        printf("\nEnter the car's registration number ");
        scanf("%d", &car.regno);
        printf("Enter the car's model ");
        scanf("%s", &car.model);
        printf("Enter the colour of the car ");
        scanf("%s", &car.colour);
    }
    getchar();
    getchar();
}
4

3 に答える 3

6

そのように関数をネストしないでください。との定義は のにある必要がCustomer_Record()あります。これらの関数の定義も削除する必要があります。Car_Record()main();

コードをより適切にフォーマットしてみてください。これは、長期的には非常に役立ちます。

于 2013-01-24T23:33:59.030 に答える
2
  1. main の最後に } がありません。コンパイラは、関数宣言が main 関数内にあると見なします。

  2. 関数から末尾のセミコロンを削除します。元:

    void Car_Record();
    {   
    

     void Car_Record()
     {   
    

そのセミコロンは必要ありません。

于 2013-01-24T23:37:00.460 に答える
1

あなたのプログラムのすべての問題のリストをまとめました。ここにあります:

  • if ステートメントは=、比較演算子を使用する必要があるときに代入演算子を使用しています==。たとえば、次のように変更します。

    if (num = 1) {
    

    if (num == 1) {
    

    elseこれはあなたの声明でも発生します。

  • main 内で関数を定義することも正しくありません。これらのブロックは、メイン句の外で定義する必要があります。main の上で既に関数のプロトタイプを作成したので、次に main の下でそれらを定義する必要があります。さらに、関数を定義するときは、パラメーター リストの後にセミコロンを付けるべきではありません。それは構文的に間違っています。

以下アドバイスです。このコードを C++ としてコンパイルしていますが、これは C 関数/ヘッダーを使用して記述されています。C++ に変換するには、次の変更を行います。

  • ヘッダーを変更します: stdio.h、conio.h、stdlib.h; これらはすべて C スタイルのヘッダーです。基本的に、「.h」で終わるヘッダーはすべて C スタイルのヘッダーです。C++ には独自の I/O ライブラリがあるため、C に相当するものは廃止されています。代わりに次のヘッダーを含めます。

    #include <iostream>
    #include <cstdlib>
    

    printf/scanfとのみを使用しているように見えるため、追加systemのヘッダーを省略しました。これは、C++iostreamcstdlibヘッダーが既に所有しているものと同等です。たとえば、std::coutiosteamstd::cinの場合。そして同等のものgetcharは std::cin.get()` です。

  • メインは int を返します:標準 C++ では、戻り値の型を省略できません。main の戻り値の型を として指定しますが、最後intに置く必要はありませんreturn 0(これは暗黙的です)。

std::coutC++ 関数とコンテナー ( /などstd::cin)を調べたい場合は、このリファレンスが大いに役立ちます。

于 2013-01-25T01:58:57.120 に答える