0

I use a array to represent a table and I want to use "getchar" to update the value in the table.

 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);

  } 

My idea is that I should create a table and set all the value to be zero for input table first. Then change it base on the getchar(). At last, update the original table. However, I dont know how to use getchar to change the value. Can someone help me out? If there is something makes you confuse, leave a comment. Thank in advance. :)

user2185071
  • 187
  • 1
  • 6
  • 1
    `getchar()` doesn't change anything but the buffer behind `stdin`. It just *gets* a character. – Kninnug Jul 09 '13 at 01:33
  • @Kninnug I want to use the getchar() store the input then change the value. Is it possible? – user2185071 Jul 09 '13 at 01:36
  • @user2185071 what did stop you changing the value? – D.Pham Jul 09 '13 at 01:39
  • @user2185071 sure you can: `int c = getchar(); /* now use c somewhere */`. But that's probably not your problem. I think your actual problem is that you don't know where to put the value acquired through `getchar`. – Kninnug Jul 09 '13 at 01:42
  • @Kninnug You are right. I dont know hot to store the input(1,0,1,1) by using getchar. – user2185071 Jul 09 '13 at 01:46
  • getchar() just returns one character from the standard input stream (stdin) buffer as an integer. If you want to enter more than one character on a line, you'll need to read an entire line and parse it. Otherwise you'll need to indicate to the user inputting the data which row and column in the input table (array) they are currently entering the ONE character that getchar() is capable of reading. – CXJ Jul 09 '13 at 01:48
  • Why getchar() and not scanf()? – Ran Eldan Jul 09 '13 at 01:58
  • First of all use a debugger, because there seems problems in your code. – 0decimal0 Jul 09 '13 at 02:05
  • @CXJ That's what I want to know. How to use getchar to read the whole line(include newline character)? Can you give an example below? – user2185071 Jul 09 '13 at 02:11
  • @user2185071 I guess using it inside a loop itself will do the job. – 0decimal0 Jul 09 '13 at 02:16

1 Answers1

0

You can read an entire line using getchar() as illustrated in the answer to this question: getchar() and reading line by line

This is a non-trivial question. Here's some C code which will read a table in, subject to some limitations in line length and 10 rows maximum. There's also very little error checking. Each row is stored as one string, a line. You will have to parse the string for each row later in a loop to find each column's value. You might use strtok() or a regex() ("man 3 regex") to do that.

#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>

#define MAXLINE 1024
#define MAXROWS 10

int
main() {
    int inRows = 0;
    int inCols = 0;
    int i;
    int c;
    int line_length = 0;

    char rows[MAXROWS][MAXLINE];

    printf("How many rows in the input table?  ");
    scanf("%d", &inRows);
    getchar();  // get and throw away newline
    printf("How many columns in the input table?  ");
    scanf("%d", &inCols);
    getchar();  // get and throw away newline

    if (inRows < 1 || inCols < 1 || inRows > MAXROWS) {
        printf("Table dimensions of %d rows by %d cols not valid.\n", inRows, inCols);
        exit(1);
    }

    // read inRows lines of inCols each.
    for (i = 0; i < inRows; i++) {
        printf("Input table data for row #%d in the format col1 col2...\n", i);
        while ((c = getchar()) != '\n' && line_length < MAXLINE - 1) {
            rows[i][line_length++] = c;
        }
        rows[i][line_length] = 0;   // nul terminate the line
    }
}
Community
  • 1
  • 1
CXJ
  • 4,301
  • 3
  • 32
  • 62