crushed (12-05-2009)
Info:
I'm going to show and explain some of the basics of C++ for all the new people that want to learn something. Should my information be incorrect, feel free to correct me.
1. The Main() Function
Info:
This is the function that happens the second your program starts and is USUALLY the last piece of code that runs, also.
Example: (int comes before main() because it returns an integer value)
2. VariablesCode:int main() { cout << "Hello World!"; system("pause >nul"); return 0; }
Info:
Variables store information for use during your program.
Types of Variables:
Integers - Store numbers ONLY.
Boolean - True or False.
Char - Stores ONE character
Float - Integers are great for counting whole numbers, but sometimes we need to store very large numbers, or numbers with a fractional component. A floating point type variable is a variable that can hold a real number, such as 4.0, 2.5, 3.33, or 0.1226. (Credits to Learn C++ - 2.5 — Floating point numbers since I couldn't think of a way to explain it.)
Strings - Store multiple characters. (Strings require an additional header to be defined before use.)
3. Basic Output/Input
Info:
Basic statements that allow you to write something to the console window or recieve user input for the value of a variable.
Examples:
This writes Hello World! to the console window:
This receives user input for the variable x:Code:cout << "Hello World!";
Code:int x; cout << "What is x?"; cin << x;
4. If Statement
Info:
Allows you to check if a boolean is true (or even false, in some cases) and if so, do a piece of code.
Example:
5. Loop StatementsCode:int a = 1; if(a = 1) { cout << "Integer A is equal to one!"; }
Info:
Loops allow you to continuously do something until you get the wanted result. (Like +1 until the variable is 10)
Examples:
If you just try to pop these codes into your compiler, they won't do anything for you without proper use and proper headers being defined. This is just meant to show newer C++ coders some of the fundamentals of C++ to get started with. If you have any suggestions, please post themCode:while(a=1) { a = a+1; } for(int i = 0; i <= 10; i++) { cout << i << endl; }
crushed (12-05-2009)
Ah, this is good, but I noticed cin and cout.
use cout like this: cout << (variables, expressions, etc. )
use cin like this: cin >> ( same as above. )
you had the << and >> mixed up.
also, at the end of a line of input or output, you should get into the habit of using <<endl or \n
and can you explain what a double is? They confuse me.
The character or char data type holds a numerical value from 0 to 256, and each number stands for a character on a keyboard. Let me sift through my source code collection and see if I can find something showing this.
Last edited by t7ancients; 12-05-2009 at 11:16 AM.
Yay! I found it in this massive folder with like 700 different source files in it...
Code:#include <iostream.h> int main() { using namespace std; for (int i = 0; i<256; i++) cout << (char) i; getchar(); // waits for a key to be pressed to continue execution of code. return 0; }
a double is named for its size in memory which is a DWORD or double word. An int (integer) is a word) so a double is twice the size and allows for floating point operations. I forget how large it can be, but its a pretty large value.
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
When you give examples you should include all the header files and enclose in a function, as well as give an example of all the variables. Not to be harsh but this would be hard to understand for a beginner.
Goals In Life:
[X] Become an Advanced Member
[X]Release a tut on mpgh
[0]Post 300 posts
[X]Make a working hack
[X] Learn c++
pixi, you don't really need to put using napespace for anything, when I leave it out it all works fine. I'm using borland 5.5 compiler so maybe it's just that. I haven't seen anyone else on here that uses borland, it's either dev c++ or msvs.
shouldn't while(a=1) be while(a==1) because it is checking a value and not changing one.Code:while(a=1) { a = a+1; } for(int i = 0; i <= 10; i++) { cout << i << endl; }
and replace a = a+1; by a++; does the same but looks pro...
a +=1 is possible too
Hell_Demon (12-05-2009)
an incrementing value has less margin for error, so a++ would be more efficient. looking good is nice and all, but shouldn't practicality hold more priority? and a = 1 as a parameter in a while loop doesn't change the value of a to 1. a = 1 does the EXACT same thing as a == 1 in a loop parameter and takes less to type. you seem like you've confused the syntax and rules of c++ with c#.
lalakijilp is right. It changes the value of a to 1, and if that succeeded it continues with the loop.
Code:int _tmain(int argc, _TCHAR* argv[]) { int a=1337; bool bdone=false; while(a=2) { cout<<a<<endl; if(bdone==true) { system("pause"); return 1; } bdone=true; Sleep(1); } return 0; }
Last edited by Hell_Demon; 12-05-2009 at 04:40 PM.
Ah we-a blaze the fyah, make it bun dem!
I say this is absolutely easy. Therefore, if people don't understand this then I don't know what they will.
I suggest you continue this, and it should be added to the C++ Tut thread under: for beginners as a reference for other people that are just starting C++. It's simple, and to the point.
Perfect for a noob like me.