1

I was trying to count the number of characters in a string class but for some reason the program is skipping over my function completely. This is just the test code from the main program, it still was giving me the same results. How come the counter function is skipped over?

#include <iostream>
#include <string>

using namespace std;

void prompt(string& dna)
{
    cout << "Input: ";
    getline(cin, dna);
}

void counter(const string DNA,
                 int* a_count, int* t_count, int* c_count, int* g_count)
{
    for (int i = 0; i < DNA.size(); i++)
    {
        if (DNA.at(i) == 'a')
        {
            *a_count++;
        }
        else if (DNA.at(i) == 't')
        {
            *t_count++;
        }
        else if (DNA.at(i) == 'c')
        {
            *c_count++;
        }
        else if (DNA.at(i) == 'g')
        {
            *g_count++;
        }
    }
}

int main()
{
    string dna;
    int a = 0;
    int t = 0;
    int c = 0;
    int g = 0;

    prompt(dna);

    if (! dna.empty())
    {
        cout << "Before:\n"
             << "A: " << a << endl
             << "T: " << t << endl
             << "C: " << c << endl
             << "G: " << g << endl;
        counter(dna, &a, &t, &c, &g);
        cout << "\n\nAfter:\n"
             << "A: " << a << endl
             << "T: " << t << endl
             << "C: " << c << endl
             << "G: " << g << endl;
    }

    system("pause");

    return 0;
}
krizzo
  • 1,823
  • 5
  • 30
  • 52

3 Answers3

7

You're applying operator ++ the wrong way. It should be:

    if (DNA.at(i) == 'a')
    {
        (*a_count)++;
    }
    else if (DNA.at(i) == 't')
    {
        (*t_count)++;
    }
    else if (DNA.at(i) == 'c')
    {
        (*c_count)++;
    }
    else if (DNA.at(i) == 'g')
    {
        (*g_count)++;
    }
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
4

You've got a priority problem between the ++ and * operators. You are incrementing the pointer address, not the value. (*a_count)++; would be correct.

Marc Plano-Lesay
  • 6,808
  • 10
  • 44
  • 75
1

You may find it easier to use reference parameters for the counts instead, since you don't actually need to do any pointer arithetic. ie:

void counter(const string DNA, int& a_count, int& t_count, int& c_count, int& g_count)

And, yes a switch statement would be neater.

Dale
  • 101
  • 1
  • 2
  • 2
  • Yeah I should have it's been a while since I did C++ and my history of C made me think that way instead. I need to get in the habit Thanks. Also wouldn't a switch statement be slower or messy with breaks instead of just an if? – krizzo Sep 22 '11 at 18:21
  • For something this simple, several lines such as `case 'a' : ++a_count; break;` should be quite readable. Anyone maintaining this code can quickly scan through the case statements to find what they're looking for rather than reading each if statement (to make sure that it doesn't relate to a different variable). – Dale Sep 23 '11 at 02:00
  • As for the speed, there seems to be [contention on the issue](http://stackoverflow.com/questions/2129718/switch-optimization-for-many-cases-guarantees-equal-access-time-for-any-case-c), but some compilers do appear to optimise switch statements with [many case statements](http://stackoverflow.com/questions/1241256/switch-case-programming-practice/1241498#1241498). – Dale Sep 23 '11 at 02:10
  • Interesting, I've just been taught by a few C++ teachers that using break; is not a good practice. Is that just to help new programmers not get bad habits? Thanks for the comments and links helped me understand better. – krizzo Sep 24 '11 at 16:43
  • Glad to help. As far as I'm aware, that _is_ the main reason breaks are discouraged. – Dale Sep 25 '11 at 01:28