Like Tree7Likes
  • 3 Post By Matticus
  • 1 Post By Click_here
  • 1 Post By christop
  • 1 Post By c99tutorial
  • 1 Post By Click_here

error checking

This is a discussion on error checking within the C Programming forums, part of the General Programming Boards category; After finally completing my code. I am trying to add some standard errors. One of the errors I want to ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    106

    error checking

    After finally completing my code. I am trying to add some standard errors. One of the errors I want to do is if the number of entries in matrix that user inputs exceeds the actual number their should be than an error should pop up. This is actually quite simple and I have done it a couple of times but it is not working this time. The error is in my matrix_fill function. Only posting a bit of my code since its very long.
    Code:
    #include<stdio.h>
    #include <stdlib.h>
    
    void matrix_display(int **A, int row_size1, int col_size1);
    int **matrix_alloc(int row_size1, int col_size1);
    void matrix_fill(int **A, int row_size1, int col_size1);
    
    int main(void)
    {
    int row_size1[5],col_size1[5],flag=1;
    int **A = NULL;
    int **p1, **p2;
    char c,k1,k2;
    
    
    do {
    printf("\ncmd > ");
    c = getchar();
    
    
    if (c == 'A') {
    scanf("%d %d", &row_size1[0], &col_size1[0]);
    A = matrix_alloc(row_size1[0],col_size1[0]);
          matrix_fill(A,row_size1[0],col_size1[0]);
          matrix_display(A,row_size1[0],col_size1[0]);
      }
    
    
      while (flag == 1);
      return 0;
    }
    
    void matrix_display(int **A, int row_size1, int col_size1)
    {
    int i, j;
    printf("The required matrix is\n");
    for (i = 0; i < row_size1; i++) {
    for (j = 0; j < col_size1; j++)
    printf("%d ", A[i][j]);
    printf("\n");
      }
    }
    
    int **matrix_alloc(int row_size1, int col_size1)
    {
    int i;
    int **result = malloc(row_size1 * sizeof(int *));
    for (i = 0; i < row_size1; i++) {
    result[i] = malloc(col_size1 * sizeof(int));
      }
    return result;
    }
    
    void matrix_fill(int **A, int row_size1, int col_size1)
    {
    int i, j;
      //  printf("Enter matrix of %d rows and %d columns\n", row_size1, col_size1);
    for (i = 0; i < row_size1; i++) {
    for (j = 0; j < col_size1; j++) {
    scanf("%d", &A[i][j]);
        }
      }
     if(j>0){
     fprintf(stderr, "the number of entries is not valid\n");
     exit(1);}
    }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    You mean u want me to move it to the display function instead like this?
    Now it always prints the error even if the dimensions are right
    Code:
    void matrix_display(int **A, int row_size1, int col_size1)
    {
    int i, j;
    printf("The required matrix is\n");
    for (i = 0; i < row_size1; i++) {
    for (j = 0; j < col_size1; j++)
    printf("%d ", A[i][j]);
    printf("\n");
       
    }
    if(j>0){
    fprintf(stderr, "the number of entries is not valid\n");
    exit(1);}
    }
    Last edited by zafy; 11-20-2012 at 07:51 PM.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    Any more suggestions guys?

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    function with better indentation
    Code:
    voidmatrix_display(int**A, introw_size1, intcol_size1)
    {
    inti, j;
    printf("The required matrix is\n");
    for(i = 0; i < row_size1; i++) {
       for(j = 0; j < col_size1; j++)
          printf("%d ", A[i][j]);
    printf("\n");
    }
    if(j>0){
    fprintf(stderr, "the number of entries is not valid\n");
    exit(1);}
    }
    Last edited by zafy; 11-20-2012 at 08:51 PM.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,374
    Here are a few suggestions:

    - If you're going to start propositioning people for assistance through PM, make sure the code you provide in your thread is able to compile.

    - Learn how to properly indent your code so it's readable. We are here to offer our time and skills to help people - the least you can do is make it easy for us to help. If we have to spend five minutes indenting your code [in addition to correcting obvious errors so it can compile], then we will be less inclined to help. Sloppy questions will receive sloppy answers.

    - If your code doesn't clearly state the expected input, and you provide no comments in the code to explain proper input (nor in your OP), how much time are we expected to take to decipher what input you might be feeding into your program to see the issues you're facing? I can't speak for anyone else here, but I at least am not able to read minds.

    - Based on your previous posts, and your seeming lack of following advice, I'm sure many are hesitant with walking through such a mire once more.

    - Read your book. Read tutorials. Most imporantly, read this: How To Ask Questions The Smart Way

    If you feel I've unfairly characterized you in this post, my apologies. I'm not out to make people feel bad. If you are genuinely trying your best to understand, then follow the advice and criticism I give so that people are more willing to help. Make it easier for us to help you.
    stahta01, Salem and AndiPersti like this.

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,385
    Quote Originally Posted by zafy
    function with better indentation
    No.

    Indent style - Wikipedia, the free encyclopedia

    Quote Originally Posted by zafy
    One of the errors I want to do is if the number of entries in matrix that user inputs exceeds the actual number their should be than an error should pop up
    Use proper indentation and then tell me what you want Salem's code to do.
    stahta01 likes this.
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    Okay code that compiles with good indentation:
    Code:
    #include<stdio.h>
    #include <stdlib.h>
    
    
    void matrix_display(int **A, int row_size1, int col_size1);
    int **matrix_alloc(int row_size1, int col_size1);
    void matrix_fill(int **A, int row_size1, int col_size1);
    
    
    int main(void)
    {
    int row_size1[5],col_size1[5],flag=1;
    int **A = NULL;
    int **p1, **p2;
    char c,k1,k2;
    
    do {
        printf("\ncmd > ");
        c = getchar();
    
    
    if (c == 'A') {
          scanf("%d %d", &row_size1[0], &col_size1[0]);
          
    A = matrix_alloc(row_size1[0],col_size1[0]);
          matrix_fill(A,row_size1[0],col_size1[0]);
          matrix_display(A,row_size1[0],col_size1[0]);
        }
      }
    
    while (flag == 1);
    return 0;
      }
    
    void matrix_display(int **A, int row_size1, int col_size1)
    {
    int i, j;
        
    printf("The required matrix is\n");
        
    for (i = 0; i < row_size1; i++) {
          for (j = 0; j < col_size1; j++)
            printf("%d ", A[i][j]);
            printf("\n");
                                                }
    }
    
    int **matrix_alloc(int row_size1, int col_size1)
    {
    int i;
    int **result = malloc(row_size1 * sizeof(int *));
        
    for (i = 0; i < row_size1; i++) {
          result[i] = malloc(col_size1 * sizeof(int));
        }
    return result;
      }
    
    void matrix_fill(int **A, int row_size1, int col_size1)
      {
        int i, j;
        //  printf("Enter matrix of %d rows and %d columns\n", row_size1, col_size1);
        
    for (i = 0; i < row_size1; i++) {
        for (j = 0; j < col_size1; j++) {
            scanf("%d", &A[i][j]);
                                                    }
                                                  }
    if(j>0){
            fprintf(stderr, "the number of entries is not valid\n");
            exit(1);}
              }
    how the code works when I don't have the last j>0 line in it
    Code:
    (user) A 2 2 1 2 3 4 
    (displays a 2 by 2 matrix 1 2 3 4)
    now how the code works with the j>0 statement
    A 2 2 1 2 3 4
    (displays 2 by 2 matrix)
    AND
    "the number of entries is not valid"

    This statement "the number of entries is not valid should only print if the user inputs lets say 5 values for a 2 by 2 matrix.

    p.s. I did read the wiki page for indentation if I still didn't do it right please let me know provided you also respond to this thread

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    Come on you guys are you really going to ignore me?
    I've done everything you've asked. This is honestly a one minute question. I am sorry for annoying you so much in the last thread.
    I've learned my lesson.

  9. #9
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Your indentation is still sloppy. Use either tabs or spaces for indentation in your source code, but not both (ie, don't mix tabs and spaces for indentation). I suspect this is the problem you're having with pasting your code in this forum. Any good IDE can auto-indent your code consistently for you. Even vim can auto-indent code! Learn how to auto-indent your code in your IDE's editor if you don't want to learn how to indent your code properly yourself.

    I also doubt this is really a one minute question. It's probably a one week question.
    rags_to_riches likes this.

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    1,099
    The question seems to be about input only, so why not restrict your attention to this one function:

    Code:
    void matrix_fill(int **A, int row_size1, int col_size1)
    {
        int i, j;
        printf("Enter matrix of %d rows and %d columns\n", row_size1, col_size1);
    
        for (i = 0; i < row_size1; i++) {
            for (j = 0; j < col_size1; j++) {
                scanf("%d", &A[i][j]);
            }
        }
        
        if(j>0) {
            fprintf(stderr, "the number of entries is not valid\n");
            exit(1);
        }
    }
    Taking a look, notice that if row_size1 and col_size1 are both 4, then scanf will run exactly 16 times. In other words, there is currently no possibility that you could discover if a user enters, say, 17 or 18 items (which would be an error, according to your definition). Furthermore, if the user enters only 14 or 15 items, then scanf will just wait for more input (assuming stdin is connected to the terminal).

    This means that first you need to decide how you want the user to signal the "end" of the matrix. If you want the end signal to be an integer itself, then you can use what is called a "sentinal value" that signals the end. Or you could wait for a specific non-integer. For example,

    Code:
    Please enter your 4x4 matrix now (enter ";" to end the input):
    
    1 3 4 5
    2 3 4 5
    2 1 4 -1
    4 0 -10 2
    ;

  11. #11
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,385
    Quote Originally Posted by zafy
    "I've done everything you've asked"
    "This is honestly a one minute question"
    Both of these statements are wrong.

    Quote Originally Posted by zafy
    "code that compiles with good indentation"
    No no no!!! That is NOT good indentation.

    Seriously, have you looked at the link I keep giving you over and over?

    Indent style - Wikipedia, the free encyclopedia

    Every time you use a curly brace, indent your code.

    "I've learned my lesson"
    Tell us what you have learnt after nearly 100 posts?
    stahta01 likes this.
    Fact - Beethoven wrote his first symphony in C

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
     Poor indentation:
    
    voidmatrix_display(int**A, introw_size1, intcol_size1)
    {
    inti, j;
    printf("The required matrix is\n");
    for(i = 0; i < row_size1; i++) {
       for(j = 0; j < col_size1; j++)
          printf("%d ", A[i][j]);
    printf("\n");
    }
    if(j>0){
    fprintf(stderr, "the number of entries is not valid\n");
    exit(1);}
    }
    
    Good indentation:
    voidmatrix_display(int**A, introw_size1, intcol_size1) {
       inti, j;
       printf("The required matrix is\n");
       for(i = 0; i < row_size1; i++) {
          for(j = 0; j < col_size1; j++)
             printf("%d ", A[i][j]);
          printf("\n");
       }
       if(j>0) {
          fprintf(stderr, "the number of entries is not valid\n");
          exit(1);
       }
    }
    Each indent is three spaces (not tabs), and immediately each curly brace marks a block of code that is subordinate - and closes directly below either the matching curly brace OR the first char of the line of code that it terminates. (the i in "if" is matches with the closing brace for the if statement).

    This is usually called K&R style since it was used in the bible of C, "The C Programming Language". Student's style is similar, but moves the opening braces down one line, allowing the matching braces to always line up in the same column.

    As far as getting input - the sentinel idea seems perfect. Is there some reason it won't work for you, Zafy?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help doing error checking
    By t_6 in forum C Programming
    Replies: 4
    Last Post: 05-29-2008, 08:23 PM
  2. Error checking....
    By michigan353 in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2005, 09:27 PM
  3. help with error checking
    By mfitzp in forum C Programming
    Replies: 2
    Last Post: 11-02-2005, 02:24 PM
  4. Error Checking
    By Inquirer in forum C++ Programming
    Replies: 0
    Last Post: 11-15-2002, 05:06 PM
  5. NEED help with a Error checking
    By paulroseby in forum C Programming
    Replies: 7
    Last Post: 10-16-2002, 03:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21