Thread: Please help debug

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

    Please help debug

    as of now it wont compile..when it did compile it wouldnt work after the user chose 1 or 2 (float or decimal)..any help would be appreciated!
    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\n");
            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;
                }
    
         case '2':
                  printf( "Enter B to add, E to subtract, J to divide, and K to multiply");
                  while ( (  choice2 = getchar() )  ) do
    
                    switch ( choice2 )
                      {
    
                      case 'B': case 'b':
                        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 'E': case 'e':
                        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 'J': case 'j':
                        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 'K': case 'k':
                        printf(" Enter integer 1 ");
                        scanf( "%f", &num3 );
                        printf(" Enter integer 2 ");
                        scanf( "%f", &num4);
                        ans = ( num3 * num4 );
                        break;
                      }
          }
    }

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    5
    Code:
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    C:\Personal\School\CS225\temp\case01.c:
    Warning W8060 C:\Personal\School\CS225\temp\case01.c 23: Possibly incorrect assignment in function main
    Warning W8060 C:\Personal\School\CS225\temp\case01.c 61: Possibly incorrect assignment in function main
    Error E2308 C:\Personal\School\CS225\temp\case01.c 98: do statement must have while in function main
    Warning W8070 C:\Personal\School\CS225\temp\case01.c 99: Function should return a value in function main
    *** 1 errors in Compile ***
    
    Tool completed with exit code 1
    You need to nest your while loops. Otherwise the compiler thinks it's a do/while loop. Just put braces after the while loop.

    Here's an example (the braces I refer to are colored red):
    Code:
    while ( condition ) {
    
        /* insert the parts you need repeated here */
    
    }
    And for when you need a do/while loop, here's how you implement it:
    Code:
    do {
    
       /* insert the parts you need repeated here */
       
    } while (condition);
    In either case (I see that you're trying to use both type of loops), the syntax is wrong.
    Last edited by Cornelius; 11-10-2002 at 10:25 PM.

  3. #3
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    This shows a fairly complete (on purpose) use of c's control statements.
    In c++ you can also add exceptions. Instead of
    using scanf("%d", &n) you may subsitute with
    call to read_int. Another way is to mess with scanf(" %d", &n)


    Code:
    #include <ctype.h>
    #include <limits.h>
    #include <stdio.h>
    
    #define EXPECTED_INT_ERROR 1
    #define OVERFLOW_ERROR 2
    
    int read_int(FILE* stream, int *error_code)
    {
        int n = 0;
        int c, m;
    
        if (error_code != NULL) {
            *error_code = 0;
        }
    
        /* skip ws */
        while((c = fgetc(stream)) != EOF && isspace(c))
            continue;
    
        if (isdigit(c)) {
            n = c - '0';
            while((c = fgetc(stream)) != EOF && isdigit(c)
                  && (error_code == NULL || *error_code == 0)) {
                m = c - '0';
                if (n > (INT_MAX - m)/10) {
                    if (error_code != NULL) {
                        *error_code = OVERFLOW_ERROR;
                    }
                }
                n *= 10;
                n += m;
            }
            ungetc(c, stream);
        }
        else if (error_code != NULL) {
            *error_code = EXPECTED_INT_ERROR;
        }
    
        return n;
    }
    
    int main(void)
    {
        int n, m;
        int error = 0;
    
        n = read_int(stdin, &error);
        if (error != 0)
            goto print_error;
    
        m = read_int(stdin, &error);
        if (error != 0)
            goto print_error;
    
        printf("n = %d\n", n);
        printf("m = %d\n", m);
        goto quit;
    
     print_error:
        switch(error) {
        case EXPECTED_INT_ERROR:
            printf("Expected integer\n");
            break;
        case OVERFLOW_ERROR:
            printf("Overflow on input\n");
            break;
        default:
            printf("Unknown error\n");
        }
     quit:
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary not built with debug info - why?
    By ulillillia in forum C Programming
    Replies: 15
    Last Post: 12-11-2008, 01:37 AM
  2. makefiles - debug & release?
    By cpjust in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 04:00 PM
  3. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  4. Results in Debug and Release mode are different
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-27-2006, 11:08 AM
  5. Ask about Debug Assert Failed
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 11:07 PM