Thread: Menu Driven Array of Function Pointers Calculator, Help Needed

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    1

    Menu Driven Array of Function Pointers Calculator, Help Needed

    So, I am very much a beginner with working with C code, I have an assignment and I am in way over my head. The premise of this assignment is to create a menu driven, array of functions calculator. This is the code I have thus far (I haven't finished all of my comments yet, I've been to focused on clearing errors).

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    /*prototypes*/
    void sum (int a, int b);
    void dif (int a, int b);
    void pro (int a, int b);
    void quo (int a, int b);
    void printMenu();
    
    int main (void) /*begin program execution*/
    {
    
        printf("Hello, Welcome to the Calculator Program! This program allows you to select a mode of operation and enter two numbers to apply this operation.");
    
        void(*c[ 4 ]) (int, int) = {sum, dif, pro, quo};/*intialize array of 4 pointers to functions that each take an arguement and return void*/
        
        int userSelect = 0; /*menu selection*/
        
        do{
            printMenu();
            scanf_s("%d", &userSelect);
        } while (userSelect < 0 || userSelect > 4); /*end do...while*/
    
        if (userSelect = 1){
            sum (int a, int b);
        } /*end if*/
        else (userSelect = 2);{
            dif (int a, int b);
        } /*end else*/
        if (userSelect = 3){
            pro (int a, int b);
        }
        else (userSelect = 4);{
            quo (int a, int b);
        }
    
        return 0;
    
    } /*end main*/
    
    void printMenu()
    {
        printf("Menu:\n 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4. Division\n");
    } 
    
    void sum (int a, int b)
    {
        printf("Enter first number");
        scanf_s("%d", a);
        printf("Enter second number");
        scanf_s("%d", b);
        sum = int a + int b;
        printf("Sum is:%d\n", sum);
    
    }
    void dif (int a, int b)
    {
        printf("Enter first number");
        scanf_s("%d", a);
        printf("Enter second number");
        scanf_s("%d", b);
        dif = int a - int b;
        printf("Difference is:\n", dif);
    }
    void pro (int a, int b)
    {
        printf("Enter first number");
        scanf_s("%d", a);
        printf("Enter second number");
        scanf_s("%d", b);
        pro = int a * int b;
        printf("Product is:%d\n", pro);
    }
    void quo (int a, int b)
    {
    
        printf("Enter first number");
        scanf_s("%d", a);
        printf("Enter second number");
        scanf_s("%d", b);
        quo = a / b;
        printf("Quotient is:%d\n", quo);
    }
    I am using Visual Studio as a compiler, and I have the following errors:
    error C2144: syntax error : 'int' should be preceded by ')'
    error C2660: 'sum' : function does not take 0 arguments
    error C2059: syntax error : ')'
    error C2144: syntax error : 'int' should be preceded by ')'
    error C2660: 'dif' : function does not take 0 arguments
    error C2059: syntax error : ')'
    error C2144: syntax error : 'int' should be preceded by ')'
    error C2660: 'pro' : function does not take 0 arguments
    error C2059: syntax error : ')'
    error C2144: syntax error : 'int' should be preceded by ')'
    error C2660: 'quo' : function does not take 0 arguments
    error C2059: syntax error : ')'
    error C2062: type 'int' unexpected
    error C2062: type 'int' unexpected
    error C2062: type 'int' unexpected
    error C2659: '=' : function as left operand

    I can only assume that I am making simple mistakes, as I mention before, I am a novice at this and I was hoping some more experience programmers out there may be willing to help me a bit. Thank you.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    13
    The errors you are recieving are from the redelaration of variables everytime you use a variable. You only need to declare a variable once. Afterwords, you can refer to it without using its type like 'int a'.
    For example,
    int a, b;
    int sum;

    To use them just refer to the variable name

    sum = a + b;

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    void sum (int a, int b)
    {
        printf("Enter first number");
        scanf_s("%d", a);
        printf("Enter second number");
        scanf_s("%d", b);
        sum = int a + int b;
        printf("Sum is:%d\n", sum);
    }
    It looks like you need to reread how functions work. If you want to return a value you need to declare the appropriate type and use the return statement.

    Quote Originally Posted by SarahDaniella View Post
    (I haven't finished all of my comments yet, I've been to focused on clearing errors).
    Comments are only needed for parts of your code which need an additional explanation. Comments like
    Code:
    int main (void) /*begin program execution*/
    ...
    } /*end if*/
    are not necessary because it's pretty obvious what these lines of code do.
    See also How and Why to Comment C and C++ Code - Cprogramming.com

    Bye, Andreas

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
    else (userSelect = 4); <--- ';' should not be there, "=" should be "=="
    {
      quo (int a, int b);  <--- this is more of a declaration, you have miss understood  what needs to be done
    }
    You are passing a and b to the function, so you need to have them in the main function and then passed through, not at the start of each function

    You need to do something like
    Code:
    ...
    printf("Enter first number");
    scanf("%d", &a);
    printf("Enter second number");
    scanf("%d", &b);
    
    answer = c[userSelect-1](a,b);
    
    printf("Answer is: %d", answer);
    -> Note that I changed the functions to return an int type
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    You need to change the functions and your table of function pointers to return integers:

    Code:
    /*prototypes*/
    int sum (int a, int b);
    int dif (int a, int b);
    int pro (int a, int b);
    int quo (int a, int b);
    
    int main (void) /*begin program execution*/
    {
    /* ... */
    
         int (*c[ 4 ]) (int, int) = {sum, dif, pro, quo};/*intialize array of 4 pointers to functions ... */
    Then as part of the program assignment, you should be using the array of pointers to functions in c[] to call the functions base on the user selection.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. menu driven program help
    By vallamrao in forum C Programming
    Replies: 2
    Last Post: 09-10-2010, 04:56 AM
  2. Menu driven program
    By paushali in forum C Programming
    Replies: 10
    Last Post: 11-26-2007, 10:52 AM
  3. menu driven interface
    By weegi in forum C Programming
    Replies: 4
    Last Post: 04-17-2004, 04:08 PM
  4. C menu driven program
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-25-2002, 08:56 AM

Tags for this Thread