Switch Case.. for menu selection

I have been trying to use a Switch Case for a menu selection based on letter prompts
Q or q (are my quits)  
I have  do while to ask for prompts
1) I can get it to work as I have but tried using default in case user enters other letters then
A, F, D, L but if I use default then Q or q does not exit do while.
Anyone have ideas to do this ?
#include <iostream.h>
int main() {

      char option;
      double interest = 0.0;


      do {
      cout << endl;
      cout << "Enter Option A, D, F, L or Q to quit" <<endl;
      cin >> option;

switch (option){
  case 'A':
  case 'a':
    interest = 2.3;
      cout << 'A' << interest;
    break;
  case 'D':
  case 'd':
    interest = 2.6;
      cout << 'D' << interest;
    break;
  case 'F':
  case 'f':
    interest = 2.9;
      cout << 'F' << interest;
    break;
  case 'L':
  case 'l':
    interest = 3.8;
      cout << 'L' << interest;
    break;
  /*default:
    interest = 0.0;
      cout << "DEFAULT" << interest;*/
}//SWITCH

      }while (option!='q' && option !='Q');


return 0;
}
MarcosInfoAsked:
Who is Participating?
 
y_o_yCommented:
Hi!

sorry about that first comment of mine...just dont mind it..

I think your code structure is good..just change the "&&" to "||"..it will definitely not quit since it's an AND condition so make an OR condition...try this one...

#include <iostream.h>
int main() {

     char option;
     double interest = 0.0;


     do {
     cout << endl;
     cout << "Enter Option A, D, F, L or Q to quit" <<endl;
     cin >> option;

switch (option){
  case 'A':
  case 'a':
    interest = 2.3;
     cout << 'A' << interest;
    break;
  case 'D':
  case 'd':
    interest = 2.6;
     cout << 'D' << interest;
    break;
  case 'F':
  case 'f':
    interest = 2.9;
     cout << 'F' << interest;
    break;
  case 'L':
  case 'l':
    interest = 3.8;
     cout << 'L' << interest;
    break;
  /*default:
    interest = 0.0;
     cout << "DEFAULT" << interest;*/
}//SWITCH

     }while (option!='q' || option !='Q');


return 0;
}


best regards!
      
            
            
      
0
 
suprapto45Commented:
Hi,

Try this. I am not C++ Programmer. I am Java Programmer but I did this in school three years ago. Just try that...if that is not working....please let me know :). I am just using my logic.

switch (option){
  case 'A':
  case 'a':
    interest = 2.3;
     cout << 'A' << interest;
    break;
  case 'D':
  case 'd':
    interest = 2.6;
     cout << 'D' << interest;
    break;
  case 'F':
  case 'f':
    interest = 2.9;
     cout << 'F' << interest;
    break;
  case 'L':
  case 'l':
    interest = 3.8;
     cout << 'L' << interest;
    break;
case 'Q':
case 'q':
   break;
default:
    interest = 0.0;
     cout << "DEFAULT" << interest;
}//SWITCH

     }while (option!='q' && option !='Q');

Regards
Dave
0
 
InteractiveMindCommented:
This seems to work for me:


#include <iostream>
using namespace std;

int main() {

     char option;
     double interest = 0.0;


     do {
     cout << endl;
     cout << "Enter Option A, D, F, L or Q to quit" <<endl;
     cin >> option;

switch (option){
  case 'A':
  case 'a':
    interest = 2.3;
     cout << 'A' << interest;
    break;
  case 'D':
  case 'd':
    interest = 2.6;
     cout << 'D' << interest;
    break;
  case 'F':
  case 'f':
    interest = 2.9;
     cout << 'F' << interest;
    break;
  case 'L':
  case 'l':
    interest = 3.8;
     cout << 'L' << interest;
    break;
  /*default:
    interest = 0.0;
     cout << "DEFAULT" << interest;*/
}//SWITCH

     }while (option!='q' && option !='Q');


return 0;
}

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Jaime OlivaresSoftware ArchitectCommented:
a shorter version:

#include <iostream>
#include "ctype.h"
using namespace std;

int main() {

     char option;
     double interest = 0.0;


     do {
     cout << endl;
     cout << "Enter Option A, D, F, L or Q to quit" <<endl;
     cin >> option;

     if (islower(option))
          option = toupper(option);

    switch (toupper(option)){
      case 'A':
        interest = 2.3;
        break;
      case 'D':
        interest = 2.6;
        break;
      case 'F':
        interest = 2.9;
        break;
      case 'L':
        interest = 3.8;
        break;
      default:
        interest = 0.0;
    }//SWITCH

    if (interest > 0.0)
         cout << option << ' ' << interest << endl;

     }while (option !='Q');

     return 0;
}


0
 
Julian HansenCommented:
Further simplification - although minor

you don't need to switch ( toupper(option)) because you already converted option to upper case in the previous statement you can simply

switch(option){
...
}

Also the

if (islower(option))

is not really necessary - you are not really saving anything by doing this as opposed to

option = toupper(option) ;


0
 
Jaime OlivaresSoftware ArchitectCommented:
if (islower(option))
          option = toupper(option);

IS necessary because toupper macro varies in different C++ compilers implementations, and can give you strange result if argument is not really a lowercase or an alphabetic character.

Countersense,
switch (toupper(option))

is not necessary (doesn't need toupper() call). I have just forgotten to remove it after simplification.
0
 
y_o_yCommented:
#include <iostream.h>
int main() {

     char option;
     double interest = 0.0;


     do {
     cout << endl;
     cout << "Enter Option A, D, F, L or Q to quit" <<endl;
     cin >> option;

switch (option){
  case 'A':
  case 'a':
    interest = 2.3;
     cout << 'A' << interest;
    break;
  case 'D':
  case 'd':
    interest = 2.6;
     cout << 'D' << interest;
    break;
  case 'F':
  case 'f':
    interest = 2.9;
     cout << 'F' << interest;
    break;
  case 'L':
  case 'l':
    interest = 3.8;
     cout << 'L' << interest;
    break;
  /*default:
    interest = 0.0;
     cout << "DEFAULT" << interest;*/
}//SWITCH

     }while (option!='q' && option !='Q');


return 0;
}


Send to a Friend    Printer Friendly       
            
      
      
            
            
      
Go Pro with Premium Services
5 times faster
We have the answers you want
Access more IT solutions
Access more IT experts

GO PRO NOW!
      
More Answers
Get instant IT solutions that solve your toughest IT problems, provide instant answers, and save thousands of jobs.
Power Search
Search our entire knowledge base for instant solutions.
Fast, organized, and powerful.
Find multiple solutions to your specific question.
Largest Solution Knowledge Base
Worlds largest IT solutions knowledge base.
Find multiple solutions to your specific question.
Personal Knowledge Base
Develop your own personal library of IT solutions.
No More Ads
Enough said.
Customized Navigation
Quick links provide one click navigation to the technology channel of your choice.
Premium Services Instant Help
Instant personal help from an EE concierge.
      
            
            
      
Comment from suprapto45
Date: 04/25/2005 06:41AM PDT
      Comment       

Hi,

Try this. I am not C++ Programmer. I am Java Programmer but I did this in school three years ago. Just try that...if that is not working....please let me know :). I am just using my logic.

switch (option){
  case 'A':
  case 'a':
    interest = 2.3;
     cout << 'A' << interest;
    break;
  case 'D':
  case 'd':
    interest = 2.6;
     cout << 'D' << interest;
    break;
  case 'F':
  case 'f':
    interest = 2.9;
     cout << 'F' << interest;
    break;
  case 'L':
  case 'l':
    interest = 3.8;
     cout << 'L' << interest;
    break;
case 'Q':
case 'q':
   break;
default:
    interest = 0.0;
     cout << "DEFAULT" << interest;
}//SWITCH

     }while (option!='q'|| option !='Q');
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.