What is wrong with this Laravel 5 response?

Given a int variable named yesCount and another int variable named noCount and a char variable named response.?

  • I am absolutely stumped. I've been doing homework and studying for the past 6 hours, (my day off from work, so I get to study... yay), and my brain is just completely wracked from looking at code all day. (second part of the question) if the character typed in is a y or a Y then increment yesCount and print out "YES WAS RECORDED" if the character typed in is an n or an N then increment noCount and print out "NO WAS RECORDED" If the input is invalid just print the message "INVALID" and do nothing else. My code: cin >> response; { if ( response == 'y' || 'Y') { cout << "YES WAS RECORDED"; yesCount++; } else { if ( response == 'n' || 'N') { cout << "NO WAS RECORDED"; noCount++; } if (response != 'y' && response != 'Y' && response != 'n' && response != 'N') { cout << "INVALID"; } } Please help.

  • Answer:

    The problem is you can't write things like: if( response == 'y' || 'Y' ) Each condition needs to be a full statement, like: if( response == 'y' || response == 'Y' )

Fredmast... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

A few things you can do to clean this up a little. If it's not y,Y and not n,N, then we know it's something else, so you don't have to specifically check for it. So you can do this: if (response == 'y' || response == 'Y') { cout << "YES WAS RECORDED"; yesCount++; } else if (response == 'n' || response == 'N') { cout << "NO WAS RECORDED"; noCount++; } else // something else was selected { cout << "INVALID"; } or, if you know case statements, you can wrap all this in a switch and it would be even tighter: switch (response) { case 'y': case 'Y': cout << "YES WAS RECORDED"; yesCount++; break; case 'n': case 'N': cout << "NO WAS RECORDED"; noCount++; break; default: cout << "INVALID"; }

llaffer

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.