Error Control in C Program

This is a discussion on Error Control in C Program within the C Programming forums, part of the General Programming Boards category; I have this program where I am supposed to only allow the user to input positive numbers, like 1, 56, ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    6

    Error Control in C Program

    I have this program where I am supposed to only allow the user to input positive numbers, like 1, 56, 101 etc.

    How can I implement error control for this program such that if the the user input negative numbers, alphabets, words, decimals or symbols, the program will not crash and loop itself to ask for another input.

    Thanks

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    What have you tried so far?

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    Have tried if/else statements. Tried switch with break/continue also. Able to eliminate the negative numbers in error control. But the alphabets and decimals will pluck a random number from the memory.

    Thanks for the quick reply

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Perhaps I should have been a little more specific; could you post the latest code you have been working on? Also, are you familiar with the functions in <ctype.h>?

  5. #5
    Registered User
    Join Date
    Sep 2010
    Location
    Halesowen, England
    Posts
    30
    If a postive integer string can only contain the characters 0 to 9, it's easy enough to create a function to check it:
    Code:
    bool is_positive_number(const char *str)
    {
      int i;
    
      /* Validate that a string exists */
      if ((!str) || (!*str)) return FALSE;
    
      /* Check each character in the string is on the range '0' to '9' */
      for (i=0; i<strlen(str); i++) {
        if ((*(str+i) < '0') || (*(str+i) > '9')) return FALSE;
      }
      return TRUE;
    }

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    Code:
    #include <stdio.h>
    int main()
    
    {
        int i;
        int k;
        int sum;
        sum=0;
    
        printf("Please input the number n u want to compute the sum to: ");
        scanf("%d", &k);
    
        if (k>0){
    
        for(i=1; i<=k; i++)
        sum=sum+i;
    
        printf("The sum to n terms is: %d", sum);
        }
    
        else {
        printf("Error");
        }
    
        return 0;
    }
    Have tried this so far. Not familiar with the <ctype.h> though. Lecturer finished the course on c programming in 3 weeks only.

    Thanks for the help!

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    Another question. Still noob in C programming

    Assuming i got a algorithm to sort the numbers in an array in ascending order. How is it possible that i can use it to identify the duplicate numbers in the array.

    Thanks

  8. #8
    Registered User
    Join Date
    Sep 2010
    Location
    Halesowen, England
    Posts
    30
    Just parse the array, checking each element against the one before it.
    Assuming you have your sorted array (of integers?) called myArray, and it has array_size elements:
    Code:
    int i;
    /* Note, start at the 2nd element (i=1, not i=0) */
    for (i=1; i<array_size; i++) {
      if (myArray[(i-1)] == myArray[i]) {
        /* duplicated element code here */
      }
    }

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    Quote Originally Posted by Jamdog View Post
    Just parse the array, checking each element against the one before it.
    Assuming you have your sorted array (of integers?) called myArray, and it has array_size elements:
    Code:
    int i;
    /* Note, start at the 2nd element (i=1, not i=0) */
    for (i=1; i<array_size; i++) {
      if (myArray[(i-1)] == myArray[i]) {
        /* duplicated element code here */
      }
    }
    Hmm, think i roughly get it, But still cant get my code to run

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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