0

I am using C Programming Language.

I write a simple code for arithmetic operations using command-line arguments, but i have a simple problem. when i open the command-line console from windows to test my program. the multiply operation not worked but the other operations work successfully.

here is my code:

#include <stdio.h>
int main(int argc, char *argv[])
{
    if (argc != 4)
    {
        printf("Error: you must use 4 arguments\n\n");
        return 1;
    }

    int x = argv[1][0] - '0';
    int y = argv[3][0] - '0';
    int z = 0;

    if (strcmp(argv[2], "+") == 0)
        z = x + y;
    else if (strcmp(argv[2], "-") == 0)
        z = x - y;
    else if (strcmp(argv[2], "*") == 0)
        z = x * y;
    else
        z = x / y;
    printf("Result = %d\n\n", z);
    return 0;
}
Hamada Hosny
  • 63
  • 2
  • 8
  • 10
    * (Asterisk) is the wildcard character in command prompt. Try "*" OR \*. – skrtbhtngr Jun 06 '15 at 16:02
  • 1
    @skrtbhtngr That should have been an actual answer. I'd upvote it. – mtijanic Jun 06 '15 at 16:12
  • @skrtbhtngr not working, same problem exists. – Hamada Hosny Jun 06 '15 at 16:13
  • @skrtbhtngr: the `*` is only expanded as a wildcard on Linux-like systems, and OP is on Windows. (Which is kind of important enough to write with a capital.) – Jongware Jun 06 '15 at 16:22
  • Try `if (argv[2][0] == '*') ...` rather than string comparison – pmg Jun 06 '15 at 16:22
  • 2
    Do not tell us "it did not work" – we can infer as much because we can already guess it doesn't. What does it output? If it prints the line `Error: you must use 4 arguments`, then add a line to your code that outputs *what your program thinks it got instead*. – Jongware Jun 06 '15 at 16:24
  • @Jongware: See this : https://technet.microsoft.com/en-us/library/bb490639.aspx – skrtbhtngr Jun 06 '15 at 16:26
  • * is speacial because it means something to the command shell. In particular it means - any characters in the file name. If there are spaces around it that would mean - any file. As pradheep suggests below - try with backslash. – fukanchik Jun 06 '15 at 16:27
  • @skrtbhtngr: only if you see this: [Is there any way to get the windows cmd shell to expand wildcard paths?](http://superuser.com/a/460609), or (even better) https://msdn.microsoft.com/en-us/library/e1w828yy.aspx: ".. which by default does not expand wildcards into separate strings in the argv string array" – Jongware Jun 06 '15 at 16:27
  • 1
    @HamadaHosny Do as Jongware suggested. After the error appears (and before you `return 1;`), print `argc` for example. I'd also try printing each item in `argv` to display the arguments that the command interpreter was sending to your program: `fprintf(stderr, "argc: %d\nargv:", argc); while (*argv) fprintf(stderr, "\t%s\n", *argv++);` That will aid in determining whether wildcard expansion is a factor here or not, though it shouldn't be, according to the documentation. –  Jun 06 '15 at 16:41

2 Answers2

2

Run with astreik

$ ./a.out 1  +  3 
Result = 4 

$ ./a.out 1  \*  3 
Result = 3 
Pradheep
  • 2,973
  • 21
  • 32
1

Try using \* (backslash before the *) when running your program.

Dan Getz
  • 7,743
  • 6
  • 29
  • 60
Klaus
  • 11
  • 1