Help me in this "Switch Statement" Puzzle !

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NewYorker
    New Member
    • Sep 2006
    • 13

    Help me in this "Switch Statement" Puzzle !

    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.
  • smartway
    New Member
    • Oct 2006
    • 24

    #2
    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;
    }

    Comment

    • NewYorker
      New Member
      • Sep 2006
      • 13

      #3
      Awesome !

      Really A Smart Way !

      Regards.


      Originally posted by smartway
      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;
      }

      Comment

      Working...