Thread: Main() arguments

  1. #1

    Unhappy Main() arguments

    I am trying to learn more about main() taking arguments. I have tried creating a type of calculator program where you would use this syntax from the command line to calculate.
    progname 1 + 1
    I am having a problem with the operator part. I know that they will not show up as char type, so would I need to use their ASCII decimal form with my switch statement? Or how can I take different argument types such as, int, char, int?

    Here is my lame code:
    #include <iostream>
    using namespace std;
    int main(int *argv)
    	case '+': cout << argv[1]+argv[2]; break;
    	case '-': cout << argv[1]-argv[2]; break;
    	case '*': cout << argv[1]*argv[2]; break;
    	case '/': cout << argv[1]/argv[2]; break;
    	default: cout << "\nIncorrect usage.\n";
    	return 0;

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001

    Re: Main() arguments

    You have multiple problems here.
    For one, you're not actually taking correct parameters for main. It should be:

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

    Additionally, you cannot simply use the addition sign to add strings. Keep in mind that each argument to main is not simply a number, it's a string.

    You either need to overload all of these operators, or even easier, and better, convert the strings to numbers and then do the work.

    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Maybe something like this :

    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <conio.h>
    int main( int argc, char *argv[] )
    	int number1 = atoi(argv[1]);
    	int number2 = atoi(argv[3]);
    	char type = *argv[2];
    	case '+': cout << number1+number2; break;
    	case '-': cout << number1-number2; break;
    	case '*': cout << number1*number2; break;
    	case '/': cout << number1/number2; break;
    	default: cout << "\nIncorrect usage.\n";
    	return 0;
    Probably should add some error checking aswell.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Some compiler support differenŽt syntax for main. Check your compiler documentation - maybe youŽll find one which suits you better...

  5. #5
    Thank you all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Too many arguments to function int main?
    By plain in forum C++ Programming
    Replies: 13
    Last Post: 08-29-2006, 06:01 PM
  2. Actient main
    By swgh in forum C Programming
    Replies: 2
    Last Post: 03-21-2006, 01:33 PM
  3. C99 and int main()
    By cwr in forum C Programming
    Replies: 8
    Last Post: 09-19-2005, 06:54 AM
  4. Passing arguments to main?
    By xddxogm3 in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2003, 03:59 AM
  5. arguments passed to main?!?
    By threahdead in forum C++ Programming
    Replies: 14
    Last Post: 01-22-2003, 08:43 PM