Table of Contents
This updated guide examines How to Write Standard Code in C++ and organizes the essential facts, background, and practical takeaways in clear American English.
Method 1
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
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 */usingnamespacestd;/* we are using the std (standard) functions */intmain()/* declare the main function;youcanhaveintmain(void)too.*/{cout<<"nHello Daddy";/* 'n' is a newline (t is a tab) */cout<<"nHello Mummy";cout<<"nThis Is My First Program";cout<<"nDate 11/03/2007";return0;} - Consider this Example 2:
/* This Program Calculates The Sum Of Two Numbers */#includeusingnamespacestd;intmain(){floatnum1,num2,res;/* declare variables; int, double, long.. work too */cout<<"nEnter the first number= ";cin>>num1;/* put user's value into num1 */cout<<"nEnter the second number= ";cin>>num2;res=num1+num2;cout<<"nThe sum of "< - Learn from Example 3:
/* Product Of Two Numbers */#includeusingnamespacestd;intmain(){floatnum1;intnum2;doubleres;cout<<"nEnter the first number= ";cin>>num1;cout<<"nEnter the second number= ";cin>>num2;res=num1*num2;cout<<"nThe Product of two numbers = "< - 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.#includeusingnamespacestd;intmain(){// Opening Main.intsum1=0;intsum2=0;intsum3=0;intsum4=0;// Creates the integers needed to figure out the answer.for(inta=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(intb=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(intc=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< - Take a look at this example of different styles:
intmain(){inti=0;if(1+1==2){i=2;}}/* This is Whitesmiths style */intmain(){inti;if(1+1==2){i=2;}}/* This is GNU style */intmain(){inti;if(condition){i=2;function();}}
FAQ
What is How to Write Standard Code in C++ about?
It provides a structured overview of program, explains the main context, and highlights practical takeaways for readers.
Why does this topic matter?
Understanding the main concepts helps readers evaluate the issue, avoid common mistakes, and make better-informed decisions.
How should readers use this information?
Use the guidance as a practical starting point, confirm details that may have changed, and follow current product, safety, or security recommendations.
Reader Comments 0
Sign in with email or Google to join the discussion.