Main() arguments

This is a discussion on Main() arguments within the C++ Programming forums, part of the General Programming Boards category; I am trying to learn more about main() taking arguments. I have tried creating a type of calculator program where ...

  1. #1
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280

    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:
    Code:
    #include <iostream>
    using namespace std;
    
    int main(int *argv)
    {
    	switch(argv[2])
    	{
    	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
    Posts
    14,826

    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.

    Quzah.
    Hope is the first step on the road to disappointment.

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

    Code:
    #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];
    
    	switch(type)
    	{
    	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
    Posts
    29
    Some compiler support differenŽt syntax for main. Check your compiler documentation - maybe youŽll find one which suits you better...

  5. #5
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    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, 07: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, 07:54 AM
  4. Passing arguments to main?
    By xddxogm3 in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2003, 04:59 AM
  5. arguments passed to main?!?
    By threahdead in forum C++ Programming
    Replies: 14
    Last Post: 01-22-2003, 08:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21