This is a simple calcualtor program. What I don't understand is how I could modify it so that the calculation parameters could be intered from cmd. I don't understand how the argv and argc really work in this case. Do I need to replace all the ints with argv or something?

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

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

int num1, num2, result;
char op;
int check=0;

do{ 
    printf("Enter a calculation or press q to quit: ");
    scanf("%d%c%d", &num1, &op, &num2);

    if (op=='q'){
    check = 1;
    return 0; 
}
    else{
        if  (op =='+') 
        {result = num1+num2;
        printf("%d\n", result);}
        else if
        (op =='-') {result = num1-num2;
        printf("%d\n", result);
        }
        else if
        (op =='*') {result = num1*num2;
        printf("%d\n", result);
        }
        else if
        (op =='/') {result = num1/num2;
        printf("%d\n", result);
        }
        }
}while(!check);
return 0;       

}

Recommended Answers

All 4 Replies

Depends on how you want it to work. If you expect the formula as a single string you could do something like:

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

int main(int argc, char *argv[])
{
    int num1, num2, result;
    char op;
    int check=0;

    if (argc != 2)
    {
        printf("usage: %s <formula>", argv[0]);
    }
    else
    {
        sscanf(argv[1], " %d %c %d ", &num1, &op, &num2);

        if (op == 'q')
        {
            check = 1;
            return 0;
        }
        else
        {
            if  (op =='+')
            {
                result = num1+num2;
                printf("%d\n", result);
            }
            else if (op =='-')
            {
                result = num1-num2;
                printf("%d\n", result);
            }
            else if (op =='*')
            {
                result = num1*num2;
                printf("%d\n", result);
            }
            else if (op =='/')
            {
                result = num1/num2;
                printf("%d\n", result);
            }
        }
    }

    return 0;
}

example in- and output:

C:\>test 1+5
6

C:\>test "9 * 20"
180

C:\>test "    23  -        15      "
8

Another way is to see every formula element as a seperate command line parameter:

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

int main(int argc, char *argv[])
{
    int num1, num2, result;
    char op;
    int check=0;

    if (argc != 4)
    {
        printf("usage: %s <number> <operation> <number>", argv[0]);
    }
    else
    {
        sscanf(argv[1], "%d", &num1);
        sscanf(argv[2], "%c", &op);
        sscanf(argv[3], "%d", &num2);

        if (op == 'q')
        {
            check = 1;
            return 0;
        }
        else
        {
            if  (op =='+')
            {
                result = num1+num2;
                printf("%d\n", result);
            }
            else if (op =='-')
            {
                result = num1-num2;
                printf("%d\n", result);
            }
            else if (op =='*')
            {
                result = num1*num2;
                printf("%d\n", result);
            }
            else if (op =='/')
            {
                result = num1/num2;
                printf("%d\n", result);
            }
        }
    }

    return 0;
}

example in- and output:

C:\>test 3 + 9
12

C:\>test     35    /     7
5

Note that you should be weary of shell extensions, for example when using '*'. Also, the program itself contains some flaws, although it's not asked here so I won't go into them.

Thank you so much Gonobe I needed to see the code to understand it!

To answer your question about how to use argc and argv, argc is an int. It represents the number or strings that are seperated by white space in the commandline. argv is an array of char pointers or strings. The values of each index is the information entered on the commandline. argv[0] is always the program name. Each other index is the characters up to a white space. Hope this helps.

argv[0] is always the program name.

Not always. If argc is greater than zero, argv[0] is allowed to be an empty string, should the program name not be available for some reason. Even if argv[0] isn't an empty string, the format of the "program name" is implementation dependent. It's generally a safe assumption to expect either just the file name of the executable or the full path, but that's still an assumption.

Each other index is the characters up to a white space.

The command line interpreter will handle quoted strings that have embedded whitespace (assuming such a thing is supported by the system), so the "up to a whitespace" part isn't entirely accurate. For example, take this program:

#include <stdio.h>

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

    for (i = 0; i < argc; ++i)
        printf("'%s'\n", argv[i]);

    return 0;
}

If the command line contains $prog.exe this "is a" test, the output will be something along the lines of

'prog.exe'
'this'
'is a'
'test'

Also, don't forget that argv[argv] is guaranteed to be NULL, even if argc is 0. So on top of having a count of arguments in the form of argc, you also have a sentinel value at the end of argv if argc is not available for some reason (like argv being passed to a function) or pointer arithmetic over the argv array is faster than indexing on your system.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.