我正在尝试用C语言编写一个程序,其中用户输入由空格分隔的定义数量的整数(在本例中为5个整数)。然后,输入被存储在一个int数组中,最后,它可以被存储在一个char数组中。
作为程序如何工作的示例,当程序要求输入时:
Input: 20 5 63 4 127程序的输出应该是:
Output: 20 5 63 4 127这就是我到目前为止所写的内容,但是我不知道如何将输入转换为int数组。请注意,我事先知道输入的长度(在本例中,如上所述,5个整数)。
// Input: 20 5 63 4 127
// Ask for user input.
// Store the input in this int array.
int input_int_array[5];
unsigned char char_array[5];
for(int i=0;i<5;i++)
{
char_array[i]=input_int_array[i];
printf("%d ", char_array[i]);
}
// Should print: 20 5 63 4 127发布于 2019-02-16 02:37:58
您可能希望使用scanf()将用户输入作为整数读取到int数组中
#include <stdio.h>
int main() {
int input_int_array[5];
// Ask for user input.
printf("input 5 numbers: ");
for (int i = 0; i < 5; i++) {
// Store the input into the array.
if (scanf("%d", &input_int_array[i]) != 1)
return 1;
}
// Output the contents of the array:
for (int i = 0; i < 5; i++) {
printf("%d ", input_int_array[i]);
}
printf("\n");
return 0;
}https://stackoverflow.com/questions/54714937
复制相似问题