Results 1 to 10 of 10

Thread: Console (command line)

  1. #1

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335

    Console (command line)

    ok i know some c++ cuz of you people out her thanks now i want to know about the command line arguments . Do they have to do with
    VB Code:
    1. int main (int nArgs, char* pszArgs[])

    if i do this
    VB Code:
    1. #include <stdio.h>
    2. #include <iostream.h>
    3.  
    4.  
    5. int main (int nVal, int nVal2, char* pszArgs[])
    6. {
    7.  
    8. int nVal3 = nVal + nVal2;
    9. cout << "The total is: " << nVal3;
    10.  
    11.  
    12. return 0;
    13. }
    Will it work? Like how would i enter the command line arg?
    like
    Add.exe 3, 4 is incorrect but whats the right way
    Last edited by JasonLpz; Jan 25th, 2003 at 03:25 PM.
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  2. #2
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    No, the command line arguments don't work like that. The arguments are passed as an array of char*s, the first argument of main is the number of arguments in the array, the second is the array itself:
    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int main(int argc, char** argv)
    {
        if (argc!=3) {
            cerr << "You must pass two arguments" << endl;
        }
        int val1 = atoi(argv[1]);
        int val2 = atoi(argv[1]);
        cout << val1 << " + " << val2 << " = " << (val1+val2) << endl;
        return 0;
    }
    Note that the first argument of argv is always the name of the program as it is called (therefore argc will be one more then the actual number of arguments)

  3. #3

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    how do i enter the 2 args now?
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  4. #4

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    Oops theres your problem thanks its great now
    VB Code:
    1. int val1 = atoi(argv[1]);
    2.     int val2 = atoi(argv[2]);
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  5. #5

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    ok great so far so good. Now i have 1 problem i made this program with the info u gave me. Now i get this error not when i compile but when i run this without any arguments. how can i catch this error and stop it like if they dont type any arguments then show the

    VB Code:
    1. if (sVal == 0) {
    2.         cout << "You must select a program to run. Press number to run program.\n";
    3.         cout << "\n1 = Adding Application";
    4.         cout << "\n2 = Subtract Application";
    5.         cout << "\n3 = Multiply Application";
    6.         cout << "\n4 = Divide Application\n\n\n";
    7.  }
    VB Code:
    1. #include <iostream>
    2. #include <cstdlib>
    3. using namespace std;
    4.  
    5. int main(int argc, char** argv)
    6. {
    7.    
    8.     int sVal = atoi(argv[1]);
    9.    
    10.     if (argc!=3) {
    11.         cerr << "For A list or programs press 0.\n\n"<< endl;
    12.     }
    13.        
    14.  
    15.  
    16.     if (sVal == 0) {
    17.       cout << "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n";
    18.           cout << "||You must select a program to run. Press number to run program.||\n";
    19.       cout << "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||";
    20.         cout << "\n||1 = Adding Application                                        ||";
    21.     cout << "\n||2 = Subtract Application                                      ||";
    22.     cout << "\n||3 = Multiply Application                                      ||";
    23.     cout << "\n||4 = Divide Application                                        ||\n";
    24.       cout << "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n\n\n"
    25.  }
    26.  
    27. if (sVal == 1)
    28.     {
    29. int a1;
    30. int a2;
    31. int a3;
    32.  
    33. cout << "Enter numbers below to add\n";
    34.    
    35. cout << "1:";
    36. cin >> a1;
    37.        
    38. cout << "2: ";
    39. cin >> a2;
    40.  
    41. a3 = a1 + a2;
    42.  
    43. cout << "\n\n The Sum is: " << a1 << " + " << a2 << " = " << a3;
    44.  
    45.     }
    46.  
    47. if (sVal == 2)
    48.     {
    49. int a1;
    50. int a2;
    51. int a3;
    52.  
    53. cout << "Enter numbers below to subtract\n";
    54.  
    55. cout << "1:";
    56. cin >> a1;
    57.        
    58. cout << "2: ";
    59. cin >> a2;
    60.  
    61. a3 = a1 - a2;
    62.  
    63. cout << "\n\n The Subtracted sum is: " << a1 << " - " << a2 << " = " << a3;
    64.  
    65.     }
    66.  
    67. if (sVal == 3)
    68.     {
    69. int a1;
    70. int a2;
    71. int a3;
    72.  
    73. cout << "Enter numbers below to multiply\n";
    74.        
    75. cout << "1:";
    76. cin >> a1;
    77.        
    78. cout << "2: ";
    79. cin >> a2;
    80.  
    81. a3 = a1 * a2;
    82.  
    83. cout << "\n\n The Multiplied sum is: " << a1 << " x " << a2 << " = " << a3;
    84.  
    85.     }
    86. if (sVal == 4)
    87.     {
    88. int a1;
    89. int a2;
    90. int a3;
    91.  
    92. cout << "Enter numbers below to divide\n";
    93.        
    94. cout << "1:";
    95. cin >> a1;
    96.        
    97. cout << "2: ";
    98. cin >> a2;
    99.  
    100. a3 = a1 / a2;
    101.  
    102. cout << "\n\n The Divided sum is: " << a1 << " / " << a2 << " = " << a3;
    103.  
    104.     }
    105.     return 0;
    106. }
    Last edited by JasonLpz; Jan 25th, 2003 at 06:34 PM.
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  6. #6

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    oh yeah in VB there is a easy to use SELECT CASE is there something like this in c++ (there must be ) Can someone tell me something about that plz thank you all
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    There's a switch in C++ that does the same as select in VB. It can't handle strings though.

    As for this:
    int sVal = atoi(argv[1]);
    Don't access any command line arguments without having checked that there really is something! If the user starts your app without arguments then you'll probably end up with an access violation.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    ok thanks got it
    VB Code:
    1. int main(int argc, char** argv)
    2. {
    3. cout << "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n";
    4. cout << "||You must select a program to run. Press number to run program.||\n";
    5. cout << "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||";
    6. cout << "\n||0 = This list                                                 ||";
    7. cout << "\n||1 = Adding Application                                        ||";
    8. cout << "\n||2 = Subtract Application                                      ||";
    9. cout << "\n||3 = Multiply Application                                      ||";
    10. cout << "\n||4 = Divide Application                                        ||\n";
    11. cout <<  "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n\n\n";
    12.    
    13. int sVal;
    14. if (argc!=3) {
    15. cerr << ""<< endl;
    16. }
    17.  
    18.    
    19. if (argc!=1){
    20. sVal = atoi(argv[1]);
    21. }
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  9. #9

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    another question. I noticed my programs wil close if i just double click them is there a way to change this?
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I guess they open, notice that there are insufficient arguments and thus end. The console window is not kep open.

    To make up for that you can either start the app from a console window or add a line of code that keeps the window open (like cin.ignore(1); )
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width