ページ 11

教えてください☆

Posted: 2009年11月19日(木) 20:32
by cげん
以下のように実行されるプログラムを作成したのですが、本来は実行結果の例のように最後が「誕生日まであと2ヵ月ですね」と表示されなければいけないのですが、なぜか私が作成したプログラムでコンパイルすると、「誕生日まであと10カ月ですね」と表示されてしまいます。プログラムのどの部分を直せば、「誕生日まであと2ヵ月ですね」と表示されるのでしょうか!? 教えてください!!

【正常の実行結果の例】

<誕生の月>

月:0 【Enterを押す】

入力ミスです!

月:2 【Enterを押す】

<現在の月>

月:22 【Enterを押す】

入力ミスです!

月:12 【Enterを押す】

誕生日まであと2カ月ですね ←この部分です!!


【私が作成したプログラム】

#include<stdio.h>

void error_message(void);
int get_month(void);

int main(){

int birth,now,month;

while(1){

printf("<誕生の月>\n");
birth=get_month();

if(!(birth>=1 && birth<=12))
error_message();
else
break;
}
while(1){

printf("<現在の月>\n");
now=get_month();

if(!(now>=1 && now<=12))
error_message();
else
break;
}

month=now-birth;
if(month<0)
month=12+month;

printf("誕生日まであと%dヶ月ですね\n",month);

return 0;

}

void error_message(void){

printf("入力ミスです!\n");
}

int get_month(void){

int month;

printf("月:");
scanf("%d",&month);

return month;

}

Re:教えてください☆

Posted: 2009年11月19日(木) 20:57
by non
>month=now-birth;
おかしくないですか?