Advertisement

C++ Workshop - Getting Started with C++ (Ch. 1 & 2)

Started by June 01, 2006 11:40 AM
182 comments, last by Dbproguy 16 years, 3 months ago
Quote: Original post by kaydash
Well I though this was suppose to be a C++ 6.0 workshop!!!

No.

Quote: Darn man, I have microsoft visual studio .net 2003 on my laptop. But I don't know how to use it.

Learn. It comes with excellent documentation. Or ask here for tips. Why have software install that you not only don't use, but never learn to use?

Quote: Secondly, do I need visual studio 2005 express edition?

That'd be Visual C++ 2005 Express Edition. Each "Express" product is distinct, which can simplify usage significantly for people who don't need the language-integrated features or other compilers included with the full Visual Studio product.

Further, Visual C++ 2005 Express Edition is part of the Visual Studio 2005 family (it's internal version is 8.0; 6.0 is now about 11 years old, so I absolutely recommend that you do not use it), which means it contains the latest versions of Microsoft's optimizing C++ compiler, with better performance and standards adherence.

Third, Visual C++ 2005 Express Edition is free. Totally, completely free, in perpetuity - that is, forever. If you can't afford to spend money on development tools as yet, that makes it a sweet deal. Also, if you acquired previous versions of Visual Studio products through less-than-ethical means, you can right those wrongs by replacing those versions (which you don't use, anwyay) with the Express product.

Lastly, don't you think it's odd that you're including dos.h in a console-mode Windows program? Shouldn't that tell you something? (Either your compiler is crap or your code is. This workshop is a chance for you to learn about modern tools and programming practices. Take full advantage of it.)

Good luck!
Help!!!!

i've noticed that in some C++ books, that in the very first program (normally 'Hello World') sometimes it uses "Int Main()" and sometimes "Void Main()". What's the difference between the two?
You're looking at a wanna-be right now :P
Advertisement
Quote: Original post by destron
Help!!!!

i've noticed that in some C++ books, that in the very first program (normally 'Hello World') sometimes it uses "Int Main()" and sometimes "Void Main()". What's the difference between the two?


Hi. int main is correct, as you can use a value to indicate whether you exited your program cleanly or exited under an error.

If the int that main returns is 0, this should mean all went well. If you are exiting because of an error, you can return any other value, for example return 1 as opposed to return 0.

void main is to be avoided, it is non-standard. Note also that case is importaant, both "int" and "main" need to be lowercase.
Quote: Original post by destron
Help!!!!

i've noticed that in some C++ books, that in the very first program (normally 'Hello World') sometimes it uses "Int Main()" and sometimes "Void Main()". What's the difference between the two?


"int" and "void" refer to the value that a function can return. You will learn more about funcgtions and return types in later chapeters. The value that you return tells the Operating System that you are done and everything worked (or not). It isn't so important for current systems, but it is still considered good practice to return a value.

Edit: Wow I'm slow...
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute
Quote: Original post by destron
Help!!!!

i've noticed that in some C++ books, that in the very first program (normally 'Hello World') sometimes it uses "Int Main()" and sometimes "Void Main()". What's the difference between the two?


int main() is correct, void main() is not. That is all.

The C++ standard mandates that main() return an int. Anything else is incorrect. void main() is explicitely marked as being incorrect. That value is passed back to the program that executed your program (e.g. Windows GUI or command line) and can be used in shell scripts (or batch files) to determine whether the program succeeded or not.

You do not actually have to explicitely return a value (though you still must define main() as returning an int), since "falling off" the end of main() is equivalent to returning 0.

Some compilers that were written before C++ got standardized accepted void main(). Outdated tutorials, or tutorials written by people with incompled or outdated knowledge may use void main() even though it is wrong. Same with books.


[warning]
If you see a tutorial using void main() or, for that matter #include <iostream.h>, beware! Odds are the tutorial is outdated or contains other incorrect information.

Also note that even if your compiler accepts some code you have written, it doesn't mean it is valid, standard C++.
[/warning]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
ok, thanks.
You're looking at a wanna-be right now :P
Advertisement
I'm using Dev-C++, and have used it before, as in yesterday to do some coding, and it worked perfectly. Now, when I tried to do it for this lesson, I'm running into issues with it. My code is:

1 #include <iostream>
2
3 int main();
4 {
5 cout << "Hello World!";
6 return 0;
7 }

When I compile it I get these two errors errors:

1) Expected unequalified-id before '{' token
2) Expected ',' or ';' before '{' token

I have no idea what's wrong with my code. I get the same error in Code Blocks, and it doesn't work in Visual C++ Express.
3 int main();

5 std::cout << "Hello World!";


Quote: My other question, is about Visual C++ Express. I've just finnished installing it, but was wondering how portable it was? If I write code in that, and compile it, will it run on Linux and Mac? Or will it only compile to Windows?


If you write pure standard C++ code, you will be able to recompile your program without changes on Linux and Mac, and run the resulting program. Do not expect the executable itself to run on another platform.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Aaah, thanks Fruny! You made me realize that I wans't using the using deal like their usually is.
I'm familiar with Dev C++ and I'm trying to learn how to use Visual C++ 2005 EE and I'm a bit confused.

In Dev C++, I used compile and then run or used compile and run, to get my projects or code running. In Visual C++ 2005 EE, I use Build solution or Build [insert project name here], but I don't know how to run the code in the IDE. What I did was looked through the folders and I finally found the Hello World.exe and then ran it from there. Is there an easier way to do this?

This topic is closed to new replies.

Advertisement