Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Command line arguments to main
03-12-2010, 07:46 PM
Post: #1
Command line arguments to main
ok I have been trying to get this command line arguments thing going with somthing simple. So i decided to try a very simple calculator. However even though it compiles correctly, it seems to not read my arguments properly. Any help here?

Code:
#include <iostream>
#include <cstdlib>


using namespace std;

int main(int argc, char *argv[])
{
    double a;
    double b;


    if(argc != 4)
    {
        cout<<"Invalid argument. Use calculate number operator number.";
    }
    else
    {
        a = atof(argv[1]);
        b = atof(argv[3]);



        if (argv[2] == "+")
        {
            cout<< a + b ;
        }
        else if (argv[2] == "-")
        {
            cout<< a - b;
        }
        else if (argv[2] == "*")
        {
            cout<< a * b;
        }
        else if (argv[2] == "/")
        {
            cout<< a / b;
        }
        else
        {
            cout<< "invalid operator";
        }
    }



    return 0;
}

This is really annoying me.
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:

Contact UsDeveloper SolutionReturn to TopReturn to ContentLite (Archive) ModeRSS Syndication