• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

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;
}
0
MarcosInfo
Asked:
MarcosInfo
1 Solution
 
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
 
Jaime OlivaresCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 OlivaresCommented:
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
 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now