Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.


+19 votes

How do you write this code so that two shorter names don't end up giving an output that says "(there was a a tie)." My code works when the tied shorter names come after the true longest one, but it's the first test that keeps messing me up where DANE and Erik come before the actual longest name.

asked in CSC211_Winter2018 by (1 point)

1 Answer

+14 votes

I used an if/ else if loop with the condition under the if part being if the new longest name was longer than the previous longest name which set the new longest name as the current one and under the else if I had if the new longest name was equal to the previous longest name I set a variable = to 1. Later in the code I had another if loop that said if that variable was equal to one then it was a tie so it was taking into account all the ties not just the longest name ones. The solution was to set that variable = to 0 under the first if loop if there was a new longest name, that way even if there was a tie for shorter names the variable would not be set to 1 anymore but would instead be 0, therefore not tripping the later if loop that said it was a tie.

answered by (1 point)