-1

I need some help with my simple calculator program. It seems to work fine until I use the * symbol for multiplication. When I use the * symbol it comes out to be 99 instead of the ASCII equivalent of 42. The arguments it expects are an integer, operator(+ ,-, *, /) and another integer.

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


/*
void usage() {
    printf("This is a calculator program, just put in to numbers and and operator\n");
    printf("Example:\n\t2 + 2\n");

}
*/

int main(int argc, char *argv[]) {

    int first_number, second_number;
    int symbol;
    int sum;

    first_number = atoi(argv[1]);
    second_number = atoi(argv[3]);
    symbol = (int)*argv[2];


    printf("symbol varable = %d\n", symbol); // debugging for argv[2]

    if (symbol == 43 ) {

        sum = first_number + second_number;

        printf("sum = %d\n", sum);
    }

    else if (symbol == 45 ) {

        sum = first_number - second_number;

        printf("sum = %d\n", sum);
    }

    else if (symbol == 42) {

        sum = first_number * second_number;

        printf("sum = %d\n", sum);

    }
     else if (symbol == 47) {

        sum = first_number / second_number;

        printf("sum = %d\n", sum);
    }
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    It's cool that you are learning how to code, but I think you should also learn [how to ask](https://stackoverflow.com/help/how-to-ask). – Henri Menke Jun 27 '17 at 00:31
  • Just a tip: Using explicit characters instead of trying to use ASCII values (`'+'` instead of `43`) makes the code more readable and portable. – e0k Jun 27 '17 at 00:34
  • Please [edit] your question title to something that describes the problem or question you're asking. The title should be useful to a future reader who sees it in a list of search results. *Please help me with my calculator* is absolutely meaningless, *having some issues* is simply useless noise, *(C language)* is apparent from the tag, and you can show your appreciation by upvoting or accepting (or both) answers that help you instead of saying it in the title. Thanks. – Ken White Jun 27 '17 at 00:48
  • 1
    Your problem is that you have a program (possibly called `calculator`) in your directory, and when you run `./calculator 9 * 23`, the `*` is expanded by the shell so your program is passed `./calculator 9 calculator calculator.c 23` or something similar, and your code sees the `c` and finds code `99`. To run the program accurately, use `./calculator 9 '*' 23` (or `./calculator 9 \* 23`, or `./calculator 9 "*" 23`). Basic debugging technique: print your inputs! – Jonathan Leffler Jun 27 '17 at 01:03

1 Answers1

3

The problem has nothing to do with your code.

When you run your program with command line parameters like 12 * 12 you are putting a wildcard in the command line and the shell sees the * and replaces it with a list of every filename in the directory - The first program in the directory must start with a lower case c if the symbol comes out with 99.

To make it work escape your command line parameters like 12 '*' 12 or 12 \* 12 or disable filename globbing altogether as shown in this answer: Stop shell wildcard character expansion?

Jerry Jeremiah
  • 9,045
  • 2
  • 23
  • 32
  • 1
    So the program isn't called `a.out` because then the `*` would be expanded to a name that begins with `a` (`97`) instead of `c` (`99`). I agree with the rest of your diagnosis. I'd also recommend `printf("[%s] [%s] [%s]\n", argv[1], argv[2], argv[3]);` to diagnose the problem. The code should also check `if (argc < 4)` before using the arguments. – Jonathan Leffler Jun 27 '17 at 01:07
  • 1
    I used a.out because he didn't say what it was called and that's the default. But you're right - it must be called calc or calculator or something in order for the first file to start with c. I should change it so it makes more sense but it will only be really helpful if I guess the name of the executable correctly... – Jerry Jeremiah Jun 27 '17 at 01:15
  • See my comment to the main question for how I'd do it. – Jonathan Leffler Jun 27 '17 at 01:16