Thread: need help with a switch statement inside of a loop.

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    12

    need help with a switch statement inside of a loop.

    Here is the code.

    Code:
    #include <stdio.h>#include <conio.h>
    #include <math.h>
    
    
    int main()
    {
        char choice;
        float num1,num2;
        
       
       do
       {
        printf("Please enter a numbers number: ");
        scanf("%f", &num1);
        printf("Please enter another number: ");
        scanf("%f", &num2);
        printf("Please select the option: ");
        printf("\n a or A for addition");
        printf("\n s or S for subtraction");
        printf("\n m or M for Multiplication");
        printf("\n d or D for Division");
        printf("\n e or E to exit the program:  ");
        printf("\n    ");
        scanf("\n%s",&choice);
        
        
        
        
         switch (choice)
         {
               case 'A':
               case 'a':
                    
                    printf("The sum of the two numbers are: %5.2f\n", num1 + num2);
                    break;
               
               case 'S':
               case 's':
                    
                    printf("The total is: %5.2f\n", num1 - num2);
                    break;
               
               case 'M':
               case 'm':  
                    
                    printf("The total is: %5.2f\n", num1 * num2);
                    break;
                    
               case 'D':
               case 'd':
                    
                    printf("The total is: %5.2f\n", num1 / num2);
                    break;
                    
               case 'E':
               case 'e':
                    break;
                    default: 
                    printf("\nPlease pick a valid option:");
                    
               
                 
                  
                   
      }
      
    }while(choice != 'e' || 'E');
        return 0;
        getch();
    
    
    }
    My problem is when i use e or E the program will not exit. When i take the loop out it works. I need the program to loop until i use e or E. ive tried a few things but no such luck. Any pointers or tips?

    any help would be greatly appreciated!

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Turn on Compiler Warnings!

    This is NOT correct.
    Code:
    while(choice != 'e' || 'E');
    Closer to being right. Code not tested!
    Code is likely still wrong; but, should compile without warnings.

    Code:
    while(choice != 'e' || choice != 'E');
    Hint: Why or instead of and?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    12
    ah
    Code:
    while(choice != 'e' && choice != 'E');
    ya i ran it before and i didnt get any warnings. Thanks for the hint and im going to turn on the warning messages.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > scanf("\n%s",&choice);
    You should use
    scanf(" %c",&choice);
    to read the next non-space character.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 08-06-2009, 07:20 AM
  2. infinite while loop inside a switch statement.
    By tummala_005 in forum C Programming
    Replies: 6
    Last Post: 12-15-2008, 05:46 PM
  3. switch statement inside a do while
    By Chaplin27 in forum C++ Programming
    Replies: 4
    Last Post: 09-14-2004, 08:33 PM
  4. cin.get() inside switch statement
    By timberwolf5480 in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 01:26 AM
  5. switch statement that uses loop
    By mike in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2002, 05:47 PM