2012年6月3日日曜日

開発環境

『実践プログラミング 第3版』 (Steve Oualline (著)、 望月 康司 (監修) (翻訳)、 谷口 功 (翻訳)、 オライリー・ジャパン、1998年、ISBN978-4900900646) の6章(条件文と制御文)6.8(どこにでも代入できることによる副作用)設問6-1を解いてみる。

設問6-1.

「==」とすべき箇所が代入「=」になっているから。

修正

コード(TextWrangler)

#include <stdio.h>
char line[80];
int balance_owed;
int main(){
  printf("Enter number of dollars dowed: ");
  fgets(line,sizeof(line),stdin);
  sscanf(line,"%d",&balance_owed);
  if(balance_owed == 0){
    printf("You owe nothing.\n");
  } else {
    printf("You owe %d dollars.\n",balance_owed);
  }
  return (0);
}

入出力結果(Terminal)

$ cc -g -o sample sample.c
$ ./sample
Enter number of dollars dowed: 10
You owe 10 dollars.
$ ./sample
Enter number of dollars dowed: 0
You owe nothing.
$

0 コメント:

コメントを投稿