954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

switch statements

Hi, I have a code, but I don't get what I'm doing wrong, can you please help me with this

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



my code is:

switch(response){
case 'y': response;
case 'n': response;
case 'h': response;
break:;
}

limergal
Newbie Poster
11 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 
Hi, I have a code, but I don't get what I'm doing wrong, can you please help me with this 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 my code is: switch(response){ case 'y': response; case 'n': response; case 'h': response; break:; }




Why you are not using break statement for each of the case statement..?

madhuri_mi
Newbie Poster
2 posts since Nov 2006
Reputation Points: 10
Solved Threads: 1
 

oh I forgot about that;

ok so I have this, but I'm missing what goes in the response



switch(response){
case 'y':;
break;
case 'n':;
break;
case 'h':;
break:;
}

limergal
Newbie Poster
11 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

the syntax for switch case is :

switch(data type)
{
      case value1: statements; break;
      case value2: statements; break;
      .............................................
      ..............................................
      case valuen: statements; break;
      default: statements;
}

so you could write statements for every case. In this case you need to put the output on screen so you could simply usecout. Default contains the statements to be executed when none of the case matches the value of the variable. So now you should try yourself writing this program using the above case syntax.

vishesh
Nearly a Posting Virtuoso
1,381 posts since Oct 2006
Reputation Points: 85
Solved Threads: 42
 

ok so I write the statements after the cout, and that's it? I tried it, and it doesn't work...:sad:


switch(response){
case 'y':cout<<"";
break;
case 'n':cout<<"";
break;
case 'h':cout<<"";
break:;
}

limergal
Newbie Poster
11 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

Thanks alot, It took a while, but I got what you meant....Thanx:lol:


Finished code:

switch(response)
{
case 'y': cout<<"Your request is being processed";
break;
case 'n': cout<<"Thank you anyway for your consideration "; break;
case 'h': cout<<"Sorry, no help is currently available";
break;
default: cout<<"Invalid entry; please try again";
}

limergal
Newbie Poster
11 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

Please use code tags while posting code since it preserves fomatting and makes the code more readable by syntactic highlighting..

Read the forum annoucements for more details..

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

how to quit the program using switch statement???

bhemz11
Newbie Poster
1 post since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You