Thread: Int Arguments

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    8

    Int Arguments

    This is so great! This is a great board, a lot of helpful people here and I look forward to learning a lot here. I just finished "Teach Yourself C++ in 21 Days", so I'm not a complete newb, but I definately need to learn a lot more. So everyone prepare for my MANY MANY questions!

    Heres a question to get things rolling

    Code:
    //  Triangle computation program
    
    #include <iostream>
    using namespace std;
    
    int main(int argc, int *argv[])
    {
        int a, b, c;
      //cout << argch[0];
      cout << "\n";
      cout << argv[1];
      cout << argv[2];
      cout << argv[3];
    
    
    
      if (argc > 4 || (argc < 4 && argc > 1))
       {
         cout << "Invalid number of sides.\n";
         cout << "Implementation: <filename> sideA sideB sideC\n";
       }
    
      if (argc == 4)
       {
        a = argv[1];
        b = argv[2];
        c = argv[3];
         cout << "Calculating based on given arguments.\n";
       }
    
    
      if(argc == 1)
       {
        cout << "Enter side 1: ";
        cin >> a;
        cout << "Enter side 2: ";
        cin >> b;
        cout << "Enter side 3: ";
        cin >> c;
        cout << "Calculating...\n";
        }
    
        if ( a == b && a == c && b == c )
           cout << "This is an equilateral Triangle.\n\b";
    
        if ( (a == b && a !=c) ||
             (a == c && a !=b) ||
             (b == c && b !=a) )
             cout << "This is an isocles Triangle.\n\b";
        if ( ((a*a)+(b*b)== (c*c)) ||
             ((b*b)+(c*c)== (a*a)) ||
             ((c*c)+(a*a)== (b*b)) )
             cout << "This is a right triangle.\n\b";
        else
            cout << "Please check the numbers and try again!\n";
    
        return 0;
    }
    The only thing I couldn't get to work is for this program to use integers it receives from the command line. How can I get this to work? Thanks.
    -Peace

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    Re: Int Arguments

    Originally posted by Scytz0
    Code:
    int main(int argc, int *argv[])
    this should be:
    Code:
    int main( int argc, char *argv[] )
    {
    //and now you need to parse argv for the arguments...remember
    //that arguments are usualy seperated by a flag ( - )
    //...so if running a prog with arguments you would have somthin
    //like this:
    //user~>main -nrows -ncols
    ....
    axon
    Last edited by axon; 09-25-2003 at 07:18 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    8
    Thanks, I'll try that.

    // ** EDIT **

    Does this look right?

    Code:
    //  Triangle computation program
    
    #include <iostream>
    using namespace std;
    
    int main(int argc, char argv[])
    {
        int a, b, c;
      //cout << argch[0];
      cout << "\n";
      cout << argv[1];
      cout << argv[2];
      cout << argv[3];
    
    
    
      if (argc > 4 || (argc < 4 && argc > 1))
       {
         cout << "Invalid number of sides.\n";
         cout << "Implementation: <filename> sideA sideB sideC\n";
       }
    
      if (argc == 4)
       {
        a = argv[1];
        b = argv[2];
        c = argv[3];
         cout << "Calculating based on given arguments.\n";
         cout << a << "\n" << b << "\n" << c << endl;
       }
    
    
      if(argc == 1)
       {
        cout << "Enter side 1: ";
        cin >> a;
        cout << "Enter side 2: ";
        cin >> b;
        cout << "Enter side 3: ";
        cin >> c;
        cout << "Calculating...\n";
        }
    
        if ( a == b && a == c && b == c )
           cout << "This is an equilateral Triangle.\n\b";
    
        if ( (a == b && a !=c) ||
             (a == c && a !=b) ||
             (b == c && b !=a) )
             cout << "This is an isocles Triangle.\n\b";
        if ( ((a*a)+(b*b)== (c*c)) ||
             ((b*b)+(c*c)== (a*a)) ||
             ((c*c)+(a*a)== (b*b)) )
             cout << "This is a right triangle.\n\b";
        else
            cout << "Please check the numbers and try again!\n";
    
        return 0;
    }
    This is what I type at the command line

    C:\Documents and Settings\admin\Desktop>tri2 -3 -4 -5

    and I get this:

    8" Calculating based on given arguments.
    56
    34
    0
    Please check the numbers and try again!


    What am I doing wrong?
    Last edited by Scytz0; 09-25-2003 at 07:22 PM.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    axon's advice was correct, but you didn't quite follow it exactly. He said
    int main( int argc, char *argv[] )
    but you used
    int main( int argc, char argv[] )
    Once you fix that your code won't work still because you are setting a char array to an int. Since argv is an array of char* objects, that means it is 0 or more char* strings. So argv[0] is a null terminated string, as is argv[1], argv[2] and argv[3] (assuming your example arguments).

    I'd suggest using the standard string class to hold those strings, but whichever way you do it you'll have to read up a bit on it to properly save the values as ints.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    8
    Could you nudge me in the right direction or give me a hint as to how I would use a string to accomplish this? Thanks.
    -Peace

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    jlou is right..

    >>C:\Documents and Settings\admin\Desktop>tri2 -3 -4 -5

    if these are your arguments, trace back what happens to them.
    argc is set to 4. So the first element of argv is tri2. Anyways, below I'm attaching a prog that parses a command line which I wrote not so long ago...here is what it supposed to do...it does have one bug when your command starts out with an alpha char.
    Code:
    //argument:    main 234aa34  34b  23  3b4
    //display:
    234
    34
    34
    23
    3
    4
    as you can see the prog seperates the integesr from the characters. each time an char or white space is encoutered go to new line.
    Code:
    #include <iostream>
    #include <cctype>
    #include <cstring>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	int a=0; //counter for amount of letters
    	char c;
    
    	if ( argc == 1 ) {	//check for amount of arguments
    		cout << "\nThere is nothing to parse!\n";
    		cout << endl;
    	} else {
    		for ( int i = 1; i < argc; i++ ) {
    			for ( int j = 0; j < strlen(argv[i]); j++ ){
    				c = argv[i][j];	//step through each string and
    				if ( isdigit(c) ) { //check if value is a digit
    					if ( a > 0 ) {
    						cout << endl;
    						a=0;
    					}
    					cout << argv[i][j];// << endl;
    				}else { a++; } //if value is not digit increment a
    			}
    			cout << endl;
    		}
    	}
    	return 0;
    }
    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  7. #7
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Originally posted by Scytz0
    Could you nudge me in the right direction or give me a hint as to how I would use a string to accomplish this? Thanks.
    -Peace
    one word atoi()

    do a little search for it. It should also be in your book.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM