Archived

This topic is now archived and is closed to further replies.

Help with C++

This topic is 5805 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m new to C++, and I need some help. First, what''s the difference between int main, void mani, and int main(void)? Next, what the hell does return 0 or whatever mean? Lastly, how does passing stuff, and calling other procedures work. I do understand much of the syntax, however. BTW, I''m only 15, I''m not a complete idiot.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
When you make a function it''s put in the following format:


Return type, Function Name, Input Types and variable names.

So lets get into what a type is: int, char, short, and long are just a few type names. If your function doesn''t need to return a value then you use void. The void type pretty much means "nothing". So lets look at the functions you listed

int main(void) This means that the function called main returns an integer, but takes in no values.

void main(void) This means that the function called main returns nothing and takes in no values.

Return is a keyword. It ends the function and returns what ever value you give it. In the case you listed below "return 0;" it returns 0 from a function. However you can not use return if your return value on the function is void.

How do you pass stuff?

  

// Function prototypes NOTE: All functions need a prototype but main.


// This function will take in an integer value and square it.

int Square(int origionalvalue);

int main(void)
{
int test = 2;

cout << "The current value of our test variable is : " << test << endl;

cout << "Now squaring the test variables value." << endl;
test = Square(test);

cout << "The squared value is : << test << endl;

return 0;

}

int Square(int origionalvalue)
{
return ( origionalvalue * origionalvalue);
}


Share this post


Link to post
Share on other sites
1)
void main(void) - declares the fuction main with no return value,
and no parsed values to it.

int main - equals to "int main(int)"
returns an integer and gets one integer.

int main(void) - function returns int, no value parsed

2)
return 0;
passes the value ''0'' to and then returns to the code that called the function.

3)
lets say you wanna call "int add(int a, int b)"
The function returns a+b:

  
int add (int a, int b)
{
return a+b;
}

// this could be anywhere in your code:

void main(void)
{
printf("%d + %d = %d\n", 1, 2, add(1, 2));
}


HTH
/Fredrik Olsson

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
However you can not use return if your return value on the function is void.


Not quite. You can use return, just not return a value.

void foo(void)
{
return;
}


Gamechamp, invest in a C++ book. Check out the faq for comp.lang.c++ for good titles. C++ FAQ LITE

Share this post


Link to post
Share on other sites
int main()
int main( void )
void main()

First of all this are possible declerations of the
main function in C/C++. There are also other ones for
getting the args you pass with the commandline.

The first two of the function declarations listed above
are equal, just an alternative syntax, but I would
prefer writing () if you mean you don''t need any params,
however this is a matter of style.

The int means the function returns an int,
for the main function this would be treated as the error code.
Under MS-DOS for example you could check the error code in a batch file.

But as far as I can say that, I would suggest for your programs
void main() would be sufficiant.

Then you don''t need to write something like "return 0;" this
is often stupid because it always returns 0 so theres no fun,
except you return/exit before.

It only really makes sense to use a return value if this value is somehow variable and not always the same ...

So this should be enough for now.
But do yourself a favour and by a book on C/C++ or search for
some tutorials on the web or whatever ( no attack, just an advice ).

Share this post


Link to post
Share on other sites
AP:
// Function prototypes NOTE: All functions need a prototype but main.// This function will take in an integer value and square it.

Not really true at all.
If the function is defined before its call you wont need a
prototype for it ...

I just wanted to add that because for writing some really simple
programs at the beginning of your C++ career it doesn''t come in
handy to have a prototype for the only tiny function the proram is all about ...

Share this post


Link to post
Share on other sites
I do have a book, but my friend has it. Anyway, thanks for the help. BTW, I need help with something. I made a cool program, but when it finishes execution, it closes itself, and you can''t see the result. How do you make it stay open until the user closes it? BTW, it''s a Win-32 console application using void main().

Share this post


Link to post
Share on other sites
quote:
Original post by gamechampionx
I made a cool program, but when it finishes execution, it closes itself, and you can't see the result. How do you make it stay open until the user closes it?
You have to use something called a "loop." I seriosly suggest you pick up the book "Sams Teach Yourself C++ in 21 Days" and read away. I don't know how much you know, so I can't exactly give you an example.

If you cannot get that book, go here for a book/tutorial for free.

Good luck!

------------------------------
Simple DirectMedia Layer:

Main Site - (www.libsdl.org)
Cone3D Tutorials- (cone3D.gamedev.net)
GameDev.net's Tutorials - (Here)

OpenGL:

Main Site - (www.opengl.org)
NeHe Tutorials - (nehe.gamedev.net)
Online Books - (Red Book) (Blue Book)


Edited by - Drizzt DoUrden on February 23, 2002 9:39:05 PM

Share this post


Link to post
Share on other sites
According to ANSI, main should always take the form: int main(int argc, char* argv[]) (or, equivalently int main(int argc, char* argv[])). Most compilers will let you get away with having a void return type on it or taking no arguments, but will at least give you a warning. And you should make sure you return EXIT_SUCCESS or EXIT_FAILURE (defined in stdlib.h) from main, suppressing all inclinations to return hard-coded values like, say, 0.

Share this post


Link to post
Share on other sites
For example you could use getch() ( the name should be similar to that ) declared in the nonstandart header conio.h .
This tries to read in a character and if there''s no character
waiting in the standart input stream it waits until you typed some usually. But be warned the behaviour is not defined by the standart ''cause it is no standart function.

But if the one implmented in your library acts like the one I
described above, this is an easy way to accomplish the task you
want to do.

An other way for instance is to start the console app in a DOS-box.

Share this post


Link to post
Share on other sites
Just remember that the objective here is to answer the persons questions, not to confuse the issue by adding in a bunch of exceptions. Just keep it in perspective.

Share this post


Link to post
Share on other sites
> I made a cool program, but when it finishes execution, it closes itself, and you can''t see the result. How do you make it stay open until the user closes it? BTW, it''s a Win-32 console application using void main().

this happens when you run dos programs in windows. when it returns, it closes the window too. if you run a program in msvc++ it is very generous and doesn''t close the window when it returns, it just puts "press any key to continue" at the end. However, if you are not usin this or want to release a program you could prompt for input at the very end. one good example is getch() found in conio.h. basically, it prompts for any key to be pressed. just put this right before your return 0 and your program won''t close until a user presses a key.

~EODCyismARDEM

Share this post


Link to post
Share on other sites
oops, missed somethin. you said your usin void main(), so put getch() before the closing bracket of the void main() function. nm about puting it before return 0, since you don''t return 0.

~EODCyismARDEM

Share this post


Link to post
Share on other sites
EODCyismARDEM, you''re an imbecile. If it''s a Win32 Console application, then it''s a Win32 application, not a DOS application. DOS is an operating system, not a generic term for text-mode programs.

Share this post


Link to post
Share on other sites
ummmm.... gamechampion i duno if you''ve ever ran a dos program in windows but it does close after its finished. i do win32 console apps all the time, i know what hes talking about. i dunno why you think what i''m saying is wrong, why don''t you explain what i am saying is wrong.

~EODCyismARDEM

Share this post


Link to post
Share on other sites
oh, hehe, ok you were sayin that its called a win32 console app instead of dos program. i know i just call it that out of habit. btw, thanks for bein an asshole callin me an imbecile

~EODCyismARDEM

Share this post


Link to post
Share on other sites
The answer to all the original problems?

Buy a book. or at the least borrow one

I also advocate cin.get() instead of getch because the former is standard (and thus portable), and actually C++ (and not C).

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
By the way, champ - I can tell you''re new to C++, because your grammar is too good
welcom to hte jungle.

Share this post


Link to post
Share on other sites