A question that'll take 2 seconds to answer

This is a discussion on A question that'll take 2 seconds to answer within the C++ Programming forums, part of the General Programming Boards category; Yeah so I got C++ 5 for dummies and in int main() they put things in the parameters. Code: int ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    100

    Question A question that'll take 2 seconds to answer

    Yeah so I got C++ 5 for dummies and in int main() they put things in the parameters.

    Code:
    int main (nNumberofArgs, char* pszArgs[]) {
    //blah blah code here
    return 0; 
    }
    Okay an idiot can read the first one - it means number of arguments. But the second however I find to be in swahili. So, I don't know much about pointers, and this is in the beginning of the whole friggin book. Without explaining a thing!

    Also at the top, they always include

    <cstdlib>
    and
    <cstdio>

    What are these includes including?

  2. #2
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89
    the second parameter is an array of character strings which containsthe actual arguments the user passes on start up of the program your making. You have to know what the user passed as arguments right?..you can't do much with just the numer. char* is what makes it string and the [] is what makes it an array. I'm not surprised that the book didtnt explain this right away its not comonly th first thing you learn...they're just trying to get you in the habit of puting those arguments in I guess.

    as for the headers the the cstlib is including a standdard library of useful functions to use (not sure what in your case tho) and the stdio is standard io useful functions to use..like printf and cout/cin

    hope that helps,
    Boomba
    Last edited by Boomba; 09-03-2005 at 03:13 PM.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    Should I put those arguments in?

    I mean i've been programming for a while and int main(//nothing here) has been doing fine for me.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    No reason to put the parameters there unless you want your program to accept command-line arguments. (If you didn't know, that's what you type after the program name if you run it from a command prompt, ie "myprog.exe arg1 arg2")
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89
    if you dont wanna support parameters being passed to your program then dont bother...here i'll give you an example of a a use for this:

    Code:
    int main (nNumberofArgs, char* pszArgs[])
    {
        if(pszArgs[0] == 'A') //if first argument equals letter A
        {
            printf("you typed letter A \n");
        }
        else if(pszArgs[0] == 'B') //if first argumentequals letter B
        {
            printf("you typed letter B \n");
        }
        else
        {
             printf("you typed A letter other than A or B as your first argument. \n ");
        }
    
        return 0; 
    }
    so now when you execute your program from a cmd like you can execute it like this:

    >program.exe A
    Result: you typed letter A

    >program.exe B
    Result: you typed letter B


    >program.exe Cv
    Result: you typed A letter other than A or B as your first argument.

    pszArgs[1] would be your second argument, pszArgs[2] would be your 3rd argument..and so on

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    With a few modifications that program would do as explained
    Code:
    #include <stdio.h>
    
    int main (int nNumberofArgs, char* pszArgs[]) {
       if  ( nNumberofArgs > 1 ) {
           if(pszArgs[1][0] == 'A') { //if first argument equals letter A
               printf("you typed letter A \n");
           }
           else if(pszArgs[1][0] == 'B') { //if first argumentequals letter B
               printf("you typed letter B \n");
           }
           else {
                printf("you typed A letter other than A or B as your first argument. \n ");
           }
        }
        return 0; 
    }
    Kurt

  7. #7
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89
    With a few modifications that program would do as explained
    woops... thanx for catching that mistake Zuk.

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    So basically it's kinda pointless?

  9. #9
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89
    yeah well if those argument variables arent being used anywhere then removing them for now wont lead to any bad outcomes.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by blankstare77
    Yeah so I got C++ 5 for dummies
    That was your first mistake. Throw it in the trash (or take it back where you bought it) and get a decent book.

  11. #11
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    Quote Originally Posted by Ancient Dragon
    That was your first mistake. Throw it in the trash (or take it back where you bought it) and get a decent book.
    That is a decent book. What are you talking about.

  12. #12
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    That is a decent book. What are you talking about.
    The "... for Dummies" books tend to be overly simplistic. Keep using that book by all means, but if you intend to get serious about programming, I suggest you diversify your reading material.

  13. #13
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by blankstare77
    That is a decent book. What are you talking about.

    That series of books are named "for dummies" for good reason -- only a true dummy would bother waste their hard-earned monty to buy and read them. They are a waste of time and money. Here is a list of books that are worth reading and studying.

  14. #14
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    Don't let the name fool you. Seriously though, those books are great for beginners and I'm learning a lot from it. Then I'll move on to an advanced book. I am by no means stating that this book has it all, because if it did it would be like 1000 pages.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. some question asking for answer...
    By mat13mat in forum C++ Programming
    Replies: 6
    Last Post: 04-30-2008, 04:40 AM
  2. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 03:52 PM
  3. C++ Question with uncomplete answer..anyone can solve it?
    By jason07 in forum C++ Programming
    Replies: 9
    Last Post: 09-13-2005, 05:56 PM
  4. Please Answer My Question:
    By DeanDemon in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2002, 12:50 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM

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