-1

I'm unable to make a program that asks the user to input three arguments on the command line: 1) an operator (+, -, *, /); 2) an integer (n); 3) another integer (m). The program should thus act as a basic calculator producing the output in this format: .

e.g.
operator='+'
n=5
m=6
output: 5+6
        = 11
sbhayana26
  • 61
  • 1
  • 7
  • Do you mean command line as in `program.exe + 2 6` in console or should the program ask the user for input when it starts? – user4520 May 29 '15 at 17:21
  • @szczurcio: In console. – sbhayana26 May 29 '15 at 17:22
  • possible duplicate of [Arguments to main in C](http://stackoverflow.com/questions/4176326/arguments-to-main-in-c) – JAL May 29 '15 at 17:23
  • 1
    Your question is confusing people. When you say `asks the user to input`, to me it means you want to run the code from the command line not an IDE. Basically it is not passing arguments via `main`, therefore `command-line-arguments` tag need to be removed or you need to rewrite the question to reflect the main purpose. – CroCo May 29 '15 at 17:46

2 Answers2

3

If you want to take the argument from the command line while the user executes the program you can use the argv vector to fetch the values

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

}

So that if you execute your program as follows,

./prog + 1 2

argv will contain the follwing,

argv[0] = 'prog',
argv[1] = '+',
argv[2] = '1',
argv[3] = '2',

So that you can fetch each value from argv and implement your logic.

Read this tutorial for better understanding.

Sajith Eshan
  • 696
  • 4
  • 17
2

If you are interested in giving arguments while executing the file, i.e. ./executable_name arg1 arg2 arg3 then use

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

to get argument from command line. argc gives the count of argument(s) passed including the executable name and argv is the argument array preserving the order of the input.

You can then compile and run code in command prompt:

cl filename.c

executablename arg1 arg2 arg3

Rakholiya Jenish
  • 3,165
  • 18
  • 28