CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2003
    Posts
    15

    Comparision Error ----

    Hi

    I am a beginner in C++. I am trying towrite a program where the user enters the number of sequences which can be more than 1 and then compare each letter of the sequence with each element of char array.but it falls over on line 80.


    Any help is appreciated.


    ------------------------------------------------------------------------------
    // This program asks the user to enter the no of sequences
    // and read the sequences as string and then compare them

    // Assumption
    // The char of sequences should be A,C,T,G,U

    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;

    bool checkSequenceChar(string);

    int main()
    {

    int input = 0; // sequence

    vector<string> sequences;

    cout << "Enter The Number Of Sequence in PTree: ";


    while(!(cin >> input))
    {
    string buffer;
    cin.clear(); //clear failbit
    cin >> buffer; //flush stream
    cout << "Invalid Input.. Please Enter An Interger Only !!!" << endl;
    cout << "Enter The Number Of Sequence : ";

    }

    for (int i = 0; i < input; i++)
    {
    string temp;

    // store the elements in array.
    cout << "Please Enter Sequence [" << i << "] : " ;

    cin >> temp;
    sequences.push_back(temp);
    }

    for (int i = 0; i < input; i++)
    {


    for (int j = 0; j < sequences[i].length(); j++)
    {

    string seqChar;

    seqChar = sequences[i].substr(j,1);

    if ( checkSequenceChar(seqChar) )
    {
    cout << "Sequence [" << i << "] Matches " << sequences[i] <<endl;
    }
    else
    {
    cout << "Sequence Doesn't [" << i << "] Matches " << sequences[i] <<endl;
    }
    }
    }


    return 0;
    }

    // checks every char of the sequence entered
    bool checkSequenceChar(string c)
    {
    char seqFilter[] = "ACTGU";

    cout << " The Comparision Char is : " << c << endl;
    for (int i = 0; i < 5; i++)
    {
    cout << "Sequence Pick : " << " " << i << " " << seqFilter[1] << endl;

    // THE PROGRAM FALLS HERE
    if ( seqFilter[i] != c )
    {
    cout << "Sequence Char NOT Matches : " << c << " " << seqFilter[i] << endl;
    //return false;
    }
    }
    return true;
    }

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    • First, the reason for the compile error: you are
      comparing a "char" with a "std::string". To fix this
      problem, change :
      Code:
      string seqChar;
      seqChar = sequences[i].substr(j,1);
      to
      Code:
      char seqChar = sequences[i].at(j); // or sequences[i][j]
      and modify checkSequenceChar() to take a char as its argument.
    • What are you trying to check here ? There is probably an
      easier way. For example, are you trying to check that the only
      characters entered by the user in the string are A,C,T,G,U ?

      You can do this as follows:

      Code:
      const string seqFilter = "ACTGU";
      
      std::string testString = "AGGGTUTACC";
      
      std::string::size_type position = testString.find_first_not_of(seqFilter);
      
      if (position != std::string::npos)
      {
         std::cout << " a character other than   A,C,T,T,G, or U   was found " << std::endl;
      }

  3. #3
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Philip, I bet that's exactly what he is doing. It appears he is trying to write a program for the user to enter in Nucleic Acid sequences (DNA and RNA). ACTGU correspond to adenine, cytosine, thymine, guanine, and uracil.

    CheckSequenceChar will always fail as it is written. Philip has a good suggestion as how to use the STL. A slightly more complicated approach that will run much more quickly -- and that is to copy the (most compilers') C library's implementations of the isXXX functions:


    static bool NABase[256]; // static initializes entire array to false

    void InitNABase()
    {
    &nbsp; &nbsp;NABase['A'] = true;
    &nbsp; &nbsp;NABase['C'] = true;
    &nbsp; &nbsp;NABase['T'] = true;
    &nbsp; &nbsp;NABase['G'] = true;
    &nbsp; &nbsp;NABase['U'] = true;
    }

    inline bool IsNABase(char c)
    {
    &nbsp; &nbsp;return NABase[c];
    }

    #define checkSequenceChar(c) IsNABase(c)


    All you have to do is call InitNABase at the beginning of the program. This method does use 256 bytes of data memory and requires initialization, but is quicker, is compatible with C, and is not that difficult to understand (I'm not saying that this is any more or less difficult to understand compared to other methods).

    You can avoid the initialization function, by initializing in the source. Something like:


    static bool NABase[256] =
    {
    false, false, false, false,
    // ...
    false, true, false, false,
    // ...
    false, false, true // last true ends the sequence. The rest
    // will automatically be initialized to false by being declared static
    };

  4. #4
    Join Date
    Jan 2003
    Posts
    15
    Hi


    What Kevin Hall says is right.I am trying to draw a Phylogenetic Tree.
    Kevin if you got the source code will you mind passing it to me.

    Thanks

  5. #5
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Sorry, I don't have any code -- I just remember my biochem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured