3

This issue I feel is more my understanding of pointers but here goes. I am suppose to create a system program in C that performs calculations as such math operator value1 value2. Example math + 1 2. This would produce 3 on the screen. I am having troubles comparing or summing the numbers. Here is what I have so far:

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

int main( int ac, char* args[] )
{
    int total;
    if (strcmp(*++args,"+") == 0)
    {

    }
    printf("Total= ", value1);
    if (strcmp(*args,"x") == 0)
        printf("multiply");
    if (strcmp(*args,"%") == 0)
        printf("modulus");
    if (strcmp(*args,"/") == 0)
        printf("divide");
    return 0;
}

I can do a string compare to get the operator but am having a hard time adding the two values. I have tried:

int value1=atoi(*++args);

Any help would be appreciated.

12
  • 2
    I voted for this question to be moved to SO (since it is more about programming than *nix. However, I can tell you that that code could use some serious tender love and care (please use braces for all conditional statements (read: gotofail), it looks like a switch statement might help out, indentation is your friend). Best of luck! Commented Jan 11, 2015 at 4:31
  • Unless I am wrong there is no harm in omitting braces when the if statement has only one line. As you can see I did use braces for the first if because I was using more than 1 line when testing. Also the indents are missing due to pasting it in and having to use four spaces for display as code. Commented Jan 11, 2015 at 4:43
  • 1
    Here, let me fix the indents for you. But you should always use braces for any block statement. In C, technically, you do not have to if it is one line, but read up on Apple's Gotofail if you think it's never caused harm. Commented Jan 11, 2015 at 4:45
  • If I were you, I'd a) never increment args, and b) if I absolutely had to increment args, increment it after the text has succeeded: if (strcmp(*args), ..) {args++; .. }. What inspired args++ instead of using an index variable? Commented Jan 11, 2015 at 5:33
  • 1
    @PM2Ring Which is why continuing to do it is the sign of a bad prof. Commented Jan 11, 2015 at 6:13

2 Answers 2

1
*++args

Since you are doing a pre-increment ++ operator has higher precedence than * so the pointer is incremented and later you are dereferencing it which case you might never get to access the arguement which you actually intend to.

If you have input like

+ 1 2

We have

args[1] = +

args[2] = 1

args[3] = 2;

Why can't just access atoi(args[2])

You can do something like

int main(int argc, char **args)
{
    if(argc != 4)
    {
        printf("Fewer number of arguements\n");
        return 0;
    }

    else if((strcmp(args[1],"+")) == 0)
    {
      printf("Sum = %d\n",atoi(args[2]) + atoi(args[3]));
    }

    return 0;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I can't suggest edits on this site yet, but the inconsistent indentation bugs me so! Fix it please? :D
Worked great and your explanation helped me understand what I was doing wrong.
@Gopi: This suggested edit was just rejected. I initially though I had made a blunder, but now I think perhaps not. It is unclear whether the indexes refer to args before or after the increment. Also, I have no idea what "Why can't just access..." means. Your answer would benefit from some clarification.
1

Instead of accessing the command line args via pointers, easily you can do this using array referencing. For example, "math + 1 2", args[0] is math, args[1] will be + etc.

int main( int ac, char* args[] )
{
    if(ac < 4)
    {
        printf("Invalid Argument");
        return 0;
    }
    if (strcmp(args[1],"+") == 0)
    {
        int x = atoi(args[2]);
        int y = atoi(args[3]);
        printf("%d + %d = %d\n", x,y, x + y);
    }
    if (strcmp(args[1],"x") == 0)
    {
        int x = atoi(args[2]);
        int y = atoi(args[3]);
        printf("%d * %d = %d\n", x,y, x * y);
    }
    if (strcmp(args[1],"%") == 0)
    {
        int x = atoi(args[2]);
        int y = atoi(args[3]);
        if(y == 0)
            return 0;

        printf("%d %% %d = %d\n", x,y, x % y);
    }
    if (strcmp(args[1],"/") == 0)
    {
        int x = atoi(args[2]);
        int y = atoi(args[3]);

        if(y == 0)
            return 0;
        printf("%d / %d = %d\n", x,y, x / y);
    }
    return 0;
}

1 Comment

Thanks for your explanation, Worked like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.