2
if(gene1A[20] == 'T' || gene2A[20] == 'T')
    outFile << "Person A is of 'Anemic' type." << endl;
else if(gene1A[20] == 'T' && gene2A[20] == 'T')
    outFile << "Person A if of 'Carrier' type." << endl;
else
    outFile << "Person A is of 'Normal' type." << endl;

if(gene1B[20] == 'T' || gene2B[20] == 'T')
    outFile << "Person B is of 'Anemic' type." << endl;
else if(gene1B[20] == 'T' && gene2B[20] == 'T')
    outFile << "Person B if of 'Carrier' type." << endl;
else
    outFile << "Person B is of 'Normal' type." << endl;

if(gene1C[20] == 'T' || gene2C[20] == 'T')
    outFile << "Person C is of 'Anemic' type." << endl;
else if(gene1C[20] == 'T' && gene2C[20] == 'T')
    outFile << "Person C if of 'Carrier' type." << endl;
else
    outFile << "Person C is of 'Normal' type." << endl;

if(gene1D[20] == 'T' || gene2D[20] == 'T')
    outFile << "Person D is of 'Anemic' type." << endl;
else if(gene1A[20] == 'T' && gene2A[20] == 'T')
    outFile << "Person D if of 'Carrier' type." << endl;
else
    outFile << "Person D is of 'Normal' type." << endl;

is my code as of now. What I need to do is output the the 'outFile' if the Person is Anemic, a Carrier, or normal, based on the array I have set up. Each array is 444 characters long and is either an A, C, T, or O. If a T is in the 20th position of gene1[] and/or gene2[] then the person would be Anemic (if only one array) or a carrier (if in both arrays).

What I have now makes them automatically be "normal". I believe my if statements aren't set up properly, but what I need is to reference the 20th value in an array and then if it == 'T', output their "type".

Note: I noticed in my code i put 20 instead of 19. I made that correction so just look past that.

Thanks guys!

5
  • 1
    It looks to me like it should work... Commented Nov 27, 2012 at 2:27
  • Why not just fix the code to say 19 instead of putting a disclaimer? It would take about as long to do this. Commented Nov 27, 2012 at 2:30
  • 1
    Well, you should do the if(&&) before the else if(||). Commented Nov 27, 2012 at 2:32
  • @ Marcelo: I was about to post, then website told me to indent, then that is when I noticed the mistake was while I was indenting the code to post. @irrelephant: Done and done. Commented Nov 27, 2012 at 2:45
  • @TannerRoss: You can edit your question. Commented Nov 28, 2012 at 0:40

1 Answer 1

1

(This isn't quite an answer, but it's difficult to express as a comment, and the resulting simplification might lead you towards an answer anyway...)

Functional decomposition is your friend:

const char* type(const char* gene1, const char* gene2) {
    return gene1[19] != 'T' ? "Normal" : gene2[19] == 'T' ? "Anemic" : "Carrier";
}
⋮
outFile << "Person A is of '" << type(gene1A, gene2A) << "' type." << endl;
outFile << "Person B is of '" << type(gene1B, gene2B) << "' type." << endl;
outFile << "Person C is of '" << type(gene1C, gene2C) << "' type." << endl;
outFile << "Person D is of '" << type(gene1D, gene2D) << "' type." << endl;

It also makes bugs like the one you introduced for person D much harder to introduce and easier to spot when you do.

EDIT: @MarkB pointed out an error in my logic (I misread the original logic). Unfortunately, I'm not sure how to fix it, since the original logic is of the form:

     if A or  B then X
else if A and B then Y
else                 Z

Since (A or B) is true whenever (A and B) is true, the second clause will never trigger, which almost certainly wasn't your intent. If you meant to have the AND clause first, then the type() function could be rewritten thus:

const char* type(const char* gene1, const char* gene2) {
    bool t1 = gene1[19] == 'T';
    bool t2 = gene2[19] == 'T';
    return t1 && t2 ? "Anemic" : t1 || t2 ? "Carrier" : "Normal"  );
}

Incidentally, this function wouldn't be a "sub-function" (whatever that means) of the current code, it would simply be a free function declared above the function. OTOH, if your compiler supports C++11 lambdas, you can in fact declare the type() function locally to the function in question:

auto type = [](const char* gene1, const char* gene2) -> const char * {
    …
};
Sign up to request clarification or add additional context in comments.

3 Comments

....Is that a sub-function of some sort Marcelo? I am just about finished my CS 110 class so my understanding is limited, but to me that looks like a sub-function that may make my code much much smaller. Just trying to understand how it works
It's just a function, short and sweet. You can't have "subfunctions" in C or C++. Whenever you find yourself doing the same thing multiple times you should think to yourself "should this be a separate function?"
Unfortunately this doesn't yield the correct result when gene1[19] != 'T' and gene2[19] == 'T': It would print normal instead of anemic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.