0
我使用数组来表示一个表,我想使用“getchar”来更新表中的值。使用“getchar”更新数组中的值
Original table: 0 0 0 0 Input table: 1 0 Output table: 1 0 0 0
0 0 0 0 1 1 1 1 0 0
0 0 0 0 0 0 0 0
struct dimension {// represent the number of row and number of col of a table
int num_row;
int num_col;
};
void set_value(int t[],
const struct dimension *dim,
const int row,
const int col,
const int v) {
t[row*dim->num_col+col] = v;
}//update the value in a table
void update (int t[],
const struct dimension *table_dim,
struct dimension *input_dim) {
for (int k=0; k<(input_dim->num_row); k++){
for (int l=0; l<(input_dim->num_col); l++){
array[l] = getchar();
table_set_entry(array, input_dim, 0, 0,array[l]);
if (array[l] == '\n') break;
}
}
}
int main(void) {
int o[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
const struct dimension a = {3,4};
struct dimension in_dim = { 4, 5 };
update(o,a,in_dim);
}
我的想法是,我应该创建一个表,并将所有值设置为零的输入表第一。然后基于getchar()更改它。最后,更新原始表格。但是,我不知道如何使用getchar来更改值。有人可以帮我吗?如果有什么让你感到困惑,留下评论。预先感谢。 :)
'getchar()'不会改变'stdin'后面的缓冲区。它只是*获得*一个字符。 – Kninnug
@Kninnug我想使用getchar()存储输入,然后更改值。可能吗? – user2185071
@ user2185071什么阻止了您更改值? – DzungAh