How to Program in C++ and Batch

C++ is a mid-level programming language—it easy to write and it runs very quickly. As a result, it is widely used to develop games, business apps, and software, such as Google Chrome and Microsoft Office Suite.https://www.youtube.com/wat...
Method 1 of 4:

Getting Started with C++

  1. How to Program in C++ and Batch Picture 1How to Program in C++ and Batch Picture 1
    Introduce yourself to C++ language. C++ is related to C programming language. Unlike its predecessor, C++ is an object-oriented programming language. The object is the primary unit of this language—every object has specific properties, functions, and methods.[2]
  2. How to Program in C++ and Batch Picture 2How to Program in C++ and Batch Picture 2
    Download and install a compiler. In order to create viable programs with C++, you will need to download and install a compiler. Compilers transform your code into operational programs. There are free compilers available for Windows, Mac, and Linux users.
    1. Windows: Code::Blocks
    2. Mac: Xcode
    3. Linux: g++.
  3. How to Program in C++ and Batch Picture 3How to Program in C++ and Batch Picture 3
    Find useful introductory resources and tutorials. Learning C++ is equivalent to learning a foreign language. Books, courses, and tutorials will help you establish a foundational understanding of this programming language. You will find a variety of free and purchasable resources online.
    1. Consult a comprehensive list of books and guides.[3]
    2. Enroll in a C++ programming course. You may find courses at your local college, library, adult education center, and/or online. You could even join a MOOC (Massive Open Online Course).
    3. Complete a step-by-step tutorial. You can work your way through free tutorials or subscribe to a tutorial service, like Khan Academy or Lynda.[4]
Method 2 of 4:

Creating a Basic C++ Program

  1. How to Program in C++ and Batch Picture 4How to Program in C++ and Batch Picture 4
    Launch your compiler and create a new C++ project.
  2. How to Program in C++ and Batch Picture 5How to Program in C++ and Batch Picture 5
    Select ''main.cpp.''
  3. How to Program in C++ and Batch Picture 6How to Program in C++ and Batch Picture 6
    Write a 'Hello World' program. Traditionally, the first program people create simply reads 'Hello World!'. When you create a new C++ project, the 'Hello World!' program will automatically appear in the file. Erase the existing code and rewrite it for yourself:
    #include <iostream> using namespace std; //main () is where program execution begins. int main () { cout <<'Hello World'; //prints Hello World return 0; } 
  4. How to Program in C++ and Batch Picture 7How to Program in C++ and Batch Picture 7
    Understand the meaning of '#include .' This line of code appears in the file's header. The directive '#include' tells the program to include the file '' in the current source file. Your C++ programs will not "compile" without this code[5]
  5. How to Program in C++ and Batch Picture 8How to Program in C++ and Batch Picture 8
    Understand the meaning of 'using namespace std;.' This line of code tells the compiler to use the standard C++ library. The standard C++ library is a collection of common functions and classes.[6]
  6. How to Program in C++ and Batch Picture 9How to Program in C++ and Batch Picture 9
    Understand comments. Programmers use comments to annotate their code so that they (or anyone else reading the code) can understand more about what a particular section of code is meant to do. Comments appear in the code text but do not affect the program. In the "Hello world" program, "//main () is where program execution begins" is an example of a single line comment.
    1. Single line comments always begin with "//" and stop when the line ends.
  7. How to Program in C++ and Batch Picture 10How to Program in C++ and Batch Picture 10
    Understand the program's function. In C++, functions execute individual tasks. In the 'Hello World' program, int main() is the main function. Program execution begins at this line of code. The statements inside the brackets describe the actual function.
    1. The statement cout << "Hello World"; generates the words 'Hello World' on your screen.
    2. The statement return 0; terminates, or ends, the main function.[7]
Method 3 of 4:

Exploring Batch Files

  1. How to Program in C++ and Batch Picture 11How to Program in C++ and Batch Picture 11
    Understand batch files. Batch files are exclusive to Windows—the Mac counterpart is a bash file. Batch files contain a one or more commands that are executed in sequence by a command line interpreter. These files are used to simplify basic and/or repetitive jobs such as opening multiple programs, deleting files, and backing up files.[8] You may incorporate batch files into your C++ programs.[9]
  2. How to Program in C++ and Batch Picture 12How to Program in C++ and Batch Picture 12
    Create a batch file. Batch files are simple text files. You may create your batch files with Window's text editor, Notepad.exe. Click Start and type 'notepad' into the search bar, and select 'Notepad' from the results.[10]
  3. How to Program in C++ and Batch Picture 13How to Program in C++ and Batch Picture 13
    Save the file. Click File > Save. Rename your file 'HelloWorld.cmd.' Change the 'Save as type' to 'All Files (*,*).'
    1. If you are using a modern version of Windows, use the extension .cmd. If you are using an older system, use the extension .bat.[11]
  4. How to Program in C++ and Batch Picture 14How to Program in C++ and Batch Picture 14
    Code a 'Hello world' batch file. In the text editor, enter in the following lines of code:[12]
    @echo Hello world. @pause 
  5. How to Program in C++ and Batch Picture 15How to Program in C++ and Batch Picture 15
    Understand '@echo.' In batch, commands are echoed, or displayed, on the output screen by default. When a program runs, you will see the command and its output. Preceding this command with an "@" turns off echoing for a specific line. When the program runs, you will only see "Hello world."[13]
    1. You can turn of all echoing with the command '@echo OFF.' If you use this command, you can rewrite the program as:
      @echo Off echo Hello world. pause 
  6. How to Program in C++ and Batch Picture 16How to Program in C++ and Batch Picture 16
    Understand '@pause.' This command tells the command line processor to pause until the user presses a key on the keyboard.[14]
  7. How to Program in C++ and Batch Picture 17How to Program in C++ and Batch Picture 17
    Run your batch file. The fastest way to run your batch file is to simply double-click on the file. When you double-click on the file, the batch file is sent to the DOS command line processor. A new window will open and your batch file will close. Once the user presses a key to continue, the program will end and the window will close.[15]
Method 4 of 4:

Applying Your New Knowledge

  1. How to Program in C++ and Batch Picture 18How to Program in C++ and Batch Picture 18
    Incorporate functions into your code. A function is a group of statements, or instructions, that perform a specific task. Each function is assigned a type, a name, parameter(s), and statements. You will use the C++ function 'system' to run a batch file. To explore functions, try coding this program:
    // function example #include <iostream> Using namespace std; int addition (int a, int b) { int r; r=a+b return r; } int main ( ) { int z; z = addition (5,3); cout << 'The result is ' << z; } 
    1. This program contains two functions: ''addition'' and ''main''. The compiler will always call '''main'' first—in this program it will call the variable 'z' of type 'int''. The call will pass along two values, 5 and 3, to the ''addition'' function. These values correspond to the parameters declared by the 'addition' function—'int a, int b'.
    2. Inside the 'addition' function, there is a third variable: '(int r)', that is directly related to the expression r=a+b. The two values from the 'main' function, 5 and 3, will be added together to equal 'r.' In this instance, r equals 8.
    3. The final statement, 'return r;' ends the 'addition' function and returns control to the 'main' function. Since 'return' has a variable, 'r,' the call to return to 'main' is evaluated as a specific value and sends this variable to the 'main' function.
    4. The 'main' function resumes where it left off when it was called to 'addition': 'cout << 'The result is ' << z;.' This line of code prints 'The result is 8' on the screen.[16]
  2. How to Program in C++ and Batch Picture 19How to Program in C++ and Batch Picture 19
    Experiment with flow control statements. Statements are individual instructions that are always executed in sequential order. C++ programs, however, are not limited to linear sequences. You may incorporate flow control statements to alter the path of your program. The 'while loop' statement is a common flow control statement—it tells the program to execute a statement a specific number of times or while the condition is fulfilled.
    // custom countdown using while #include <iostream> using namespace std; int main () { int n = 10; while (n>0) { cout << n << ", "; --n; } cout << "liftoff!n"; } 
    1. 'int n= 10': This line of code sets the variable 'n' to 10. 10 is the first number in the countdown.
    2. 'while (n>0)': The loop will continue as long as the value of 'n' is greater than 0.
    3. If the condition is true, the program executes the the following code: 'cout << n << ", "; --n;'. The number '10' will appear on the screen. Each time the loop is executed, the number 'n minus 1' will appear on the screen.
    4. 'cout << "liftoff!n";': When the statement is no longer true—when 'n' equals '0'—the phrase 'liftoff!' will appear on the screen.[17]
  3. How to Program in C++ and Batch Picture 20How to Program in C++ and Batch Picture 20
    Run a batch file with C++. When you run a batch file with your C++ program, you will use the 'system ( )' function. The 'system' function tells the command line processor to execute a command. Enter the batch file's name within the parentheses of the 'system ( )' function.[18]
    source(HelloWorld.cmd) 
4.5 ★ | 2 Vote