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