_________SWAT MAGAZINE ISSUE TWENTY-ONE: SEPTEMBER 1999__________ / \____________________________________________/ \ / Programmation guide for beginner ( Part 2 ) \ / By Sir. Lestat UIN: 512262 \ ------------------------------------------------------------------------ The second part is finally here. I hope you will enjoy it. There is a lot of code in it so you can see how to use the theorie. +-------+ | Array | +-------+ In C/C++ a array is declared and use like a normal variable but followed with the number or numero of elements between []. #include void main ( void ) { short my_array[ 3 ]; // Declaration short normal_short; my_array[ 0 ] = 12; // Usage my_array[ 1 ] = 37; my_array[ 2 ] = 6; normal_short = my_array[ 0 ]; // normal_short now equal 12 printf( "%hd %hd %hd\n", my_array[ 0 ], my_array[ 1 ], my_array[ 2 ], normal_short ); } Output: 12 37 6 12 In this example we declare a array named my_array that contain 3 short and assign a value to every short in this array. A array can be of any type except void and they alway begin at index 0. There is a really usefull one which is a char array, it is used to store text. #include void main ( void ) { char my_name[ 21 ]; printf( "Enter your name: " ); scanf( "%s", my_text ); printf( "\nHello %s\n", my_name ); } Output: Enter your name: ( then wait for your name followed by enter ) Hello ( the name you wrote on the previous line ) The last example use a 21 characters array to store your name, maximum of 20 characters. This is because all string are null terminated ( there is a null character after the string to show the end of the string ) so your array can store any string from 0 to 20 characters. The %s option in printf and scanf is used for strings. +----------------------+ | Characters functions | +----------------------+ Tere are some string related fonctions you should also know: getch() -> get one character from the keyboard without echo on the screen getche() -> get one character from the keyboard with echo on the screen putch( character ) -> put une character at the current cursor location Another usefull function which is usefull in DOS programs is gotoxy( col, line ) which change the cursor location. All those functions are in conio.h so you need to add #include at the top of your program when you use them. #include void main ( void ) { char one_char; char string[ 5 ]; // 4 character string from 0 to 3 // string[ 4 ] is used for the null character one_char = getch(); // Read a character into one_char string[ 0 ] = getch(); // Read one character the first three array cells string[ 1 ] = getch(); string[ 2 ] = getch(); string[ 3 ] = getch(); string[ 4 ] = 0; // Make the string null terminated // This is really important or any fonction asking for a string will continue // to read the memory until they hit a null character ( 0 ). gotoxy( 10, 30 ); // Line 10, Column 30 putch( one_char ); // read a character in one_char putch( 'h' ); // write a character h printf( "%s", string ); // print the string to the screen putch( string[ 1 ] ); // write the second character of string on the screen } Single characters are between '' when string between "". This is why you write putch( 'h' ); and not putch( "h" );. +------------+ | IF KEYWORD | +------------+ The "if" keyword is used to make some part of the code conditional. The syntax is: if ( condition ) statement [ else statement ] condition is anything that can be true or false. statement is one instruction or block of instruction Examples of instructions: x = 2; x = y + 3; x = y; printf( "Will be print" ); -> which I use to tell what will be printed gotoxy( x, y ); putch( 'b' ); ... can be anything, even a if keyword Comparaison operators are <, <=, ==, !=, >=, > ( in comparaison be carefull to use == and not =, because single equal is the assign operator. ) Be carefull to put ; at the end of the statement ( not bloc ) when there is only one. There is a big if keyword example: #include void main ( void ) { short x = 2; short y = 5; if ( x < 3 ) printf( "Will be print" ); if ( x == 2 ) printf( "Will be print" ); if ( x >= 6 ) printf( "Won't be print" ); if ( y < x ) printf( "Won't be print" ); if ( x == y - 3 ) printf( "Will be print" ); // 0 is false, other values are true so: if ( 0 ) printf( "Won't be print" ); if ( 1 ) printf( "Will be print" ); if ( x ) printf( "Will be print\n" ); // Because x is true zero is false, non zero is true if ( x = 7 ) printf( "Will be print" ); // Because I used x = 7 ( not == ) now x = 7 and 7 is non 0 so x is true // This will always be print except if it was x = 0 ... that would never // be print if ( x == 2 ) printf( "Won't be print" ); else printf( "Will be print" ); // Remember x now equal 7 if ( x != 5 ) { // Will both be printed printf( "Will be print" ); // This is what we call a bloc printf( "x is not equal to 5" ); // and it contains 2 statements } if ( x * x < 20 ) { // But it can contain only one printf( "Won't be print\n" ); } else { printf( "Will be print" ); } if ( x == 7 && y == 2 ) printf( "Will be print" ); // && means AND if ( x == 3 || y == 2 ) printf( "Will be print" ); // || means OR if ( x == 3 ) { printf( "Won't be print\n" ); } else if ( y == 2 ) { printf( "Will be print\n" ); } // if is also a statement, so it's legal to put a single if in a else clause // that's equivalent to: if ( x == 3 ) printf( "Won't be print\n" ); else if ( y == 2 ) printf( "Will be print\n" ); // or if ( x == 3 ) printf( "Won't be print\n" ); else { if ( y == 2 ) printf( "Will be print\n" ); } // or if ( x == 3 ) { printf( "Won't be print\n" ); } else { if ( y == 2 ) printf( "Will be print\n" ); } // or if ( x == 3 ) { printf( "Won't be print\n" ); } else { if ( y == 2 ) { printf( "Will be print\n" ); } } // but the first one is more easy to read. As you can see there are many ways // to write the same thing } Output: The code says it all ... I think you can figure it out alone.