2013-05-21 7 views
5
#include <stdio.h> 
#include <stdlib.h> 

int main() 
{ 
    int x = 1; 

    printf("please make a selection with your keyboard\n"); 
    sleep(1); 
    printf("1.\n"); 

    char input; 
    scanf ("%c", &input); 
    switch (input) { 
     case '1': 
      x=x+1; 
      printf(x); 
    } 

    return(0); 
} 

私は自分自身に変数を追加し、その変数を出力しようとしていますが、私のコードが動作しないようです。printf Cの変数

printf("%d\n", x); 

このreference pageprintfと関連する機能を使用する方法の詳細を与える:

私の出力誤差が

newcode1.c: In function ‘main’: 
newcode1.c:20:2: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [enabled by default] 
In file included from newcode1.c:1:0: 
/usr/include/stdio.h:362:12: note: expected ‘const char * __restrict__’ but argument is of type ‘int’ 
newcode1.c:20:2: warning: format not a string literal and no format arguments [-Wformat-security] 
+0

を扱う私にとって本当に有用であった、あなたは変数を印刷していない、あなたはあります変数の[現在の] *値*を出力します。 –

+0

また、 'scanf 'の前に' input'を初期化し、 'scanf'の結果をテストする方が良いです –

答えて

16

あなたprintfは、フォーマット文字列を必要としています。

+0

これは私がする必要があるのですか?ありがとうございました。 – Dave

2

Shafikが既に書いているように、scanfはあなたにチャーを与えるので、正しいフォーマットを使用する必要があります。 使用方法がわからない場合は、ここをクリックしてください。http://www.cplusplus.com/reference/cstdio/printf/

ヒント:x=x+1を書く方が速い/いいです。短い方法:x++;はちょうど答え彼にリンクを与えたいと思っているものに答えるため

申し訳ありません - サイトはすべての時間がC.ところで