Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: Bob on August 10, 2003, 01:09 PM

Title: Well...Don't be to hard on me.
Post by: Bob on August 10, 2003, 01:09 PM
This is going to make me look really newb, but I really don't know anything about programming and I want to learn C++. Not to make bots or whatever, but b/c I want to learn, I want to be good at it. But I have a question I am just starting out and in the Hello world program as I am sure that all of you have seen at least once...

#include <iostream.h>

int main()
{
    cout << "Hello World! " << endl;

return 0;

}
What would be the difference between using int main() or void main()? Would it change the way the program works, Can someone please explain it? Btw, Yes I know I am a newb, but we all have to learn some time.
Title: Re:Well...Don't be to hard on me.
Post by: Eibro on August 10, 2003, 01:19 PM
Assuming your compile compiles void main(), no, it wouldn't change how the program works. However, some compilers won't compile void main() because it's non-standard. main() returns and int, that's just how it is. If you see otherwise (books, internet, etc.) just remember that it's wrong.
Title: Re:Well...Don't be to hard on me.
Post by: Bob on August 10, 2003, 01:23 PM
Thank you very much.
Title: Re:Well...Don't be to hard on me.
Post by: K on August 10, 2003, 02:01 PM
just as a good habit, you should use #include <iostream> instead of #include <iostream.h>.

if you try using iostream.h on newer compilers you will get a warning that it's depreciated, or it won't even be found.  The difference is that the iostream header defines everything inside the std namespace.  So your code would look like this:


#include <iostream>

using std::cout; // this means you can type cout in your program
                         // instead of std::cout every time you want to use it.
using std::endl; // or simply "using namespace std" to include the entire namespace

int main()
{
   cout << "Hello World! " << endl;

   return 0;
}


The reason main returns an int is pretty easy to explain.   Suppose you call a function that returns a variable that indicates whether it succeeded or failed. Your program is like a function to the operating system, with 0 (usually) meaning no errors. You can return different values to help the user or the operating system know what happened when running your program.

Hope that helped.
Title: Re:Well...Don't be to hard on me.
Post by: Bob on August 10, 2003, 05:33 PM
I understood what is under the code, but to have it known I have no perevious programming experience in C++ let alone hardly any other programming language, So with that said I did not really understand above that, but if you are willing to explain more thoroughly,if possible, then I am willing to try to understand. Thank you for your patience so far.
Title: Re:Well...Don't be to hard on me.
Post by: Eibro on August 10, 2003, 06:04 PM
In C++, you can declare and use namespaces.
Namespaces help avoid naming conflicts. If you put all code pertaining to, say, window creation in namespace Window. Domeone else can then come along later and not worry about conflicting names or identifiers. You could have a function, Create() inside namespace Window, and another Create() function in another namespace. Example:
namespace Window
{
  bool Create(const char* name)
  {
     // function body here
  }

  // more functions, etc.
}
That would define the Create function inside namespace Window. To use the Create() function later in your code, you have to do at least one of the following:




1) using namespace Window; This brings all contents of namespace Window into scope.  You can then use anything (function, variable, etc.) within namespace Window by simply using the name it was declared with within that namespace. Example:using namespace Window;
bool Result = Create("MyWindow"); // Calls Window::Create(...)





2) You can bring only portions of a namespace into scope by using the using keyword. An example which would bring the Window::Create() function into scope:using Window::Create;
bool Result = Create("MyWindow"); // Calls Window::Create(...)





3) You can explicitly scope to the namespace each time you want to use an identifier contained within that namespace. bool Result = Window::Create("MyWindow"); // Calls Window::Create(...) [obviously]




With that, the C++ standard library is declared in namespace std. Therefore, to use any functions or identifiers in the standard library (cout, cin, endl) you need to scope to namespace std.
Title: Re:Well...Don't be to hard on me.
Post by: Banana fanna fo fanna on August 13, 2003, 09:52 AM
Bob: well asked question. +1
Title: Re:Well...Don't be to hard on me.
Post by: Raven on August 13, 2003, 06:32 PM
+1 to everyone who responded to Bob's question. This is more like it should be; none of those 1337 dUd3z saying "OMG HAHAHA n00b!". As long as people are willing to admit they don't know something and courteously ask for help, help should always be provided for them. :)
Title: Re:Well...Don't be to hard on me.
Post by: Camel on August 13, 2003, 08:16 PM
+1 to Raven for posting once in the "proper way to post" thread instead of posting in all the others about how much they suck.
>.<
Title: Re:Well...Don't be to hard on me.
Post by: Raven on August 15, 2003, 03:50 PM
Quote from: Camel on August 13, 2003, 08:16 PM
+1 to Raven for posting once in the "proper way to post" thread instead of posting in all the others about how much they suck.
>.<

Wha? ;D
Title: Re:Well...Don't be to hard on me.
Post by: Vamp on August 22, 2003, 08:59 PM
If void Main() and int main() are the same, then why even use void main(), and how did void main() even like, start being used?
Title: Re:Well...Don't be to hard on me.
Post by: Adron on August 23, 2003, 08:18 AM
Quote from: Vamp on August 22, 2003, 08:59 PM
If void Main() and int main() are the same, then why even use void main(), and how did void main() even like, start being used?

Void main saves you "having" to return a value from main. It saves you one line of typing ;)
Title: Re:Well...Don't be to hard on me.
Post by: Hazard on August 23, 2003, 08:23 AM
Quote from: Adron on August 23, 2003, 08:18 AM

Void main saves you "having" to return a value from main. It saves you one line of typing ;)

Thank God for shortcuts.

!~!HaZaRD!~!
Title: Re:Well...Don't be to hard on me.
Post by: Yoni on August 23, 2003, 08:32 AM
void main() is nonstandard though, so people who use it are violent criminals. You should use int main() only.
Title: Re:Well...Don't be to hard on me.
Post by: Adron on August 23, 2003, 09:43 AM
Boycott proper C++ until they allow void main! They have no right to force us to return a value from main! :P
Title: Re:Well...Don't be to hard on me.
Post by: Vamp on August 23, 2003, 09:59 AM
in one tutrial, it used
int main(void)
in everything, but also had a return value, so like whats the diffence with this one?
Title: Re:Well...Don't be to hard on me.
Post by: Yoni on August 23, 2003, 11:07 AM
Quote from: Vamp on August 23, 2003, 09:59 AM
in one tutrial, it used
int main(void)
in everything, but also had a return value, so like whats the diffence with this one?

That just means it has no parameters. This is perfectly legal.
Some valid mains:

int main()
int main(void)
int main(int argc, char *argv[])
int main(int argc, char *argv[], char *envp[])


The first two are essentially the same, and the last two are for when you need to get commandline/environment info.
Title: Re:Well...Don't be to hard on me.
Post by: Eibro on August 23, 2003, 07:37 PM
Quote from: Yoni on August 23, 2003, 11:07 AM
Quote from: Vamp on August 23, 2003, 09:59 AM
in one tutrial, it used
int main(void)
in everything, but also had a return value, so like whats the diffence with this one?

That just means it has no parameters. This is perfectly legal.
Some valid mains:

int main()
int main(void)
int main(int argc, char *argv[])
int main(int argc, char *argv[], char *envp[])


The first two are essentially the same, and the last two are for when you need to get commandline/environment info.
Also, the last one is non-standard, iirc.
Title: Re:Well...Don't be to hard on me.
Post by: Yoni on August 23, 2003, 08:55 PM
Quote from: Eibro on August 23, 2003, 07:37 PM
Also, the last one is non-standard, iirc.
Eibro is correct. envp is not standard as I thought, but rather a very common extension. +1!

The standard way to get environment variables is getenv() in stdlib.h.
Title: Re:Well...Don't be to hard on me.
Post by: Bob on August 26, 2003, 09:06 AM
Well It's Bob again  ;D Long time no post, But I've learned a little bit more and I was wondering if anyone could give me some sort of a "quiz program" to test what I have learned nothing to difficult--Thanks--

When I say quiz program I mean something for me to make to test my knowledge thanks again--
Title: Re:Well...Don't be to hard on me.
Post by: Adron on August 26, 2003, 10:26 AM
Make the command line application Drag that implements this function.

Start the program with a file name (wildcards allowed) as argument.


C:\Music>Drag *kylie*mp3


The mouse cursor becomes a "drag file" cursor, and you can drop the files into a msn chat window to send them to someone.

You will find the first file name in argv[1]. Allow multiple files to be dragged at once.

Post the source code here.
Title: Re:Well...Don't be to hard on me.
Post by: Dricoust on November 06, 2003, 06:42 AM
Hi,
main's return value tells the os on how the application ran,
return 0; Everything ran correct.
return 1; Dident run correct.

Using void main is acceptable by all means. Although as i have acknowledged, when void is used the os still requires the status of what happened, and since no return. It always returns 0;.
Usually when using int main() you can return the value (Ending the program) at you're own choice. ie

#include <iostream>
using namespace std;

int main()
{
char cmd[5];
cout << "Enter a number between one and 5";
cin >> cmd;
if(atoi(cmd) > 5) {
                             return 1; // Assuming the value is critical to be
                                            // between 1 & 5.
                           }
else { return 0; }
}

As to my understanding.
And you are certainly not a 'newb' by any means. Damn i had trouble with JavaScript first time i used it.
Now i have my own chat server, using VB as script =]

Refrences:
C++ From The Ground Up
MFC Programming With Visual C++.NET 2003
C++ The Complete Refrence Edition1 - 5.
Programming With DirectX9a.
Title: Re:Well...Don't be to hard on me.
Post by: Banana fanna fo fanna on November 06, 2003, 05:31 PM
This thread is pretty stale.