#1
  1. No Profile Picture
    =) wannabe?
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 2002
    Location
    florida
    Posts
    2,153
    Rep Power
    14

    int? and another q


    =) hi

    i'm just starting and i ran intoa problem

    i have this code

    Code:
    
    int main() {
    
    cout << "hi there";
    
    }

    why is the main() int? i don't see any numbers there.

    also does that mean that hi there is the output of the function main()?


    another problem is. when i run the .exe file it opens and closes right away. i can see the hi there. but it closes almost immediately. how do i fix that?

    thank you
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,473
    Rep Power
    15
    1)why is the main() int? i don't see any numbers there.

    Here is the format for main that makes that clear:

    int main()
    {


    return 0;
    }

    0 is an int. You can also return other values to indicate errors or other things.

    2)also does that mean that hi there is the output of the function main()?

    Any thing that is printed to the screen is output, and if it's in a function, it's fair to say the output is the output of the function.

    3) another problem is. when i run the .exe file it opens and closes right away. i can see the hi there. but it closes almost immediately. how do i fix that?

    There are a couple of ways. Probably the best one is to include this as the last line of main(), right before the return statement:

    cin.get(); //this waits for you to input any char
    return 0;
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Originally posted by wannabe
    =) hi

    i'm just starting and i ran intoa problem

    i have this code

    Code:
    
    int main() {
    
    cout << "hi there";
    
    }

    why is the main() int? i don't see any numbers there.
    DOS v2 borrowed a lot of concepts from UNIX. In UNIX/Linux, a program is expected to return an exit code which tells the shell whether the program had run successfully (== 0) or had encountered certain types of errors (non-zero). In UNIX shell scripting, those exit codes get used fairly extensively; the exit code for a program is contained in the special variable, $?.

    Therefore, in UNIX, main() always returns an exit code, which is why it returns an int. BTW, your main() needs to end with the line:
    return 0;

    In DOS, the need to return an exit code is felt much less and they are used much less. Accordingly, in DOS you will often find main()'s return type to be declared as void and that it does not return any value.

    Originally posted by wannabe
    also does that mean that hi there is the output of the function main()?
    "hi there" is the output only in the sense that main() outputs it to the standard output file, stdout, which is the display. As described above, the int that main() returns is the exit code for which you need a return statement.

    Originally posted by wannabe
    another problem is. when i run the .exe file it opens and closes right away. i can see the hi there. but it closes almost immediately. how do i fix that?
    Very simple, run it in DOS as God had intended.

    OK, this has been discussed here several times. Just before the program terminates, put in an extra command. You could call sleep (I forget the case for it) to delay termination for a few seconds. Most prefer an input function like cin >> x; , such that you will need to enter something to allow the program to terminate.

    [EDIT: You should go with 7stud's cin.get();, since he's undoubtedly speaking from direct experience. I always just run it in DOS, the first window I open whenever I log on, so I never need to employ such tricks.]

    Of course, I very much prefer simply running it in DOS as God had intended.
    Last edited by dwise1_aol; June 4th, 2003 at 09:54 AM.
  6. #4
  7. No Profile Picture
    =) wannabe?
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 2002
    Location
    florida
    Posts
    2,153
    Rep Power
    14
    thanks for explaining =)

IMN logo majestic logo threadwatch logo seochat tools logo