headers and int main()

This is a discussion on headers and int main() within the C++ Programming forums, part of the General Programming Boards category; Hey guys, this is my first post. I used to use these forums a long time ago when I fiddled ...

  1. #1
    Registered User lpaulgib's Avatar
    Join Date
    May 2010
    Posts
    65

    headers and int main()

    Hey guys, this is my first post. I used to use these forums a long time ago when I fiddled around with C in highschool. Anyways, after a 4 year stint in the Marine Corps, I'm going to college for a degree in engineering and physics at Georgia Tech. I'm starting in August. I've been advised by some contacts that knowledge of a programming language is good to have on a resume if I get into research and development in biomedical engineering, which is my goal. So... I'm working on C++ right now, and I'm getting into the "for" and "while" loops. I'm using a copy of C++ for Dummies that I managed to scrounge up for free, and I'm running the Code::Blocks compiler on Windows 7.


    Here is the block of code that I'm working with to make this easy for you to see what I'm referencing. All of the comments are ones that I did on my own to kind of help me talk myself through the code.



    Code:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    
    int main(int nNumberofArgs, char* pszArgs[])
    {
        //input the loop count
        int loopcount; // initating the argument named loopcount interger type
        std::cout << "Enter loopCount: ";
        std::cin >> loopcount;  //takes input from user and puts into loopcount
    
        for (int i =1; i <= loopcount; i++)
        // it's saying the argument interger type "i" is equal to 1. When it's
        // compared to the argument loopcount, run the loop as long as it's less
        // than or equal to loopcount. the i++ means to add 1 at the start of the
        // loop to the argument "i"
        {
            std::cout << "We've finished " << i << " loops\n";
        }
        /* the COUT statement is saying print the text, then print interger "i"
        then print loops followed by a newline. then repeat the for loop.
        */
        system("PAUSE");
        return 0;
    }
    So my first question is on the #include header part. I think I grasp the concept of what a header is. If I got it right, it's basically a list of definitions for commands I use? So the book isn't explaining to me when to use which one.

    1. When I searched online for cstdlib, I got a list of functions and the explanation that it's for memory allocation and lists and sorts. Do I even need this in the program I'm using? The list of functions didn't show anything I use in that block of code.

    2. cstdio looks like something for input/output, but online referencing says it's formatted IO functions. Can someone elaborate for an idiot? Once again... I don't see how I need it in this program.

    3. ...int main(). I don't understand the (int nNumberofArgs, char* pszArgs[]) part. I searched the string on google, and got limited results. I was thinking maybe something to do with declaring an array or matrix with the [] brackets. Could someone explain this to me in a basic way. No need to go too deep. And once again..... Is this even necessary in this program?

    4. And finally.... I took out the books original content to include using namespace std. I read somewhere online that this is inefficient, and so instead I did the other method above in the code. Am I looking at this in the right way, or is there a potential problem with my styling of the code?

    The book isn't explaining these things it includes at all. I'm about to go ahead and get a different book by a new author. Until then, could I get some help? By the way remember, I'm a 1 week C++ veteran so don't give me a stroke with technical stuff yet!


    Thanks guys!

    Semper Fi.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by lpaulgib View Post
    1. When I searched online for cstdlib, I got a list of functions and the explanation that it's for memory allocation and lists and sorts. Do I even need this in the program I'm using? The list of functions didn't show anything I use in that block of code.
    The system() function is in there. system() is a function to be avoided, usually.

    2. cstdio looks like something for input/output, but online referencing says it's formatted IO functions. Can someone elaborate for an idiot? Once again... I don't see how I need it in this program.
    cstdio is completely unnecessary for this program.

    3. ...int main(). I don't understand the (int nNumberofArgs, char* pszArgs[]) part. I searched the string on google, and got limited results. I was thinking maybe something to do with declaring an array or matrix with the [] brackets. Could someone explain this to me in a basic way. No need to go too deep. And once again..... Is this even necessary in this program?
    No, it's not necessary in this program. And the reason your Google search didn't find anything is because those arguments are almost always called "argc" and "argv", not those silly names given here. Google "argc argv" and you'll get a ton of stuff.

    4. And finally.... I took out the books original content to include using namespace std. I read somewhere online that this is inefficient, and so instead I did the other method above in the code. Am I looking at this in the right way, or is there a potential problem with my styling of the code?
    It isn't inefficient. It does have some very important ramifications whether you use a "using" declaration or specify namespaces explicitly. Until you know enough to understand those ramifications, I suggest doing it the way you're doing it now and stay away from "using" declarations.

    Given just what you've posted here, that book looks like a piece of garbage. Unfortunately I don't know what books are good alternatives.

    Good critical thinking. Impressed.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lpaulgib View Post
    So my first question is on the #include header part. I think I grasp the concept of what a header is. If I got it right, it's basically a list of definitions for commands I use? So the book isn't explaining to me when to use which one.
    Altho the easiest thing to do may seem to be to ask about stuff like this on the forum, you are more likely to just waste more time that way (plus: that of others) than if you simply used the internet:

    All about "C++ #include"

    If you don't live in a city with a library, you may want to order another book, altho the Dummies books can be good so I am surprised they don't explain this.

    3. ...int main(). I don't understand the (int nNumberofArgs, char* pszArgs[]) part. I searched the string on google, and got limited results. I was thinking maybe something to do with declaring an array or matrix with the [] brackets. Could someone explain this to me in a basic way. No need to go too deep. And once again..... Is this even necessary in this program?
    In this program it is not necessary since you don't use it, however, let me be honest: there is absolutely ZERO chance that this is not discussed in a book with C++ on the cover. And I've seen that book around, it is at least 300-500 pages. You are mistaken, you just have not read enough yet. Look for sections on "program arguments". Basically:

    int nNumberofArgs, an int, (most often called int argc, for "argument count") is the number of arguments provided to the program from parameters passed on the command line. This is always at least 1, since the first parameter is the name of the program.

    char* pszArgs[], an array of char pointers (most often called char *argv[] -- evidently that book uses some kind of zany "Hungarian" notation), contains the value for each parameter. Here's a demo:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, const char *argv[]) {
    	int i = argc-1;
    
    	if (argc > 1)
    		while (i >= 0) cout << argv[i--] << endl;
    	else cout << "No args\n";
    
    	return 0;
    }
    Run this from the command line like this:

    whateverprogramname this that 123
    Last edited by MK27; 05-14-2010 at 06:42 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User lpaulgib's Avatar
    Join Date
    May 2010
    Posts
    65
    Believe it or not, MK, if you read my post I did do research. Hence the phrases like "online referencing" and "when I searched online". I know it's surprising, but I, a beginner AND a first poster, not only read the forum rules, but did some reading on the internet as my first source of information.

    As I said, I understand the basics of the #include directive. It was more of a verification of my understanding of previously read information. Sometimes all the terminology gets a little too vague for me to understand. And without a human teacher, I'm reliant on wonderful forum members like yourself. Sorry if you don't think I did enough research. This book, if it contains that information, doesn't make any reference at all to those lines of code. Not one sentence in all the chapters leading up that code block mentioned anything I asked. This book is pretty retarded, and I haven't been to happy with it.

    Anyhow, thanks for the help guys...

  5. #5
    Registered User jdragyn's Avatar
    Join Date
    Sep 2009
    Posts
    92
    On one hand, you got what you paid for...

    On the other hand, look ahead a little bit in the book. I have a C++ For Dummies book, though it has been collecting dust on a shelf for several years now because I found it rather frustrating. To be fair, this edition claimed to teach Windows programming which is why I bought it, but a little more research would have saved me that money and some frustration by buying the book I ended up getting instead.

    Anyway, one of the things that I didn't care for was that the author would "slip new things" into his code examples without explaining them beforehand. Instead he would explain them later with an attitude of "I'm so witty for doing that".

    A question about the code you posted: is the word "interger" the way the book spelled it? If not, then please note it should be "integer". If the book uses that spelling throughout however, then... wow.
    C+/- programmer extraordinaire

  6. #6
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Just addressing the use of command line args (int argc, char argv) the use of it depends on you and what you expect your target platform to be (Windows or everything else). The primary reason for implementing support for command line args is that in the UNIX way of thinking, all programs should be able to read the output of other programs as input and it should always output things in a clean manner so that programs can be chained together to solve larger problems. The dao behind this line of thinking is to create many small programs that do one thing well and use scripting to assemble these small programs into something that can solve larger problems/jobs. The UNIX programming mindset is explained a lot better in this free book, The Art of Unix Programming by Eric S. Raymond or ESR to our world.

    Windows on the other hand not only doesn't subscribe to this model but discourages running things from the command line. As such, while it might seem more correct to do this everywhere, the fact is unless most of the apps running on your platform works in this fashion (and on Windows they do not), there is little value in you doing it. However if you think you might be coding on those other platforms (or making your code cross platform) then it is a good idea. Also if you are considering this, look into getopt. This comes with Linux and Mac but like everything else, it is missing on Windows though a Google search will show a Win32 work-alike. Getopt makes dealing with the command line way less work and the resulting code is cleaner too IMHO:

    Code:
    #include <getopt.h>
    
    // supports -h (help), -i <input file>
    #define CMDOPTS "hi:"
    
       string inputFile;
       opterr = 0;
       int c;
       while((c = getopt(argc, argv, CMDOPTS)) != EOF)
       {
          switch(c)
          {
    	 case 'h':
    	 {
    	    Usage();
    	    break;
    	 }
    	 case 'i':
    	 {
    	    inputFile = optarg;
    	    break;
    	 }
          }
       }
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  7. #7
    Registered User lpaulgib's Avatar
    Join Date
    May 2010
    Posts
    65
    Alright, so I've been looking into int main(int argc, char *argv[]). I just want to verify if what I've been reading is correct. So argc is the argument (AKA variable?) count for a command line entry on that program?


    And argv[] is the assignment of each argument such as argv[0] is the first argument on the command line, argv[1] is the second argument in the line.



    Is this saying that if I were to go to the Command Line, and enter in say... telnet XXX.XXX.XXX -o then......

    argv[0] is telnet
    argv[1] is XXX.XXX.XXX
    argv[2] is -o

    And in the telnet program each of these is assigned a set of instructions in the code? Am I looking at this right?

    So that would mean, that although it's good practice to include it, it's not usable for this piece of code that I wrote correct? Thanks for clearing this up for me guys. I've been reading this stuff, but I need some tailored help to my specific questions!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function call from another .c module
    By Ali.B in forum C Programming
    Replies: 14
    Last Post: 08-03-2009, 11:45 AM
  2. argv change
    By dracayr in forum C Programming
    Replies: 9
    Last Post: 04-10-2009, 02:49 AM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Render text
    By Livijn in forum C++ Programming
    Replies: 6
    Last Post: 07-06-2007, 03:32 PM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM

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