Like Tree1Likes
  • 1 Post By Salem

need help with a switch statement inside of a loop.

This is a discussion on need help with a switch statement inside of a loop. within the C Programming forums, part of the General Programming Boards category; Here is the code. Code: #include <stdio.h>#include <conio.h> #include <math.h> int main() { char choice; float num1,num2; do { printf("Please ...

  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
    2,553
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    > scanf("\n%s",&choice);
    You should use
    scanf(" %c",&choice);
    to read the next non-space character.
    AndiPersti likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 04: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, 12:26 AM
  5. switch statement that uses loop
    By mike in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2002, 04:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21