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
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
Related Q & A:
- Unix tools: what if a file is named minus something?Best solution by Super User
- How to pass a variable from php to a modal?Best solution by Stack Overflow
- How to convert a char array into an int in C++?Best solution by Stack Overflow
- Does a 2005 Nissan Altima 2.5SL have a constant variable transmission?Best solution by answers.yahoo.com
- Have you heard of a boat maker named national marine in Miami FL?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.