How to write a calculator program in c language using command line arguments??? The calculator only needs to perform basic functions of addition, subtraction, multiplication and division of 2 integers only.

Recommended Answers

All 2 Replies

Start by reading up on how to parse command line arguments. Then see what you can come up with based on what you learn, and if you have any specific problems or questions, don't hesitate to ask.

It's very difficult to tell you how to do this without making assumptions about your level and without writing a complete tutorial on the subject (far more than is justified for a forum post).

Here's a quick example of how you can integrate command line arguments into your program:

#include <iostream>
#include <cstdlib>
int main(int argc, char* argv[]){
    if (argc>1){
        int a=std::atoi(argv[1]), b=std::atoi(argv[2]);
        std::cout<<"a: "<<a<<", b: "<<b<<" "<<std::endl; //could be std::printf("a: %d b: %d", a, b); if it's 
                                                         //required as a C program.
    }
    return 0;
}

You'll run this from the command line like this:

//Windows: 
executable.exe 1 2
//which will output:
a: 1, b: 2

//Linux
./executable 1 2
//which will output:
a: 1, b: 2

Now, knowing that, try to figure out how to do that simple calculations. Good luck.

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.