C 打印功能不允许输入第一个值
我在运行代码时无法从键盘输入值。所有东西都编译成功,但当我运行代码时,它会像这样打印出来:C 打印功能不允许输入第一个值,c,printf,C,Printf,我在运行代码时无法从键盘输入值。所有东西都编译成功,但当我运行代码时,它会像这样打印出来: How many numbers would you like to place in the array: 7 Enter number 1: Enter number 2: 等等等等 为什么会这样?另外,是否有一种方法可以计算和存储每个元素在数组中的次数 int main(void) { int numbers = 0; int j = 0; char numStorag
How many numbers would you like to place in the array: 7
Enter number 1: Enter number 2:
等等等等
为什么会这样?另外,是否有一种方法可以计算和存储每个元素在数组中的次数
int main(void)
{
int numbers = 0;
int j = 0;
char numStorage[j];
int times = 0;
char newArray[j];
printf("How many numbers would you like to place in the array: ");
scanf("%d", &numbers);
j = numbers;
int i = 1;
while (i < (numbers + 1))
{
printf("Enter number %d: ", i);
scanf("%c", &numStorage[i]);
i++;
}//close of while loop
int x;
for (x = 0; x < numbers; x++)
{
newArray[x] = numStorage[x];
}//close of for loop
int z;
int q;
for (z = 0; z < numbers; z++)
{
for (q = 0; q < numbers; q++)
{
if (numStorage[z] == numStorage[q])
{
times++;
q++;
}//close of if
else
{
q++;
}//close of else
}//close of for loop
printf("\n%d occurs %d times", numStorage[z], times);
z++;
q = 0;
times = 0;
}//close of for loop
}//end of main method
int main(无效)
{
整数=0;
int j=0;
字符存储[j];
整数倍=0;
char-newArray[j];
printf(“您希望在数组中放置多少个数字:”);
scanf(“%d”和编号);
j=数字;
int i=1;
而(i<(数字+1))
{
printf(“输入编号%d:,i”);
scanf(“%c”、&numStorage[i]);
i++;
}//while回路的闭合
int x;
对于(x=0;x
中的
您可以将numStorage
声明为包含零元素的字符数组。
然后,在
int i=1;
while (i < (numbers + 1))
{
printf("Enter number %d: ", i);
scanf("%c", &numStorage[i]);
..
}
编辑 在您刚刚读取数字之后(再次使用
scanf
),使用scanf
读取字符也是一个问题,您应该检查该问题的解决方法。中
您可以将numStorage
声明为包含零元素的字符数组。
然后,在
int i=1;
while (i < (numbers + 1))
{
printf("Enter number %d: ", i);
scanf("%c", &numStorage[i]);
..
}
编辑
在您刚刚读取数字之后(再次使用
scanf
),使用scanf
读取字符也是一个问题,您应该检查该问题的解决方法。这也是一个问题,但主要问题是换行符落在数字之后,因此第一个%c
无需等待任何输入即可获取换行符。@JonathanLeffler注意。我忽略了这一点。这也是一个问题,但主要问题是换行符落在数字后面,因此第一个%c”
无需等待任何输入即可获得换行符。@JonathanLeffler注意。我忽略了那一点。。
j = numbers;
char numStorage[j];
...
int i=0;
while (i < numbers) # Array indices should be 0 to numbers-1