0

I want the user to enter a value liked ADD or SUB to perform the desired operation but I'm getting "array subscript is not a integer instead". I know I can enter digits instead like 0, 1 , 2 etc. because of how enums work but I want to make it more user friendly. I can't find any information online about this as well, and my books are of not helping. Help please!

#include<stdio.h>
#include<stdlib.h>

int multiply(int , int);
int sum(int, int);
int divide(int, int);
int sub(int, int);

int(*calc[])(type) = {sum, sub, multiply, divide};
enum response{ADD, SUB, MULT, DIV};
enum response type;

int main()
{
    int a, b;
    char s[4];
    printf("Enter two numbers and the operation. Operations: ADD, SUB, MULT, DIV");
    scanf("%d %d %s", &a, &b, &s);
    printf("The result = %d",(calc[s])(a, b));
    return 0;
}
int multiply(int a, int b)
{
    return a*b;
}

int sub(int a, int b)
{
    return a-b;
}
int sum(int a, int b)
{
    return a+b;
}
int divide(int a, int b)
{
    return a/b;
}



JerSci
  • 167
  • 1
  • 9
  • 2
    You must read a string and compare that to the values `"ADD"`,.... and select the appropriate routines based on the string entered. – Paul Ogilvie Jan 07 '20 at 14:18
  • 2
    Do not pass `&s` to `scanf` as `s` is already an array. Just use `s`. – Paul Ogilvie Jan 07 '20 at 14:19
  • 2
    For clarity: enums are just text inside the program and do not exist anymore after compilation. – Paul Ogilvie Jan 07 '20 at 14:20
  • 2
    Did you turn warnings of your compiler on? Because `int(*calc[])(type)` should be `int(*calc[])(int,int)` – Paul Ogilvie Jan 07 '20 at 14:22
  • 1
    @PaulOgilvie: Re “Do not pass…”: That does not convey the information to a beginner that you intend. The fact that `s` is an array means it is not a pointer/address, but a beginner may know that the argument corresponding to `%s` must be a pointer, and that is why they took the address with `&`. The information they are missing is not that `s` is an array but that `%s` requires a pointer to `char`, and an array will be automatically converted to a pointer to its first element. Telling them it is “already an array” does not tell them why passing an array when a pointer is needed works. – Eric Postpischil Jan 07 '20 at 14:23
  • do you mean a condition like if(s == "ADD) or strcmp(s, "ADD) and if true, then perform (calc[ADD])(a, b)? – JerSci Jan 07 '20 at 14:27
  • `s` is not an integer and that's it. Array subscripting needs integers, be it arrays of function pointers or arrays of bananas. – Lundin Jan 07 '20 at 15:35

1 Answers1

0

Enum constants are essentially symbolic names for integer constants. They are not the same as strings, so sou can't directly use the latter as a substitute for the former.

You need to check the value of the given string and select the appropriate enum based on that.

enum response getResponse(const char *str)
{
    if (!strcmp(str, "ADD")) {
         return ADD;
    } else if (!strcmp(str, "SUB")) {
         return SUB;
    } else if (!strcmp(str, "MULT")) {
         return MULT;
    } else if (!strcmp(str, "DIV")) {
         return DIV;
    } else {
         return (enum response)-1;
    }
}

int main()
{
    int a, b;
    char s[4];
    scanf("%d %d %4s", &a, &b, s);
    enum response resp = getResponse(s);
    if (resp == (enum response)-1) {
        printf("invalid operation\n");
    } else {
        printf("The result = %d",(calc[resp])(a, b));
    }
    return 0;
}

Also, the type of calc is incorrect. It should specify the types of each argument of the array members:

int(*calc[])(int, int) = {sum, sub, multiply, divide};
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Thank you! This makes a lot of sense. Is there anyway to make it simpler, because that was my initial purpose? Thank you so much, again. – JerSci Jan 07 '20 at 14:33
  • @JerSci — You might need to use 'X-Macros' to get the names and the values. For example, see [Real world use of X-Macros](https://stackoverflow.com/q/6635851/15168) or [What is a good reference documenting patterns of use for X-Macros in C?](https://stackoverflow.com/q/264269/15168) – Jonathan Leffler Jan 07 '20 at 14:57