Author | Message | ||||
---|---|---|---|---|---|
| ![]() Well hello everyone and welcome to guide for today! This guide will inform you on how to add two numbers(I will also include multiplication and division). Well first off I don't need to tell you how to get Codeblocks etc from my last guide which can be found here: HERE Well anyways once you have a new .cpp file open lets start to program:
#include <iostream> - The "#include" part of the line is including the library "<iostream>". The "<iostream>" part is a header file which is used for input/output in the C++ programming language
using namespace std; - keeps you from having to type std::cout each time you want to display a output on the console it always ends with a ";". int main() - this line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution. "{" - Starting bracket for the main code. int a,b,c; - this declares the integers a,b, and c. It ends with a ";" always. a=1; - Give's the value of "1" to the variable "a" it always ends with a ";". b=2; - Give's the value of "2" to the variable "a" it always ends with a ";". c=a+b; - The above code gives "c" the value of a("1") and b("2") added together. You can change the operation to "*"(multiplication), "/"(division), and "-"(subtraction). This code always ends with a ";". You can change the value of a to anything you want. cout << a << " + " << b << " = " << c << endl; - This is the biggest code piece in the whole cpp.First the "cout << a"" prints the value of "a" on the console screen. Then "<< " + "" prints "+" on the console screen. Next, "<< b" prints the value of "b" on the screen. Then "<< "="" prints "=" on the console screen. Finally "<< c << endl;" prints the value of "c" on the console screen and "endl;" goes to the next line. system("PAUSE"); - Pauses the system so the output on the console screen stays up. "}" - end bracket closes the code. The click the BUIL&RUN button and it should compile fine.
#include <iostream> #include <windows.h> using namespace std; int main() { int a,b,c; a=100; b=333; c=a+b; cout << a << " + " << b << " = " << c << endl; system("PAUSE"); }
|
| ![]() Suggestions: You should have a return statement, and I read somewhere that system("PAUSE") is a bad way to pause a program (though I suppose its appropriate for a simple example like this). Some IDEs pause output after the program runs and wait for a key press anyways. You might also want to mention size limits for data types (4 bytes is usually the limit for an int) and that there are other ways of declaring/initializing variables such as: int a = 30382; int b = 204899; int c = a + b; or even int a = 30382, b = 204899, c = a + b; --- class EOSERV { Programmer | Oldbie Open source EO Client: https://github.com/ethanmoffat/EndlessClient };
|
| ![]() ethanmoffat posted: (15th Jul 2011 07:59 pm) Some IDE's do when you BUILD&RUN it but once you actually run the main .exe program it closes in a second. Also that's the way I declare :P I don't know why but those are some easier ways. Thanks |