Like Tree1Likes
  • 1 Post By nonoob

Can anyone help me debug this problem?

This is a discussion on Can anyone help me debug this problem? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h>#include <conio.h> #include <ctype.h> #include <string.h> #define LENGTH 20 int main() { char s[LENGTH] = {'

Like Tree1Likes
  • 1 Post By nonoob

Thread: Can anyone help me debug this problem?

'}; char number[10] ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    2

    Can anyone help me debug this problem?

    Code:
    #include <stdio.h>#include <conio.h>
    #include <ctype.h>
    #include <string.h>
    #define LENGTH 20
     
    int main()
    {
         
        char s[LENGTH] = {'\0'};
        char number[10] = {'\0'};
        char operation[10]= {'\0'};
             
        /* read in the users data and ends by enter */
     
        printf("enter mathematical term: \n");
     
        gets(s);
     
     
        printf("printing s\n");
        for (int i=0 ; i<LENGTH ; i++)
        {
            printf("%c" , s[i]);    
        }
        printf("\n");
     
        /*  initializing arrays  */
     
        int k=0,l=0;
     
        for (int j=0 ; j<LENGTH ; j++){
            if (('0'<= s[j]) && (s[j]<= '9'))
            {
     
                number[k] = s[j];
                printf("%c\n" , s[j] ); //to see 
                k++;
            }
                     
     
            else
            {       
             
                operation[l]=s[j];
                l++;
            }
        }
     
        /*  preview arrays  */
     
         printf("printing number\n");
        for (int m=0 ; m<10; m++)
            printf("%c" , number[m]);
     
        printf("\n");
     
        printf("printing operation\n");
        for (int n=0; n<10; n++)
            printf("%c" , operation[n]);
     
        printf("\n");
     
         
        return(0);
    }
    why is there a debugging error?

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    132
    First, get rid of conio.h and never look at it again.
    Second, this is wrong:
    if (('0'<= s[j]) && (s[j]<= '9'))

    Fix that, try again, and what is your error?

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    2
    so how do i change if (('0'<= s[j]) && (s[j]<= '9')) so that it will be right?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    This is OK: if (('0'<= s[j]) && (s[j]<= '9'))
    Your LENGTH is 20 yet your arrays are only 10 elements. So you are overrunning the array number[]
    Salem likes this.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    How does your compiler not warn you against using gets()? If you decide to enter something that's longer than twenty characters, it will silently corrupt the stack memory following your variable until it segfaults or reaches the end of your input.

    Unless you're dealing with unprintable characters, it seems pointless to have for loops of which their sole function is to print out a string. If you are, why are you declaring three different 4-byte ints for counting? A single short at the beginning would be sufficient for all of the loops, as the first part in a for loop can be used to zero the counter. Otherwise, just use
    Code:
    printf("%s", var);
    like everyone else.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    If you includes ctype.h you can use isdigit instead of your wrong line 32.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. debug problem
    By chintugavali in forum C++ Programming
    Replies: 1
    Last Post: 12-27-2007, 01:57 AM
  2. Debug problem
    By fighter92 in forum C++ Programming
    Replies: 4
    Last Post: 07-21-2007, 09:58 AM
  3. debug problem
    By pode in forum Game Programming
    Replies: 1
    Last Post: 05-17-2004, 01:23 PM
  4. Debug Problem
    By drdroid in forum C++ Programming
    Replies: 10
    Last Post: 03-22-2002, 09:05 PM
  5. Help, Debug Problem
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2002, 03:50 PM

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