Help with C programming

This is a discussion on Help with C programming within the C Programming forums, part of the General Programming Boards category; Given an expression with three operands and two operators as input, evaluate the expression while following the rules of operator ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    Exclamation Help with C programming

    Given an expression with three operands and two operators as input, evaluate the expression while following the rules of operator precedence.

    Example Execution #1:
    Enter the expression in the form [int char int char int]: 4 + 5 + 6
    (4 + 5) + 6 = 15

    These are the instructions for the code i'm supposed to me writing and this is the code I've managed to write:

    Code:
    
    #include <stdio.h>
    
    int getInput (int *, char *, int *, char *, int *);
    int logiCalc (int *, char *, int *, char *, int *);
    
    int main ()
    
    {
      //LOCAL DECLARATION
    
      int numA;  // FIRST INTEGER
      int numB;  // SECOND INTEGER
      int numC;  // THIRD INTEGER
      char opA;  // FIRST OPERAND
      char opB;  // SECOND OPERAND
      int input; // CALLING INPUT FUNCTION
      int result; //CALLING THE OPERAND FUNCTION
    
    
      // EXECUTABLE STATEMENTS
    
     input =  getIntput(&numA, &opA, &numB, &opB, &numC);
     result = logiCalc (&numA, &opA, &numB, &opB, &numC);
    
     return(0);
    
    }
    
    int getInput (int numA, char opA, int numB, char opB, int numC)
    
    {
      printf("Enter the expression in the form [int char int char int]:");
    
      scanf("%d %c %d %c %d", &numA, &opA, &numB, &opB, &numC);
    
      return (0);
    }
    
    int logiCalc (int numA, char opA, int numB, char opB, int numC)
    
    {
      switch (opA)
      {
        case '+': printf("%d + %d", numA, numB);
                  break;
    
     case '-': printf("%d - %d", numA, numB);
                  break;
        case '*': printf("(%d * %d", numA, numB);
                  break;
        case '/': printf("(%d / %d", numA, numB);
                  break;
        case '%': printf("(%d %% %d", numA, numB);
                  break;
        default:  printf("Enter the correct operator\n");
      }
    
      switch (opB)
      {
        case '+': printf("+ %d)\n", numC);
                  break;
        case '-': printf("- %d)\n", numC);
                  break;
        case '*': printf("* %d)\n", numC);
                  break;
        case '/': printf("/ %d)\n", numC);
                  break;
        case '%': printf("%% %d)\n", numC);
                  break;
        default:  printf("Enter the correct operator\n");
      }
    
      return(0);
    }
    I'd like some assistance as to how to proceed as i think there are a few problems with my logic. Also there is an error when i compile this code: conflicting types in the both the user defined functions.

    It would be helpful to know what the error is about.
    Last edited by Salem; 03-07-2011 at 03:22 AM. Reason: Fixed code tags

  2. #2
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Your prototype functions' arguments are pointers, whereas in your actual functions, they are not. Thus there is a mismatch between your function prototypes & your actual function declarations.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    Exclamation

    I'm very new to programming and don't really know how to fix this error...could you please explain how I could do that?

  4. #4
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Compare your functional declaration to your prototype:
    int logiCalc (int numA, char opA, int numB, char opB, int numC)
    int logiCalc (int *, char *, int *, char *, int *);

    int x; //this is an integer, x
    int *x; //this is a pointer to an integer x
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    Question

    Thx that makes a lot more sense now!!!

    And could you tell me if my logic is correct for the given problem and execution statement

  6. #6
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    First thing, write your code in code tags..... e.g
    Code:
    Your code here
    So that it would be easy to understand and read.

Popular pages Recent additions subscribe to a feed

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