教えてください☆

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
cげん

教えてください☆

#1

投稿記事 by cげん » 15年前

以下のように実行されるプログラムを作成したのですが、本来は実行結果の例のように最後が「誕生日まであと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;

}

non

Re:教えてください☆

#2

投稿記事 by non » 15年前

>month=now-birth;
おかしくないですか?

閉鎖

“C言語何でも質問掲示板” へ戻る