Thread: Problem with function!

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    77

    Problem with function!

    Hello to all, newbie here.

    I need help with my function. Here is the code for the function:

    Code:
    char content_scan( char sequence)
        {
            int x ;
            
            for(x=0 ; x < (strlen(sequence)) ; x++)
                {
                    while( sequence[x] )
                        {
                            if( sequence[x] == 'U' ) 
                                {
                                    return "RNA" ;
                                    break ;
                                }
                            else if (sequence[x] == 'A' || x == 'C' || x == 'G' || x == 'T')
                                {
                                    return "DNA" ;
                                    break ;
                                }
                            else
                                {
                                    return "PROTEIN" ;
                                    continue ;
                                }
                        }
                }
        }
    I am trying to return the output to the variable "loadedSequences[nSequences].type" which is a character array with the following statement:

    Code:
    loadedSequences[nSequences].type = content_scan(loadedSequences[nSequences].data) ;
    The argument for the function will be a long string of letters.

    Any help would be great.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    First off, your function definition says it returns a single character:
    Code:
    char content_scan( char sequence)
    After you fix that, I'm not sure if there will be a scope issue with your returned strings or not once the function has exited (since you'll be passing back a pointer to the string and not the string itself). Try it out.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Code:
    char content_scan( char sequence)
    The argument for the function will be a long string of letters.
    Not the way you've coded it, it won't!!

    Surely you are getting compile errors. And since you are, please post them so we don't have to play hide-and-seek or peek-a-boo analyzing your code.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    Still receiving errors. Here are the errors:

    beta_seqTool.c: In function ‘content_scan’:
    beta_seqTool.c:14: warning: passing argument 1 of ‘strlen’ makes pointer from integer without a cast
    beta_seqTool.c:16: error: subscripted value is neither array nor pointer
    beta_seqTool.c:18: error: subscripted value is neither array nor pointer
    beta_seqTool.c:23: error: subscripted value is neither array nor pointer
    beta_seqTool.c: In function ‘main’:
    beta_seqTool.c:128: warning: passing argument 1 of ‘content_scan’ makes integer from pointer without a cast

    Code:
    int content_scan( char sequence)
        {
            int x ;
            
    14        for(x = 0 ; x < strlen(sequence) ; x++)
                {
    16                while( sequence[x] )
                        {
    18                        if( sequence[x] == 'U' ) 
                                {
                                    return 1 ; // RNA
                                    break ;
                                }
    23                        else if (sequence[x] == 'A' || x == 'C' || x == 'G' || x == 'T')
                                {
                                    return 2 ; // DNA 
                                    break ;
                                }
                            else
                                {
                                    return 3 ; // PROTEIN
                                    continue ;
                                }
                        }
                }
        }
    Code:
    128     loadedSequences[nSequences].type = content_scan(loadedSequences[nSequences].data) ;
    Also, is there a way I can return a string (i.e DNA, RNA, or PROTEIN as my value for loadedSequences[nSequences].type?

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Code:
    strlen(sequence)
    You can't do a strlen() on a single character.

    Code:
    sequence[x]
    You can't subscript a single character.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Also, is there a way I can return a string (i.e DNA, RNA, or PROTEIN as my value for loadedSequences[nSequences].type?
    You could pass the address of loadedSequences[nSequences].type, and set the string in it quite easily.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    Sorry, didn't see your post. So if i use the address as my argument things should work fine?

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You'll still need the char array of sequence. But yes.
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    heres the code:

    Code:
    10    char content_scan( (char &)sequence)
        {
            int x ;
            
            for(x = 0 ; x < strlen(sequence) ; x++)
                {
                    while( sequence[x] )
                        {
                            if( sequence[x] == 'U' ) 
                                {
                                    return "RNA" ;
                                    break ;
                                }
                            else if (sequence[x] == 'A' || x == 'C' || x == 'G' || x == 'T')
                                {
                                    return "DNA" ;
                                    break ;
                                }
                            else
                                {
                                    return "PROTEIN" ;
                                    continue ;
                                }
                        }
                }
        }                
    
    char *address4content_scan = &loadedSequences[nSequences].data[0] ;
                                       
    133    loadedSequences[nSequences].type = content_scan(address4content_scan) ;
    Still receiving some errors:

    beta_seqTool.c:10: error: syntax error before ‘(’ token
    beta_seqTool.c: In function ‘main’:
    beta_seqTool.c:133: error: incompatible types in assignment


    Any suggestions?

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Yes.

    Code:
    void content_scan( char * sequence, char * out_field )
    Code:
    if( sequence[x] == 'U' ) 
                                {
                                    strcpy(out_field, "RNA" ) ;
                                    return ;
                                }
                            else if (sequence[x] == 'A' || x == 'C' || x == 'G' || x == 'T')
                                {
                                    strcpy(out_field, "DNA")  ;
                                    return ;
                                }
                            else
                                {
                                    strcpy(out_field, "PROTEIN")  ;
                                    return ;
                                }
    Code:
    content_scan( loadedSequences[nSequences].data,  loadedSequences[nSequences].type )
    Mainframe assembler programmer by trade. C coder when I can.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You really should learn C before you start messing around with the building blocks of life as we know it...
    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef enum { RNA, DNA, Protein, Unknown } SequenceType;
    
    static const char *const NucleotideCodes = "ACGTU";
    
    SequenceType content_scan(const char *sequence)
    {
       // First we'll see if it's a protein.
       const char *p = sequence;
    
       // Go through each code.
       while (p && *p != 0)
       {
          // If the current code is not a nucleotide, obviously it's an amino
          // acid chain and therefore a protein.
          if (strchr(NucleotideCodes, *p) == NULL)
             return Protein;
          ++p;
       }
    
       // Not a protein, but a nucleotide sequence; only differentiator is
       // uracil vs. thymine.
       // Reset convenience pointer
       p = sequence;
       while (p && *p != 0)
       {
          if (*p == 'U') return RNA;
    
          if (*p == 'T') return DNA;
    
          ++p;
       }
       return Unknown;
    }
    
    int main()
    {
       const char *sequences[] = {
          "AGCGAACUGCCCG", "AGCGAACTGCCCG", "AGMLAHA" } ;
    
       int i = 0;
       for (; i < (sizeof(sequences) / sizeof(sequences[0])); ++i)
       {
          printf("Result for %s: ", sequences[i]);
    
          switch(content_scan(sequences[i]))
          {
          case RNA:
             printf("RNA\n");
             break;
          case DNA:
             printf("DNA\n");
             break;
          case Protein:
             printf("Protein\n");
             break;
          case Unknown:
          default:
             printf("Unknown\n");
             break;
          }
    
       }
    
       return 0;
    }

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    Thanks for the help.

    Question: Why couldn't I return the string "DNA" etc directly? Why use "void"?

  13. #13
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    Can anyone see why this is returning "PROTEIN" when the first is DNA (all ACGT) ?

    Code:
    void content_scan( char * sequence, char * out_field )
        {
            int x ;
            
            for(x = 0 ; x < strlen(sequence) ; x++)
                {
                    while( sequence[x] )
                        {
                            if( sequence[x] == 'U' ) 
                                {
                                    strcpy(out_field, "RNA") ;
                                    return ;
                                }
                            else if (sequence[x] != 'A' || sequence[x] != 'C' || sequence[x] != 'G' || sequence[x] != 'T' || sequence[x] != EOF || sequence[x] != '\n' )
                                {
                                    strcpy(out_field, "PROTEIN") ;  
                                    return ; 
                                }
                            else
                                {
                                    strcpy(out_field, "DNA" ) ;
                                    return ;
                                }
                        }
                }
        }

  14. #14
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Because this:
    Code:
    sequence[x] != 'C'
    is true.
    Mainframe assembler programmer by trade. C coder when I can.

  15. #15
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Perhaps you meant to code
    Code:
    sequence[x+1] != 'C'
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM