私は現在、独学で C と C++ を学んでいます。このちょっとした「プログラムの練習」では、ユーザーに「i」か「d」のどちらかを入力して、整数と 10 進数 (浮動小数点数) のどちらを操作するかを尋ねます。ユーザーが間違った文字を入力したかどうかを伝えることで、ユーザーが「i」または「d」を入力するようにするwhileループがあります。何らかの理由で、while ループ内のプロンプトが getchar() に進む前に 2 回表示されます。この状況でボンネットの下で何が起こっているのかを理解するのに少し苦労しています。どんな助けでも大歓迎です。Xcode を使用しています (関連性があるかどうかはわかりません)。
#include <stdio.h>
int main()
{
char action;
printf("Please enter 'i' to work with integers or 'd' to work with decimals.\n\n");
scanf("%c", &action);
while (action != 'i' || action != 'd')
{
printf("You didn't entered the right command. Please try again (i/d)\n");
action = getchar()
}
}
したがって、出力として得られるのは次のとおりです。
You didn't entered the right command. Please try again (i/d)
You didn't entered the right command. Please try again (i/d)