🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

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

Started by
182 comments, last by Dbproguy 16 years ago
Quote: Original post by eektor
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?


Debug->Start Debugging (F5)
Debug->Start Without Debugging (CTRL+F5) -- will pause at the end of the program.

"Start Debugging" has a button on the standard toolbar. I generally add "Build" and "Start Without Debugging" next to it.
"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
Advertisement
Thank you very much, Fruny.
Here is the collection question/answer that I collected from this thread. I hope this is helpful.

History Summary

Computer language was born since the first electronic computers were built. It was represented by long strings of 0 and 1. Because it was too difficult for normal people to understand and wasted too much time to create a simple program, Programming languages were invented to made thing easier and easier after time by time. C was created by Dennis Ritchie in 1971 and Bjarne Stroustrup developed C++ in 1980s based on C language.


What is compiler and IDE?

Compiler is a program that translates a computer language into object code which can then be assembled into machine language. This is necessary for programming in all high level languages (like C/C++ and Pascal) which are not interpreted (like BASIC).

IDE is an Integrated Development Environment. An IDE consists of all the basic tools a programmer needs to create a program. Typically, an IDE consists of a text editor, a compiler, a debugger, and other necessary tools.

[taken from GameDev.net Dictionary]


What the difference between <iostream> and <iostream.h>?

<iostream.h> is out of date and non-standard C++.

What the difference between "void main()" and "int main()"?

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.

[Post by Fruny]


What the difference between "std::endl" and "\n"?

Use "std::endl" instead of "\n". std::endl does more than kick to the next line; It flushes the buffer to the screen. This isn't so important with console output, but makes a difference for file output. Also, I believe some operating systems may require more than just an endline (such as a carriage return), so it's best to keep your code platform independent.

[Post by Sr_Guapo]


Why I got error(s) when I compiled my program?

error C2065: 'endl' : undeclared identifiererror C2065: 'cout' : undeclared identifier

If you got errors like above. Please ensure you use "std::cout" and "std::endl" instead of "cout" and "endl". For example:

#include <iostream>int main(){   std::cout << "Hello World!" << std::endl;}

If you don't like std:: there are other 2 ways to omit it.

First Way
#include <iostream>using std::cout;using std::endl;int main(){   cout << "Hello World!" << endl;}


Second Way
#include <iostream>using namespace std;int main(){   cout << "Hello World!" << endl;}
DinGY
Yesterday is history.Tomorrow is a mystery. Today is a gift"
:) http://gravesworld.org/Code%20blocks.html yup, even debuged ok.

@AdamskiAirsoft you might want to enter this at line 2

#include <stdlib.h>;

And see if this works.
Hmm..?? CodeBlocks seems to be intermixing the two main.cpp files which is goofing up the compilation and building, so I'll be downloading a iso file on VC :)

[Edited by - Yu Une on June 4, 2006 10:50:02 PM]

Extra Exercises



It is 5th day for C++ Workshop and I want to give some Extra Exercises for testing their understand and improve their programming skill.

1. Write a program that print your name on the screen.

2. Track the errors on this code then fix the errors and guess what is this code do without using the compiler. There are 13 errors in this code

#include <iostream>int main(){   cout >> "There are many mistakes in this code." >> std::endl   cout >> "Can you help me fix all the bugs in this code" >> end >> endl;   cout >> "Thank You!" >> std::endl;}


3. Write a program that display something like this:
******* ************ *****


4. Write a program that ask the age of the user and display it on the screen.

For example:
How old are you? : 21You are 21 years old


5. Write a program that ask user to insert 2 numbers and then display the summary, subtraction and multiplication between those two numbers.

For example:
Please insert your first number: 5Please insert your seond number: 2The summary is: 7The subtraction is: 3The multiplication is : 10



Enjoy!
DinGY
Yesterday is history.Tomorrow is a mystery. Today is a gift"
Here is what i got for your questions
#1

#include <iostream>

int main()
{
std::cout << "Ashani Collins" << std::endl;
return 0;
}

#2
#include <iostream>

using namespace std;

int main()
{
cout <<"There are many mistakes in this code."<< endl;
cout <<"Can you help me fix all the bugs in this code" <<endl;
cout <<"Thank you!" << endl;
return 0;
}

#3
#include <iostream>

using namespace std;

int main()
{
cout <<"*******"<< endl;
cout <<"*****" <<endl;
cout <<"*******" << endl;
cout <<"*****" << endl;
return 0;
}

#4
#include <iostream>

using namespace std;

int main()
{
int age;
cout <<"Please Enter your age: ";
cin >> age;
cout <<"\nYou are "<<age<<" years old" <<endl;
return 0;
}

#5
#include <iostream>

using namespace std;

int main()
{
int firstNumber;
int secondNumber;
cout <<"Please insert your first number: ";
cin >>firstNumber;
cout <<"Please insert your second number: ";
cin >>secondNumber;
cout <<"\nThe summary is "<<firstNumber + secondNumber<<"\n";
cout <<"The subtraction is " <<firstNumber - secondNumber << "\n";
cout <<"The multiplication is "<<firstNumber * secondNumber <<"\n";
return 0;
}

That is what i got for the answers to your questions.
Stephen R, thanks for expanding on the definitions from the book. invisal, thanks for the summary and the extra exercises.

I wanted to make sure I understand commenting, because the book doesn't explicitly answer this:

Is it correct that you cannot nest a set of slash-star comments within slash-star comment? For example:

/* outer comments

/* this nested comment will not work
because the second slash-star above will be ignored
*/
and the star-slash above will be taken as the end of the outer comments
leaving an extra star-slash hanging below
*/
Quote: Original post by warsen
Is it correct that you cannot nest a set of slash-star comments within slash-star comment?


It is correct. The C preprocessor does not support nested comments.


Incidentally, I'd like to point out that you are encouraged to try things out on your own. When you believe you understand something, write up a small program that does nothing but test that understanding, and see how the compiler responds to it. Sure, the only thing you'll learn is how that compiler reacts, which is why it is good to have multiple compilers to test things on, but the experiment is still valuable. Do not limit yourself to those examples and exercises that are found in your book.
There's absolutely no shame in doing that -- I do that all the time, especially when answering questions here (to make sure that what I propose really works like I think it does). I have a "project" dedicated to that purpose, with a single source file that gets overwritten over and over.
And it is also much easier to figure things out on a small example than to try and directly integrate a feature you don't understand perfectly into a larger project (<fanboy> Which is why python is so great, since it lets you try things interactively. </fanboy>)

[opinion]
In fact, I'd go to the extent of stating that if you're not willing to experiment by yourself with small snippets of code, you have no business being a programmer.
[/opinion]
"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
Quote: Original post by Fruny

It is correct. The C preprocessor does not support nested comments.


Incidentally, I'd like to point out that you are encouraged to try things out on your own.


Thanks for the reply.

I agree on the experimentation. I leave out parts of the sample code to see what happens and try alternate code to see if I actually understand what's going on. It's the best way to get something to stick.

Of course, if I figured out everything on my own this would be an empty thread. ;)

Finally got my book today(5th edition). Though late, I got it for $10. I'm cruising through the first 2 chapters, I'll post my report soon!!!
I Have Awoken.

This topic is closed to new replies.

Advertisement