Visual C++ 2005 Programming guide v1.5 NOTE: This also works in Visual Studio 6.0 Enterprise now, too! Opening the IDE Start > All Programs > Microsoft Visual Studio 6.0 > Visual C++ 6.0 Start > All Programs > Microsoft Visual Studio 2005 Making a new project File > New... > Project (or press Control + Shift + N) Project types > Visual C++ > Win32 > Win32 Console Application Enter a name for the project ("guide" is used here) Press OK > Press Next Use the following settings: Application type - Console application Additional options - Precompiled header Press Finish Editing the project for defaults Erase the comments (//text) from the top of the file and add your own Describe the beginning and end date, revision and / or version number and other important information. Change "int _tmain(int argc, _TCHAR* argv[])" to: "int main(int argc, _TCHAR* argv[])" Including standard libraries Add these at the beginning of your program to get extra functions: "stdafx.h" - Visual Studio requires this. - Allows use of cout and cin. You probably want these. - Math. What else? - Lets you use things like strings. You probably want this. - Lets you use strings with cin. "windows.h" - Windows-specific stuff, like beeps and bloops or time garbage. - Useful for getting system time and random numbers. Including extra freebies Add these at the beginning of your program (after the includes) to get extra stuff: Pause function - Waits for user input. Requires Use pause(option); void pause(int option) { char pauseVariable; if(option==0) system("PAUSE"); if(option==1) system("PAUSE>NUL"); if(option==2){ cout<<"Press any key and enter to continue..."<>pauseVariable; } return; } Linebreak function - Inserts the specified number of lines. Requires Use brk(); void brk(int lines) { for(int i=0;i, "windows.h" Use disp(,) void disp(char text[16],short delay) { for(int i=0;i<16;i++) { cout< Use =rng(); int rng(int sides) { int out=rand()%sides+1; return out; } Clearscreen - Clears the terminal Requires "windows.h" Use cls(); void cls() { system("cls"); } Useful keyboard shortcuts Pressing Control + S saves the current project Pressing Control + Shift + S saves all open projects Pressing F5 will run the program in debug mode Pressing Shift + F5 will run the program without debug mode Pressing F7 will build the current project Pressing Control + Alternate + F7 will rebuild the current project Pressing Control + N will add a new source file to the project Pressing Control + Shift + N will make a new project Microsoft Visual Studio 2005 also supports editing commands: Control + Z - Undo Control + Y - Redo Control + X - Cut Control + V - Paste Control + C - Copy Control + A - Select All Actually learning to program After creating a new project, import the appropriate libraries for the program you want to write. These are described above. Use the command: #include (put library here) to include libraries in your program. Ensure it is at the beginning. Below your include commands, called declarations, add the following line: using namespace std; This will tell VC++ that you want to use the standard namespace. After changing the details of main() as described earlier, open the loop (using the curly braces { and }) and insert your program here. Some instructions and their syntax Common output Using the default C++ libraries you can use the following command to write text to the terminal: printf("Text"); Printf supports the following formatting commands: \a - System bell character (beep) \n - Linebreak Using , you can use the following command to write text to the terminal: cout<<"Text"; Cout supports the following formatting commands: endl - Linebreak You can't use some symbols without escape codes. The following characters are affected: \, ', ", ? To print these characters: \\, \', \". \? Formatting commands: \t Horizontal tab \f Form feed \v Vertical tab \r Carriage return (no new line) \b Backspace Variable initialization Variable types Static Does not change, program will not write to it. Define as static ; Volatile Can change at random, whether in the program or outside of it. You shouldn't use this often, it's supposed to be reserved for things the hardware can change, like the date and time or the amount of system memory free for use. Define as volatile ; Normal Program can read and write to it. Variable values Boolean True / False Initialize as bool ; True or false only using boolean logic. Floating Point 3.141592653589... Initialize as float ; Integers with a decimal that can move. Double Precision 4.99 Initialize as double ; Integers with a fixed decimal to the hundredths position. Short Integer 16384 Initialize as short ; Integers between -16384 and 16384. Long Integer 4294967296 Initialize as long ; Integers between -4294967296 and 4294967296. Integer 72 Initialize as int ; Integers that have varying values. Character A Initialize as char ; A single character (0-9, A-Z, etc.) String Hello, World! Initialize as char[] ; A string of characters. Void Initialize as void ; Absolutely nothing. Array ABCDXYZ Initialize as [] ; Holds an array of values. Matrix 1: ABC 2: XYZ Initialize as [][] ; Holds multiple arrays of values. Defining functions NOTE: Return type *cannot* be array or matrix! Use the following format: ( ,) { } For example: int add(int a,int b) { int c; c=a+b; return c; } Calling the function as such: d=add(1,2); Would result in D being 3. Another exmaple: float div(int a,int b) { int c; c=a/b; return c; } Calling the function as such: d=div(7,2); Would result in D being 3.5. Functions that return a void value do not need to return anything. For example: void brk() { cout< && AND Is only true it both are true && || OR Is true if one or the other are || Mathmatical operators ++ Increment Adds 1 to a variable ++; -- Decrement Subtracts 1 from a variable --; sqrt() Square root Gets the square root of a value =sqrt(); Conditional statements Conditional statements only run when a statement is true. This can be a single variable or a full statement. Types of conditional statements while While any static statement is true, it will loop. do Will always loop regardless. for Will loop until the statement is no longer true. if Will do something once if a statement is true. switch Will do something only in a certain case. Syntax of conditional statements Do loop do { } While loop while() { } If statement if() { } For loop for( =,, ) { } Switch statement switch() { case : break; default: break; } Examples: do { cout<<"Hello, world!"< included, use cin cin>>; cin>>a; Beeps and bloops Import windows.h Call Beep(); with the following syntax Beep(,); Example: Beep(440,500); Would play a perfect A note for half a second. Instruction comparison ECHO Hello, world! cout<<"Hello, world!"<>var; GOTO LABEL label(); TITLE Test Program SetConsoleTitle(TEXT("Test Program"); No equiv. Sleep(500); No equiv. cout<<"\a"; No equiv. Beep(440,500); :INIT void init() PAUSE cin>>pauseVariable; EXIT exit; No equiv. return; No equiv. break; COLOR 9F See below... CLS cls(); IF %A%==4 GOTO LABEL if(a=4) Colors in the C++ terminal Colors are possible! Requires "windows.h" Use system("COLOR 9F"); However! This only can change the colors of entire terminal window. No changing single characters in the window! Setting variables Most variables = ; Some variables have multiple pieces of data. Arrays [item ID] = ; Matrices [row][column] = ; BATCH Commands Most single-line BATCH commands can be executed with the "system();" command. Syntax: system(""); Graphics Requires the Borland Graphics Interface (BGI) files to be installed. It's best to simply start out using the BGI base files or the latest C++ template file, since installing the graphics files can take a bit of work and is more specialized to specific projects. The Borland Graphics Interface uses devices and viewports. A viewport is the window in which graphics are drawn, and is stored on a specific device. To create a new viewport: initwindow(,,); Where width and height are the size of the window and title is the name of the window.