Like Tree4Likes

PLEASE ! Need help fixing my program

This is a discussion on PLEASE ! Need help fixing my program within the C++ Programming forums, part of the General Programming Boards category; I have an assignment where I have to create a program that counts the number of Gs and Cs, G ...

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    7

    Exclamation PLEASE ! Need help fixing my program

    I have an assignment where I have to create a program that counts the number of Gs and Cs, G and C together and the total number of characters in the string.

    Additionally, there's the requirement to allow input in lower or uppercase, find percentage of g and c and making sure the characters are A,G,C,T. However I haven't gotten to that yet.

    I was able to successfully compile the program and run it but it gives the wrong answer. I'm extremely new to C++ and programming, so I don't know what to do. Please help me.

    Here's what I have.
    Code:
     
    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    int main () {
        
        string input;
        cout << "Please enter Nucleotide sequence:";
        getline (cin, input);
        
        int numofChars = input.length(); 
        int numofg = 0; 
        int numofc = 0;
        int numofgc = 0;
        
       for (unsigned int i = 0;  i< input.length(); i++) {
            if (input.at(i) == 'g' ) {
            numofg ++ ;
        }
         for (unsigned int i = 0; i<input.length(); i++) {
             if (input.at(i) == 'c') {
            numofc ++;
        }
    
    
        for (unsigned int i = 0;  i< input.length(); i++) {
            if (input.at(i) == 'g' ) {
            if (input.at(i+1) == 'c' ) {
                numofgc ++;
            }
        }
    }
    
    
         cout << " The number of Gs are " << numofg << endl ; 
         cout << " The number of Cs are " << numofc << endl ;
         cout << "The number of GCp are " << numofgc << endl ; 
         cout << "The total number of characters are " << numofChars ;
             
       
         }
         return 0;
    
    
    } 
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    Curly bracket in line 48 is from somewhere else. I would say from your first for loop.
    Same for the one of line 44, from the second for loop!

    Welcome to the forum.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    7
    Thank you for your response.

    I'm unsure what to do. I deleted the bracket in line 48 and then when I compiled, there was an error. When the bracket is there, it compiles but gives a wrong answer. To be clearer, the response repeats as many times as there are Gs and Cs.

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    So the first thing i will mention is your indentation, it is very inconsistent. As a result of this, you have a bunch of mismatched brackets, they are all over the place. Fix your indentation and you should immediately be able to see where the problem is.

    Second of all, your third loop will index the string one past the last element, since you are using .at() this will result in an out-of-range exception being thrown. I suggest you look into the string member function find, this should make things easier.

    Are you sure that you only need to find occurences of "gc"? What about "cg"?
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    Quote Originally Posted by zerob2st View Post
    I deleted the bracket in line 48 and then when I compiled, there was an error. When the bracket is there, it compiles but gives a wrong answer. To be clearer, the response repeats as many times as there are Gs and Cs.
    You need to delete the brackets from the lines I mentioned and then put them at the end of the for loops.

    Try this:
    Delete the brackets I mentioned.
    Then review your code to see where you should place two brackets. Not at the same spot of course. If confused, take a good look back at your notes for the for loop, or do some google.

    Be sure to see NEon's post for indentation!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Code:
    #include <iostream>
    #include <string>
     
     
    using namespace std;
     
     
    int main ()
    {     
        string input;
        cout << "Please enter Nucleotide sequence:";
        getline (cin, input);
         
        int numofChars = input.length(); 
        int numofg = 0; 
        int numofc = 0;
        int numofgc = 0;
         
        for (unsigned int i = 0;  i< input.length(); i++)
        {
            if (input.at(i) == 'g' ) 
            {
                numofg ++ ;
            }
         
        for (unsigned int i = 0; i<input.length(); i++)
        {
            if (input.at(i) == 'c')
            {
                numofc ++;
            }
     
        for (unsigned int i = 0;  i< input.length(); i++)
        {
            if (input.at(i) == 'g' )
            {
                if (input.at(i+1) == 'c' )
                {
                    numofgc ++;
                }
            }
        }
     
     
        cout << " The number of Gs are " << numofg << endl ; 
        cout << " The number of Cs are " << numofc << endl ;
        cout << "The number of GCp are " << numofgc << endl ; 
        cout << "The total number of characters are " << numofChars ;
        
        return 0;
    }
    I have fixed the indentation for you, and it should now be painfully obvious why this code is not working. Do not underestimate the importance of proper indentation.
    Elysia, stahta01 and Salem like this.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  7. #7
    Registered User
    Join Date
    Aug 2013
    Posts
    7
    Quote Originally Posted by Neo1 View Post
    Code:
    #include <iostream>
    #include <string>
     
     
    using namespace std;
     
     
    int main ()
    {     
        string input;
        cout << "Please enter Nucleotide sequence:";
        getline (cin, input);
         
        int numofChars = input.length(); 
        int numofg = 0; 
        int numofc = 0;
        int numofgc = 0;
         
        for (unsigned int i = 0;  i< input.length(); i++)
        {
            if (input.at(i) == 'g' ) 
            {
                numofg ++ ;
            }
         
        for (unsigned int i = 0; i<input.length(); i++)
        {
            if (input.at(i) == 'c')
            {
                numofc ++;
            }
     
        for (unsigned int i = 0;  i< input.length(); i++)
        {
            if (input.at(i) == 'g' )
            {
                if (input.at(i+1) == 'c' )
                {
                    numofgc ++;
                }
            }
        }
     
     
        cout << " The number of Gs are " << numofg << endl ; 
        cout << " The number of Cs are " << numofc << endl ;
        cout << "The number of GCp are " << numofgc << endl ; 
        cout << "The total number of characters are " << numofChars ;
        
        return 0;
    }
    I have fixed the indentation for you, and it should now be painfully obvious why this code is not working. Do not underestimate the importance of proper indentation.
    I understand the importance of proper indentation now. However I must apologize that I still don't understand why the code isn't working. I'm still new at this.

    ( On a side note,I made a mistake and the project actually asks for CGp only instead of GCp but that's trivial. )

  8. #8
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by zerob2st View Post
    I understand the importance of proper indentation now. However I must apologize that I still don't understand why the code isn't working. I'm still new at this.

    ( On a side note,I made a mistake and the project actually asks for CGp only instead of GCp but that's trivial. )
    You need a closing bracket '}' on line 32 and line 25 in the code i posted. Without these the program will not do as you intend.

    Also as i mentioned earlier, you're indexing your string one past the end in the third loop. This will also cause an error so you need to come up with another way of finding the character sequence.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  9. #9
    Registered User
    Join Date
    Aug 2013
    Posts
    7
    Thanks, I had line 25 but I missed line 32 for some reason.

    So I should use find? Honestly I've never seen or used it. And the link you gave me showed "find" being used to find where the word first occurs but I need to find how many times CG occurs. How would find be used for that purpose?

  10. #10
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by zerob2st View Post
    Thanks, I had line 25 but I missed line 32 for some reason.

    So I should use find? Honestly I've never seen or used it. And the link you gave me showed "find" being used to find where the word first occurs but I need to find how many times CG occurs. How would find be used for that purpose?
    Yes i suppose it could seem slightly counterintuitive to use find like this. If you were just looking for a simple value i would suggest std::count but since you are actually looking for a sequence of values we have to stick with find.

    As you can see find returns the position in the string where the sequence has been found, if no match could be found it returns std::string::npos. You could thus call find on your string in a loop, if you make sure to pass the result of the last iteration to the subsequent call to find, until the result is npos then you could simply have a counter that holds the number of iterations you have gone through.

    ...Hmm after typing it out i realize this might be quite a mouthful. If this is too steep then i suggest you return to your original approach. This could be made to work, you just have to take into account that i+1 is not a valid index in the last iteration of your for-loop. How would you fix this?

    Give either approach a try and post the code here if you get stuck.
    SirPrattlepod likes this.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  11. #11
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Neo1 View Post
    Yes i suppose it could seem slightly counterintuitive to use find like this. If you were just looking for a simple value i would suggest std::count but since you are actually looking for a sequence of values we have to stick with find.

    As you can see find returns the position in the string where the sequence has been found, if no match could be found it returns std::string::npos. You could thus call find on your string in a loop, if you make sure to pass the result of the last iteration to the subsequent call to find, until the result is npos then you could simply have a counter that holds the number of iterations you have gone through.

    ...Hmm after typing it out i realize this might be quite a mouthful. If this is too steep then i suggest you return to your original approach. This could be made to work, you just have to take into account that i+1 is not a valid index in the last iteration of your for-loop. How would you fix this?

    Give either approach a try and post the code here if you get stuck.
    Since every character in the string must be checked, is there an advantage to using std::find? I think using find would result in requiring four loops (see my suggestion below) similar to how the code is now -- requiring iterating over the string four times when it could be done by iterating over it just once...

    I personally think it would be more beneficial to use the current approach with the following modification: combine the four loops into one.

  12. #12
    Registered User
    Join Date
    Aug 2013
    Posts
    7
    Thanks for the help. I think I got it this time. I tried again and there doesn't seem to be any problems. Please check it over just in case.

    Code:
    #include <iostream>
    #include <string>
      
      
    using namespace std;
      
      
    int main ()
    {     
        string input;
        cout << "Please enter Nucleotide sequence:";
        getline (cin, input);
          
        int numofChars = input.length(); 
        int numofg = 0; 
        int numofc = 0;
        int numofcg = 0;
          
        for (unsigned int i = 0;  i< input.length(); i++)
        {
            if (input.at(i) == 'g' ) 
            {
                numofg ++ ;
            }
    	}
        for (unsigned int i = 0; i<input.length(); i++)
        {
            if (input.at(i) == 'c')
            {
                numofc ++;
            }
    	}
        for (unsigned int i = 0;  i< input.length() -1; i++)
        {
            if (input.at(i) == 'c' )
            {
                if (input.at(i+1) == 'g' )
                {
                    numofcg ++;
                }
            }
        }
      
      
        cout << " The number of Gs are " << numofg << endl ; 
        cout << " The number of Cs are " << numofc << endl ;
        cout << "The number of CGp are " << numofcg << endl ; 
        cout << "The total number of characters are " << numofChars ;
         
        return 0;
    }
    Now I'm trying the extra parts of the assignment. For allowing input to be either lower or upper, do i just put an "or" operator?

  13. #13
    Registered User
    Join Date
    Aug 2013
    Posts
    7
    Code:
    #include <iostream>
    #include <string>
    #include <ctype.h>   
       
    using namespace std;
       
       
    int main ()
    {     
        string input;
        cout << "Please enter Nucleotide sequence:";
        getline (cin, input);
           
        int numofChars = input.length(); 
        int numofg = 0; 
        int numofc = 0;
        int numofcg = 0;
           
        for (unsigned int i = 0;  i< input.length(); i++)
        {
            if (toupper(input.at(i)) == 'G' ) 
            {
                numofg ++ ;
            }
        }
        for (unsigned int i = 0; i<input.length(); i++)
        {
            if (toupper(input.at(i)) == 'C')
            {
                numofc ++;
            }
        }
        for (unsigned int i = 0;  i< input.length() -1; i++)
        {
            if (toupper(input.at(i)) == 'C' )
            {
                if (toupper(input.at(i+1)) == 'G' )
                {
                    numofcg ++;
                }
            }
        }
       
       
        cout << " The number of Gs in the sequence are " << numofg << endl ; 
        cout << " The number of Cs in the sequence are " << numofc << endl ;
        cout << "The number of CpG sites are " << numofcg << endl ; 
        cout << "The total number of characters are " << numofChars << endl;
          
        return 0;
    }
    This is my final program. Finally done. It works out when I use it. Anybody check it just in case?

    Regardless, THANK YOU EVERYBODY FOR YOUR HELP!

  14. #14
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    It looks like it should work.

    Have you considered combining the three loops into one?

  15. #15
    Registered User
    Join Date
    Aug 2013
    Posts
    7
    How would that look like?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help fixing bugs in program?
    By celticpride in forum C Programming
    Replies: 4
    Last Post: 05-04-2011, 04:19 PM
  2. need help fixing this program
    By truetrini20 in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2010, 11:07 AM
  3. need help fixing excercise program...
    By SymDePro in forum C Programming
    Replies: 2
    Last Post: 05-07-2009, 03:33 PM
  4. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM

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