How to Write Standard Code in C++
Method 1 of 2:
Writing Standard Code
- Download a C++ IDE (integrated development environment) such as Eclipse, Netbeans, and CodeBlocks, or you can use a plain text editor such as Notepad++ or VIM. You can also run programs from the command line, in that case any text-editor will suffice. It might be handy to choose an editor which supports syntax highlighting and line-numbers. Most programmers find that unix-like systems (linux, OS X, BSD) are the best environments for development.
- Create a main program file. The main file must include a function called main(). This is where execution of the program begins. From here, you should be calling functions, instantiating classes, etc. Other files of your application as well as libraries can be included into this file.
- Begin writing your program. Insert your code or the program that you need to build (see below for a few examples). Learn the syntax, semantics, Object-Oriented Programming paradigms, data striations, algorithm designs such as linked lists, priority queues, etc. C++ is not an easy language to program in, but doing so teaches you the fundamentals that extend to all programming languages.
- Insert comments in your code. Explain what your functions do and what variables are for. Choose clear names for variables and functions. Capitalize the names of global variables. In general: make sure that anyone reading your code can understand it.
- Use proper indenting in your code. Again, see the examples below.
- Compile your code with
g++ main.cpp
- Run your program by typing:
./a.out
Method 2 of 2:
Following Examples
- Take a look at Example 1:
/* This Is A Simple Program Just To Understand The Basic OF g++ Style. This Is A Program With g++ Compiler.*/ #include /* include input and output functions */ using namespace std; /* we are using the std (standard) functions */ int main() /* declare the main function; you can have int main(void) too. */ { cout << "n Hello Daddy" ; /* 'n' is a newline (t is a tab) */ cout << "n Hello Mummy" ; cout << "n This Is My First Program" ; cout << "n Date 11/03/2007" ; return 0; }
- Consider this Example 2:
/* This Program Calculates The Sum Of Two Numbers */ #include using namespace std; int main() { float num1,num2,res; /* declare variables; int, double, long.. work too */ cout << "n Enter the first number= " ; cin >> num1; /* put user's value into num1 */ cout << "n Enter the second number= " ; cin >> num2; res = num1 + num2; cout << "n The sum of "<< num1 <<" and "<< num2 <<" = "<<res 'n' ; return 0; }
- Learn from Example 3:
/* Product Of Two Numbers */ #include using namespace std; int main() { float num1; int num2; double res; cout << "n Enter the first number= " ; cin >> num1; cout << "n Enter the second number= " ; cin >> num2; res = num1 * num2; cout << "n The Product of two numbers = " << res 'n' ; return 0; }
- Take a look at Example 4:
// Looping to find a math equation. In this case, it figures out the answer to // Question #1 on Project Euler. #include using namespace std; int main() { // Opening Main. int sum1=0; int sum2=0; int sum3=0; int sum4=0; // Creates the integers needed in order to figure out the answer. for (int a=0; a < 1000; a=a+3) {sum1 = sum1+a;} // Loops until a is 1000 or over, adding 3 to a every loop. Also adds a to sum1. for (int b=0; b < 1000; b=b+5) {sum2 = sum2+b;} // Loops until b is 1000 or over, adding 5 to b every loop. Also adds b to sum2. for (int c=0; c < 1000; c=c+15) {sum3 = sum3+c;} // Loops until c is 1000 or over, adding 15 to c every loop. Also adds c to sum3. sum4 = sum1 + sum2 - sum3; // sum4 takes the sum of sum1 and sum2, and subtracts sum3. cout << sum4; // Outputs sum4, the answer. cin.get(); // Waits for user to press enter. return 0; // Return statement. } // Closing Main.
- Take a look at this example of different styles:
int main(){ int i = 0; if(1+1==2){ i = 2; } } /* This is Whitesmiths style */ int main() { int i; if (1+1==2) { i = 2; } } /* This is GNU style */ int main () { int i; if (condition) { i = 2; function (); } }
5 ★ | 1 Vote
You should read it
May be interested
- How to write emails effectively and professionally?every day you work with colleagues, partners via email, chat programs ... is a main and professional communication channel at work, how to be effective as well as optimize time and performance which email messages do you bring to you?
- Warcraft 3 code - Summary of code, cheat, Warcraft 3 standardif you are new to playing, you can use the commands (cheat) or warcarft 3 code to ask for money, immortality, revive right in the game..vv. to experience the game more easily. the following article of thuthuatphanmem.vn will summarize for you all b
- What is ASCII? The ASCII code is standard and completeif you are just beginning to learn about ascii code and ascii code table, please refer to the article below. the article provides the concept of ascii code and ascii charset and most complete for your reference and learning. enough to study and work.
- 6 steps to write good content in SEOthe trend of 2013 seo is widely appreciated as a fierce competition among the trends of focusing on content, taking the content as a platform to serve the search needs of users.
- How to Write Pseudocodethis tipsmake article shows you how to write pseudocode text for your computer programs. simply put, writing pseudocode is creating a draft of a non-programming language that clearly states the intent of your code.
- How to read and write files using JES applicationjes is a programming environment that you can use to write, test, and run code locally on your computer. jes offers many features such as photo, video and other media editing.
- JavaScript functionsa function is a group of reusable code that can be called anywhere in your program. this makes it unnecessary to write the same code over and over again. it helps programmers write modular code. functions allow a programmer to divide a large program into small and manageable functions.
- How to fix Attempted Write to Readonly Memory on Windowsattempting write to readonly memory error (stop code 0x000000be) is one of the blue screen errors, which occurs when a driver tries to write to read-only in computer memory.
- Instructions to write and read the code is correctin the article below, network administrator will guide you how to write and read roman numbers correctly. invite you to follow along.
- How to Run HTML Files on Visual Studio Codevisual studio code is microsoft's source code editor. the program is currently available for windows, macos, and linux. with visual studio code, you'll be able to write and edit code in many different programming languages, including html. but what do you do when you want to implement some html code? luckily, there are many extensions for visual studio code that make it easy to execute html code right in visual studio code. additionally, you can also use terminal to run html files. today's tipsmake will show you how to run html files on visual studio code.