By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Help me in this "Switch Statement" Puzzle !

NewYorker
P: 13
Write a switch statement that tests the value of the char variable response and performs the following actions:
if response is y , the message Your request is being processed is printed
if response is n , the message Thank you anyway for your consideration is printed
if response is h , the message Sorry, no help is currently available is printed
for any other value of response , the message Invalid entry; please try again is printed


__________________________________________________ ________________

I answered:

switch (response)
{
case 0:
if(response=='y')
cout<< "Your request is being processed"<< endl;
break;

case 1:
if(response=='n')
cout<<" Thank you anyway for your consideration" << endl;
break;

case 2:
if(response=='h')
cout<<"Sorry, no help is currently available" << endl;
break;

case 3:
cout<<"Invalid entry; please try again" << endl;
break;
}


Result: Incorrect !
Message:
You are not displaying anything on the screen for any of the different values of response listed in the instructions.
Remember what keyword to use for any other value of response.
You don't seem to be using your cases properly. Don't forget about quotes.
Nov 16 '06 #1
Share this Question
Share on Google+
2 Replies


smartway
P: 24
switch case structure is replacemet for if-else if....-else structure

try the following code
switch (response)
{
case 'y':
cout<< "Your request is being processed"<< endl;
break;

case 'n':
cout<<" Thank you anyway for your consideration" << endl;
break;

case 'h':
cout<<"Sorry, no help is currently available" << endl;
break;

default:
cout<<"Invalid entry; please try again" << endl;
break;
}
Nov 16 '06 #2

NewYorker
P: 13
Awesome !

Really A Smart Way !

Regards.


switch case structure is replacemet for if-else if....-else structure

try the following code
switch (response)
{
case 'y':
cout<< "Your request is being processed"<< endl;
break;

case 'n':
cout<<" Thank you anyway for your consideration" << endl;
break;

case 'h':
cout<<"Sorry, no help is currently available" << endl;
break;

default:
cout<<"Invalid entry; please try again" << endl;
break;
}
Nov 16 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.