2016-10-25 6 views
-2

これは、以下の私のコードです:構造体の値を印刷機能に渡し、それをメインで呼び出すにはどうすればよいですか? - C言語

enum DifficultyKind 
{ 
    Normal, 
    Hard, 
    Insane 
} DifficultyKind; 

typedef struct Target_Data 
{ 
    my_string name; 
    int hit_id; 
    int dollarvalue; 
    enum DifficultyKind difficulty; 
} Target_Data; 

enum DifficultyKind read_difficulty_kind (const char *prompt) 
{ 
int temp; 

enum DifficultyKind result; 
printf("%s\n", prompt); 

printf("\n"); 
printf("1: Normal Difficulty \n"); 
printf("\n"); 
printf("2: Hard Difficulty \n"); 
printf("\n"); 
printf("3: Insane Difficulty \n"); 
printf("\n"); 

temp = read_integer("Please make a selection between 1 and 3: \n"); 
if (temp < 1) { 
    printf("\n"); 
    printf("You did not make a selection between 1 and 3\n"); 
    printf("\n"); 
    temp = read_integer("Please make a selection between 1 and 3: \n"); 
} 

if (temp > 3) { 
    printf("\n"); 
    printf("You did not make a selection between 1 and 3\n"); 
    printf("\n"); 
    temp = read_integer("Please make a selection between 1 and 3: \n"); 
} 

result = temp - 1; 
return result; 
} 

Target_Data read_target_data (const char *prompt) 
{ 
Target_Data result; 
enum DifficultyKind Difficulty; 
printf("%s\n", prompt); 

result.name = read_string("Enter name: "); 

result.hit_id = read_integer("Enter hit ID: "); 
if (result.hit_id < 0) { 
    printf("Please enter a value of 0 or higher \n"); 
    result.hit_id = read_integer("Enter hit ID: "); 
} 

result.dollarvalue = read_integer("Enter $ value of target: "); 
if (result.dollarvalue < 0) { 
    printf("Please enter a value of 0 or higher \n"); 
    result.dollarvalue = read_integer("Enter $ value of target: "); 
} 

Difficulty = read_difficulty_kind("Please select the level of difficulty this bounty is from the below options:"); 

return result; 
} 

void print_target_data (Target_Data *toPrintData) 
{ 
    printf("\nDifficulty: %d, Target: %s, Hit ID: %i, $%i,\n", toPrintData->difficulty, toPrintData->name.str, toPrintData->hit_id, toPrintData->dollarvalue); 
} 

int main() 
{ 
    Target_Data *Target_Data; 
    read_target_data("Please enter the details of your bounty: "); 
    print_target_data(&Target_Data); 
} 

プログラムが実行され、詳細情報を入力した後、私は以下の取得:

Please enter the details of your bounty: 
Enter name: Jonathan 
Enter hit ID: 10 
Enter $ value of target: 500 
Please select the level of difficulty this bounty is from the below options: 

1: Normal Difficulty 

2: Hard Difficulty 

3: Insane Difficulty 

Please make a selection between 1 and 3: 
1 

Difficulty: 10, Target: , Hit ID: 0, $0, 

私は非常に多くの異なる方法を試してみましたが、すべて見てきました解決策はありますが、実際に何をすべきかはわかりません。

ヒットIDの入力番号として読みにくいのはなぜですか?その他の詳細は表示されません。

BountyHunter.c:96:20: warning: incompatible pointer types passing 
    'Target_Data **' (aka 'struct Target_Data **') to parameter of type 
    'Target_Data *' (aka 'struct Target_Data *'); remove & 
    [-Wincompatible-pointer-types] 
    print_target_data(&Target_Data); 
         ^~~~~~~~~~~~ 
BountyHunter.c:87:38: note: passing argument to parameter 'toPrintData' here 
void print_target_data (Target_Data *toPrintData) 

誰か助けてください。私はそれをコンパイルするとき

これも私が手警告メッセージです!

+1

(より適切にsscanfへの後続の呼び出しでfgetsへの呼び出しであるべき、それはまた別の日である)scanfに呼び出しをされていますデータを読み込むための構造体を割り当てられていません。構造体型へのポインタを割り当てていますが、初期化していません。あなたは構造体へのポインタへのポインタを、構造体へのポインタを期待する関数に渡します。コンパイラの警告に注意してください。それがあなたに指摘する問題を修正してください。その後、警告レベルを上げてください。 –

+0

'name.str'?これは、 'my_string'の型と、コードがどのようにしてコマンドエントリをどのくらい正確に読み込むかに依存します。 – Evert

+1

どこが 'read_target_data'ですか? –

答えて

0

あなたはあなたのコード内のエラーの膨大な数を持って、はるかに困難に作られましたあなたの読書を検証するのに必要なすべてのコードが不足していることを確認してください。つまり、私はread_target_dataにアドレスで渡された静的に宣言されたTarget_Dataを使用して、read_target_dataまたはread_difficulty_kindの中の構造体を動的に宣言する必要がなくなりました(現時点であなたの学習を複雑にしている可能性があります)

あなたがJavaから来たのか、Javaコードをコピーしようとしたのか分かりませんが、toPrintData->name.strは全く意味を持ちません。

また、DifficultyKindenumerr = -1を追加して、コード内で返される値を検証できるようにしました。これはどのように行うのですか。の各入力を検証して、実際の値で実際に作業していて、初期化されていない値を処理しようとしないようにする必要があります。

サイドノートとして、Cは、一般的に小文字変数名、JavaとC++のためのcamelCaseUpperCase名を残し、およびマクロのUPPERCASE名前を予約する、等...確かを使用しています(それは、スタイルですだからあなたに完全に委ねられています)

これは、私があなたの意向を保つと考えている再加工された例です。それ以上見て、それ以上の質問があれば教えてください。入力ルーチンは単にあなたは

#include <stdio.h> 
#include <stdlib.h> 

#define MAXC 128 

typedef enum { err = -1, Normal = 1, Hard, Insane } DifficultyKind; 

typedef struct { 
    char name[MAXC]; 
    int hit_id; 
    int dollarvalue; 
    DifficultyKind difficulty; 
} Target_Data; 

DifficultyKind read_difficulty_kind (const char *prompt) 
{ 
    int rtn, temp; 

    printf ("%s\n\n" 
      " 1: Normal Difficulty\n" 
      " 2: Hard Difficulty\n" 
      " 3: Insane Difficulty\n\n", prompt); 

    while ((rtn = scanf (" %d", &temp)) != 1 || (temp < 1 || temp > 3)) { 
     if (rtn == EOF) 
      return err; /* trap cancel of input (ctrl+d, ctrl+z) */ 
     fprintf (stderr, "error: invalid selection\n" 
         "Please make a selection between 1 and 3:\n\n" 
         " 1: Normal Difficulty\n" 
         " 2: Hard Difficulty\n" 
         " 3: Insane Difficulty\n\n"); 
    } 

    return temp; 
} 

Target_Data *read_target_data (Target_Data *result, const char *prompt) 
{ 
    int rtn; 

    printf ("%s\n\nEnter name: ", prompt); 
    while ((rtn = scanf (" %127[^\n]%*c", result->name) != 1)) 
     if (rtn == EOF) { 
      fprintf (stderr, "warning: input canceled, exiting.\n"); 
      exit (EXIT_FAILURE); 
     } 

    printf ("Enter hit ID: "); 
    while ((rtn = scanf (" %d", &(result->hit_id)) != 1) || 
      result->hit_id < 0) { 
     if (rtn == EOF) { 
      fprintf (stderr, "warning: input canceled, exiting.\n"); 
      exit (EXIT_FAILURE); 
     } 
     fprintf (stderr, "Please enter a value of 0 or higher \n"); 
     printf ("Enter hit ID: "); 
    } 

    printf ("Enter $ value of target: "); 
    while ((rtn = scanf (" %d", &(result->dollarvalue)) != 1) || 
      result->dollarvalue < 0) { 
     if (rtn == EOF) { 
      fprintf (stderr, "warning: input canceled, exiting.\n"); 
      exit (EXIT_FAILURE); 
     } 
     fprintf (stderr, "Please enter a value of 0 or higher \n"); 
     printf ("Enter $ value of target: "); 
    } 

    if ((result->difficulty = read_difficulty_kind ("Please select the" 
      " level of difficulty from the options below:")) == err) { 
     fprintf (stderr, "warning: input canceled, exiting.\n"); 
     exit (EXIT_FAILURE); 
    } 

    return result; 
} 

void print_target_data (Target_Data *toPrintData) 
{ 
    printf ("\nDifficulty: %d, Target: %s, Hit ID: %i, $%i,\n", 
      toPrintData->difficulty, toPrintData->name, 
      toPrintData->hit_id, toPrintData->dollarvalue); 
} 

int main (void) { 

    Target_Data Target_Data = { .name = "" }; 
    read_target_data (&Target_Data, 
        "Please enter the details of your bounty: "); 
    print_target_data (&Target_Data); 

    return 0; 
} 

使用例/出力

$ ./bin/difficultystruct 
Please enter the details of your bounty: 

Enter name: Some Funny Name 
Enter hit ID: 123 
Enter $ value of target: 234 
Please select the level of difficulty from the options below: 

1: Normal Difficulty 
2: Hard Difficulty 
3: Insane Difficulty 

0 
error: invalid selection 
Please make a selection between 1 and 3: 

1: Normal Difficulty 
2: Hard Difficulty 
3: Insane Difficulty 

4 
error: invalid selection 
Please make a selection between 1 and 3: 

1: Normal Difficulty 
2: Hard Difficulty 
3: Insane Difficulty 

2 

Difficulty: 2, Target: Some Funny Name, Hit ID: 123, $234, 
0

が完全にコードの構造を見て、推測、およびread_xxx機能がうまく実装されていると仮定すると、あなたのmainは次のようになります。

int main (void) 
{ 
    Target_Data Target_Data; 
    Target_Data = read_target_data("Please enter the details of your bounty: "); 
    print_target_data(&Target_Data); 
} 
+0

'read_target_data'関数は、ポインタではなく構造体を返します。 – mch

+0

私はそうすると、セグメンテーションフォルトが発生します:11詳細が印刷されるはずです。 – Noob

+0

@mchはい、そうです。私は編集しました。 – LPs

関連する問題