Thread: i get an error after trying to run this code

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    8

    Talking i get an error after trying to run this code

    Code:
    #include<stdio.h>
    
    void convert(char *str, int pn)
    
    
    { 
      
    	
       char ch; 
       char *words[] = {"ABC", "DEF", "GHI", "JKL", "MNO", "PRS", "TUV", "WXY"}; 
    
       if (str[pn] == '\0')
       { 
           printf("%s\n", str); 
           return; 
       } 
       ch = str[pn]; 
       if (ch < '2')
           convert(str, pn + 1); 
       else
       {
           str[pn] = words[ch - '2'][0]; 
           convert(str, pn + 1); 
           str[pn] = words[ch - '2'][1]; 
           convert(str, pn + 1); 
           str[pn] = words[ch - '2'][2]; 
           convert(str, pn + 1);
           if (ch == '7' || ch == '9')
           {
               str[pn] = words[ch - '2'][3]; 
               convert(str, pn + 1);
           }
           str[pn] = ch; 
       } 
    } 
    
    
    int main(void)
    { 
       convert("4663", 0); 
       return 0;
    }



    i was trying to get a program to run like wat handphone does... like a motorolla's i-Tap function where it will let the user to choose a variety of letters to form a meaningful word.
    please help me to debug it
    Last edited by Salem; 09-07-2005 at 11:14 AM. Reason: The / goes on the tag at the END, not at the START

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Can somebody PLEASE put a '/' in the closing code tag.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    hmmm....which closing tag... tell me please....i'm still new

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Edit your first post and change the '[ CODE]' right at the bottom to '[ /CODE]'. Makes it readable Ignore the spaces I put.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Your code errors becuase you're passing a string literal and treating it like a pointer to an array of characters you have allocated to you.
    In other words you're trying to access memory you do not own.

    try this

    Code:
    int main(void)
    {
        char x[] = "4663";
        convert(x, 0);
        return 0;
    }
    and like the others said your [ CODE] tags are backwards making your post difficult to read.
    should be something like:

    [ CODE]
    .... some code ....
    [ /CODE]

    (without the space before CODE of course)
    Last edited by spydoor; 09-07-2005 at 07:51 AM.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    Code:
    #include<stdio.h>
    
    void convert(char *str, int pn)
    
    
    { 
    
    
    char ch; 
    char *words[] = {"ABC", "DEF", "GHI", "JKL", "MNO", "PRS", "TUV", "WXY"}; 
    
    if (str[pn] == '\0')
    { 
    printf("%s\n", str); 
    return; 
    } 
    ch = str[pn]; 
    if (ch < '2')
    convert(str, pn + 1); 
    else
    {
    str[pn] = words[ch - '2'][0]; 
    convert(str, pn + 1); 
    str[pn] = words[ch - '2'][1]; 
    convert(str, pn + 1); 
    str[pn] = words[ch - '2'][2]; 
    convert(str, pn + 1);
    if (ch == '7' || ch == '9')
    {
    str[pn] = words[ch - '2'][3]; 
    convert(str, pn + 1);
    }
    str[pn] = ch; 
    } 
    } 
    
    
    int main(void)
    { 
    convert("4663", 0); 
    return 0;
    }

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    yes... it is solved... tq...
    hmmm i'm stilll far from my target...this is automatic generation..
    how to make it manual generation...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  2. How to run C code under VC environment?
    By robin in forum C Programming
    Replies: 12
    Last Post: 04-06-2005, 11:19 AM
  3. Determining required flops to run C code
    By mollyann in forum C Programming
    Replies: 5
    Last Post: 03-30-2005, 01:28 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM