2012-01-30 9 views
5

Ok、私はC言語のノブですが、簡単です。このプログラムは、大学の割り当てのためのもので、その中にisdigit()関数があるはずです。ここでは、コードGCCコンパイルエラー:フォーマット '%c'は 'char *'型の引数を期待していますが、引数2の型は 'int' [-Wformat]です。

//by Nyxm 
#include <stdio.h> 
#include <ctype.h> 

main() 
{ 
    char userChar; 
    int userNum, randNum; 
    srand(clock()); 
    printf("\nThis program will generate a random number between 0 and 9 for a user to guess.\n"); 
    /*I changed it from '1 to 10' to '0 to 9' to be able to use the isdigit() function which 
    will only let me use a 1 digit character for an argument*/ 
    printf("Please enter a digit from 0 to 9 as your guess: "); 
    scanf("%c", userChar); 
    if (isdigit(userChar)) 
    { 
      userNum = userChar - '0'; 
      randNum = (rand() % 10); 
      if (userNum == randNum) 
      { 
        printf("Good guess! It was the random number.\n"); 
      } 
      else 
      { 
        printf("Sorry, the random number was %d.\n", randNum); 
      } 
    } 
    else 
    { 
      printf("Sorry, you did not enter a digit between 0 and 9. Please try to run the program again.\$ 
    } 
} 

私がコンパイルしようとすると、私は

week3work1.c: In function ‘main’: 
week3work1.c:14:2: warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat] 

次のエラーを取得する地球上の何が起こっているか?ですか私は助けが切望されています。どんな助けでも。私は真剣にこのプログラムをあきらめようとしています。私の教科書が "%c"が正規の 'char'のものであることを示すとき、なぜ 'char *'の引数を期待しているのですか?私はnano、gcc、Ubuntuを使用しています。

+2

'gcc'はあなたのプログラムに対して複数の警告を生成します。あなたは* all *を修正する必要があります。たとえば '#include '( 'srand()'と 'rand()')と '#include '( 'clock()'に必要です)がありません。 –

+0

@KeithThompsonコンパイル時にエラーがなくなり、今度はプログラムが正常に動作します。だから、私はそれが動作し続けるかどうか尋ねていると思いますか?私の教科書では、必要なプリプロセッサ文だけが '#include 'で、 '#include 'はisdigit()関数用です。 – Nyxm

+0

これらの関数を呼び出す場合は、上記のヘッダーが必要です。あなたがそれらを使用せずに立ち去ることができる状況がありますが、私は詳細には入りません。 '#include'ディレクティブを追加するだけです。 '-std = c99 -pedantic'を使うとgccは関数呼び出しについて警告します。どの教科書を使っていますか? '#include 'を使わずに 'rand()'を呼び出す、または '#include 'を使わずに 'clock()'を呼び出す例を示していれば、教科書は間違っています。 –

答えて

11

charが値によって渡されると述べているので、あなたは、それ以外の場合は、文字を格納するためにどのような方法を持っていない、charへのポインタを渡す必要があります。だから代わりに&userCharが必要です。

たとえば、コール前にuserChar0であるとします。あなたの現在のコードを使用すると、基本的には(ユーティリティが行く限り)、これをやっている:

scanf("%c", 0); 

は何が欲しいのはこれです:

scanf("%c", some-location-to-put-a-char); 

&userCharはどれ。

scanfためmanページには、これを言及:

c  Matches a sequence of characters whose length is specified by 
      the maximum field width (default 1); the next pointer must be a 
      pointer to char, and there must be enough room for all the char‐ 
      acters (no terminating null byte is added). The usual skip of 
      leading white space is suppressed. To skip white space first, 
      use an explicit space in the format. 
+0

良い悲しみ。私は '&'を忘れたとは信じられません。私は馬鹿のように感じる。結果に直接コメントします。 – Nyxm

+0

完璧に動作します。大変ありがとうございました。私は数日間このプログラムに苦労しています。最初はint変換のcharで、今はこれです。もう一度、ありがとう! – Nyxm

+0

+1値の実際の拡大で値渡しを説明する素晴らしい方法。 –

1

scanf("%c", userChar);scanf("%c", &userChar);に置き換えます。 scanf()については

0

あなたが代わりのcharの値のポインタに渡す必要があります。

scanf("%c",&userChar); 
関連する問題