help with switch statements

This is a discussion on help with switch statements within the C Programming forums, part of the General Programming Boards category; Hello, I'm trying to do a project which will add/subtract/multiply/divide two integers, the user chooses whether its float or integer ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    6

    help with switch statements

    Hello, I'm trying to do a project which will add/subtract/multiply/divide two integers, the user chooses whether its float or integer and then inputs the two integers. Can anybody help debug my code or point me in the right direction? Thanks.
    Code:
    #include <stdio.h>
    
    int main()
    {
      int choice,num1,num2,ans;
      char choice2;
      float num3,num4,ans2;
    
      printf( "Enter 1 for integers, 2 for floats, 0 to quit" );
    
      while ( ( choice = getchar()  ) != EOF )
    
        switch ( choice )
          {
    
          default: printf("Error, invalid key pressed");
            break;
    
    
          case '1':
            printf( "Enter A to add, S to subtract, D to divide, and M to multiply");
            while ( ( choice2 = getchar() != EOF ) {
    
              switch ( choice2 )
                {
    
                case 'A': case 'a':
                  printf( "Enter integer 1: " );
                  scanf( "%d", &num1 );
                  printf( "Enter integer 2: ");
                  scanf( "%d", &num2 );
                  ans = ( num1 + num2 );
                  printf( "Answer: %d + %d = %d",num1,num2,ans);
                  break;
                case 'S': case 's':
                  printf( "Enter integer 1 ");
                  scanf( "%d", &num1 );
                  printf( "Enter integer 2 ");
                  scanf( "%d", &num2 );
                  ans = ( num1 - num2 );
                  printf( "Answer: %d - %d = %d",num1,num2,ans );
                  break;
                case 'D': case 'd':
               printf( "Enter integer 1 ");
                  scanf( "%d", &num1 );
                  printf( "Enter integer 2 ");
                  scanf( "%d", &num2 );
                  ans = ( num1 / num2 );
                  printf(" Answer: %d // %d = %d",num1,num2,ans);
                  break;
                case 'M': case 'm':
                  printf(" Enter integer 1 ");
                  scanf( "%d", &num1 );
                  printf(" Enter integer 2 ");
                  scanf( "%d", &num2);
                  ans = ( num1 * num2 );
                  break;
                  break;
    
         case '2':
                  printf( "Enter A to add, S to subtract, D to divide, and M to multiply");
                  while ( ( choice2 = getchar() != EOF ) {
    
                    switch ( choice2 )
                      {
    
                      case 'A': case 'a':
                        printf( "Enter integer 1: " );
                        scanf( "%f", &num3 );
                        printf( "Enter integer 2: ");
                        scanf( "%f", &num4 );
                        ans = ( num3 + num4 );
                        printf( "Answer: %f + %f = %f",num3,num4,ans2);
                        break;
                      case 'S': case 's':
                        printf( "Enter integer 1 ");
                        scanf( "%f", &num3 );
                        printf( "Enter integer 2 ");
                        scanf( "%f", &num4 );
                        ans = ( num3 - num4 );
                        printf( "Answer: %f - %f = %f",num3,num4,ans2 );
                        break;
                      case 'D': case 'd':
                        printf( "Enter integer 1 ");
                        scanf( "%f", &num3 );
                        printf( "Enter integer 2 ");
                        scanf( "%f", &num4 );
                        ans = ( num3 / num4 );
                        printf(" Answer: %f // %f = %f",num3,num4,ans2);
                        break;
                      case 'M': case 'm':
                        printf(" Enter integer 1 ");
                        scanf( "%f", &num3 );
                        printf(" Enter integer 2 ");
                        scanf( "%f", &num4);
                        ans = ( num3 * num4 );
                        break;
          break;
    
                      }
      }
                  }
                          )

    &#91;code]&#91;/code]tagged by Salem

  2. #2
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140
    Remove the ' ' from around your numbers -

    case '1': becomes case 1:

    Code:
    case 'M': case 'm':
    printf(" Enter integer 1 ");
    scanf( "%d", &num1 );
    printf(" Enter integer 2 ");
    scanf( "%d", &num2);
    ans = ( num1 * num2 );
    break;
    break;
    There is no need for a second 'break;' here, it's shown on my compiler as unreachable code (probably because it is)

    Your program ends with a closing bracket ' ) ' that can't be right, also your while statement is missing a closing bracket.

    It'd probably be better if you listed what problems you were having with the code...
    Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy - Groucho Marx

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    6
    errors:
    irix2[9]% gcc -ansi -Wall proj4.c
    proj4.c: In function `main':
    proj4.c:34: parse error before `switch'
    proj4.c:72: parse error before `{'
    proj4.c:85: duplicate case value
    proj4.c:45: this is the first entry for that value
    proj4.c:85: duplicate case value
    proj4.c:45: this is the first entry for that value
    proj4.c:93: duplicate case value
    proj4.c:53: this is the first entry for that value
    proj4.c:93: duplicate case value
    proj4.c:53: this is the first entry for that value
    proj4.c:101: duplicate case value
    proj4.c:61: this is the first entry for that value
    proj4.c:101: duplicate case value
    proj4.c:61: this is the first entry for that value
    proj4.c:110: warning: control reaches end of non-void function
    proj4.c: At top level:
    proj4.c:111: parse error before `}'

    code:
    Code:
    #include <stdio.h>
    
    int main()
    {
      int choice,num1,num2,ans;
      char choice2;
      float num3,num4,ans2;
    
      printf( "Enter 1 for integers, 2 for floats, 0 to quit" );
    
      while ( ( choice = getchar()  ) != EOF )
    
        switch ( choice )
          {
    
          default: printf("Error, invalid key pressed");
            break;
    
    
          case 1:
            printf( "Enter A to add, S to subtract, D to divide, and M to multiply");
            while ( ( choice2 = getchar() != EOF )
    
              switch ( choice2 )
                {
    
                case 'A': case 'a':
                  printf( "Enter integer 1: " );
                  scanf( "%d", &num1 );
                  printf( "Enter integer 2: ");
                  scanf( "%d", &num2 );
                  ans = ( num1 + num2 );
                  printf( "Answer: %d + %d = %d",num1,num2,ans);
                  break;
                case 'S': case 's':
                  printf( "Enter integer 1 ");
                  scanf( "%d", &num1 );
                  printf( "Enter integer 2 ");
                  scanf( "%d", &num2 );
                  ans = ( num1 - num2 );
                  printf( "Answer: %d - %d = %d",num1,num2,ans );
                  break;
                case 'D': case 'd':
                  printf( "Enter integer 1 ");
                  scanf( "%d", &num1 );
                  printf( "Enter integer 2 ");
                  scanf( "%d", &num2 );
                  ans = ( num1 / num2 );
                  printf(" Answer: %d // %d = %d",num1,num2,ans);
                  break;
                case 'M': case 'm':
                  printf(" Enter integer 1 ");
                  scanf( "%d", &num1 );
                  printf(" Enter integer 2 ");
                  scanf( "%d", &num2);
                  ans = ( num1 * num2 );
                  break;
                  break;
    
         case 2:
                  printf( "Enter A to add, S to subtract, D to divide, and M to multiply");
                  while ( ( choice2 = getchar() ) {
    
                    switch ( choice2 )
                      {
    
                      case 'A': case 'a':
                        printf( "Enter integer 1: " );
                        scanf( "%f", &num3 );
                        printf( "Enter integer 2: ");
                        scanf( "%f", &num4 );
                        ans = ( num3 + num4 );
                        printf( "Answer: %f + %f = %f",num3,num4,ans2);
                        break;
                      case 'S': case 's':
                        printf( "Enter integer 1 ");
                        scanf( "%f", &num3 );
                        printf( "Enter integer 2 ");
                        scanf( "%f", &num4 );
                        ans = ( num3 - num4 );
                        printf( "Answer: %f - %f = %f",num3,num4,ans2 );
                        break;
                      case 'D': case 'd':
                        printf( "Enter integer 1 ");
                        scanf( "%f", &num3 );
                        printf( "Enter integer 2 ");
                        scanf( "%f", &num4 );
                        ans = ( num3 / num4 );
                        printf(" Answer: %f // %f = %f",num3,num4,ans2);
                        break;
                      case 'M': case 'm':
                        printf(" Enter integer 1 ");
                        scanf( "%f", &num3 );
                        printf(" Enter integer 2 ");
                        scanf( "%f", &num4);
                        ans = ( num3 * num4 );
                        break;
    
                      }
      }
                  }
    Code tagged by Hammer

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There are still problems with the brackets, at least one is here:

    >>while ( ( choice2 = getchar() != EOF )

    Also, use code tags when posting code please. There's a link in my signature telling you how.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Files Inside Switch Statements
    By arealfind08 in forum C Programming
    Replies: 11
    Last Post: 03-17-2009, 04:49 PM
  2. Switch Statements
    By lazyturtle in forum C++ Programming
    Replies: 12
    Last Post: 05-02-2007, 02:40 AM
  3. Explanation of switch statements
    By ammochck21 in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2006, 01:59 PM
  4. arrays within switch statements
    By divinyl in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2003, 01:56 PM
  5. Switch statements for strings
    By cxs00u in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2002, 03:38 PM

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