C言語で単純なプログラムを試してみたところ、配列に挿入していました。 文字を受け入れるのにscanf
関数を使用しましたが、コンパイラはそれをスキップしてプログラムの最後に行ったようです。 これは私が使用するコードであった: -文字でスキャン機能が動作しない
#include <stdio.h>
void main()
{
int a[50], i, j, m, n, x;
char ch;
printf("Enter the no. elements of the array :- ");
scanf("%d", &m);
printf("Enter the elements below :- ");
for (i = 0; i < m; i++)
{
scanf("%d", &a[i]);
}
printf("The array is :- \n");
for (i = 0; i < m; i++)
{
printf("%d", a[i]);
}
printf("\nDo you want to enter an element ? (Y/N)\n");
scanf("%c", &ch); // The compiler just skips this along with the
while (ch == 'y' || ch == 'Y') // while loop and goes straight to the printf
{ // statement
printf("The index of the element :- ");
scanf("%d", &n);
printf("\nEnter a number :- ");
scanf("%d", &x);
for (i = m; i > n; i--)
{
a[i] = a[i - 1];
}
a[n] = x;
printf("\nInsert more numbers ? (Y/N)");
scanf("%c", &ch);
m = m + 1;
}
printf("\nThe array is :- ");
for (i = 0; i < m; i++)
{
printf("%d", a[i]);
}
}
をI、すなわちY
又はN
要素を挿入するか否かを、ユーザが選択を有することを可能にするために可変ch
を用います。
しかし、コンパイラは基本的にchar
を受け入れる第3のscanf
関数と、while
ループをスキップします。
scanf
機能がスキップされた理由を知りたいだけですか?
scanf( "%c"、&ch); ')に' scanf( "%c"、&ch); 'を'〜に変更してバッファに残った改行を消費してください。他の形式とは異なり、 '%c'は自動的に入力バッファ内の空白をスキップしません。 –
[Scanfスキップスキャン文字]の複製があります(http://stackoverflow.com/questions/36281871/scanf-skip-scanning-character) – EOF
....よくインデントされたコードを投稿してください... – LPs