Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C++ Programming Code Examples

C++ > Games Code Examples

Find the word a Game

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/* Find the word a Game */ #include<stdio.h> #include<conio.h> char arr[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZ "}; int pos1[20]; int pos2[20]; void main() { int i,no,j,z; textattr(1<<4|14); clrscr(); cprintf("Asume A Word!\n\n\rStep 1:\n\r=======\n\rTabel 1:\n\n\r"); textattr(1<<4|12); for(i=0;i<5;i++) cprintf("%d ",i+1); textattr(7<<4); for(i=0;i<30;i++) { if(i%5==0) printf("\n"); cprintf("%c ",arr[i]); } textattr(1<<4|14); gotoxy(1,15); cprintf("\n\n\rEnter Total Number of letters in word:"); cscanf("%d",&no); cprintf(" "); for(i=0;i<no;i++) { gotoxy(1,15); cprintf("\n\n\rEnter Letter %d Coloumn number:",i+1); scanf("%d",&pos1[i]); pos1[i]-=1; } clrscr(); cprintf("Step 2:\n\rTabel 2:\n\r"); textattr(1<<4|12); for(i=0;i<6;i++) cprintf("%d ",i+1); textattr(7<<4); for(i=0;i<no;i++) { printf("\n"); for(j=pos1[i];j<30;j+=5) cprintf("%c ",arr[j]); } textattr(1<<4|14); for(i=0;i<no;i++) { gotoxy(1,15); cprintf("\n\n\rEnter the %d Letter's Coloumn number:",i+1); scanf("%d",&pos2[i]); pos2[i]-=1; } clrscr(); gotoxy(30,13); cprintf("We Got U! "); gotoxy(30,15); cprintf("SURPRISED!"); textcolor(WHITE+BLINK); gotoxy(30,14); for(i=0;i<no;i++) { cprintf("%c",arr[pos2[i]*5+pos1[i]]); } textcolor(YELLOW+BLINK); cprintf("\n\n\n\n\n\n\n\n\rPress Esc To Exit..."); while(getch()!=27); }

In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.

In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.

It is a predefined function in "conio.h" (console input output header file) used to clear the console screen. It is a predefined function, by using this function we can clear the data from console (Monitor). Using of clrscr() is always optional but it should be place after variable or function declaration only. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.

Use the textcolor function to define what color you want to use for text. You can use this function to vary the text colors of your output. Colors must be written in all caps, or expressed as a numeral. Now, if you want your text to blink then while calling the textcolor() function pass the color and also say BLINK. This will like this: textcolor(BLUE+BLINK).

C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside another loop. Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting level can be defined at n times. You can define any type of loop inside another loop; for example, you can define 'while' loop inside a 'for' loop. A loop inside another loop is called a nested loop. The depth of nested loop depends on the complexity of a problem. We can have any number of nested loops as required. Consider a nested loop where the outer loop runs n times and consists of another loop inside it. The inner loop runs m times. Then, the total number of times the inner loop runs during the program execution is n*m.

As the name already suggests, these operators help in assigning values to variables. These operators help us in allocating a particular value to the operands. The main simple assignment operator is '='. We have to be sure that both the left and right sides of the operator must have the same data type. We have different levels of operators. Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language:

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.

In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

Positions cursor in text window. The gotoxy() function places the cursor at the desired location on the screen. This means it is possible to change the cursor location on the screen using the gotoxy() function. It is basically used to print text wherever the cursor is moved. If the coordinates are in any way invalid the call to gotoxy is ignored. Neither argument to gotoxy can be zero.


"Array index" starts with 0, which means the first array element is at index 0, second is at index 1 and so on. We use this information to display the array elements. See the C++ code








To find 'cube root of any number' we need to find 0.3 power of any number. if you need to find cube root of 27 then calculate 0.3 power of 27, result is 3. So another method for this

Read the comments in the following program to understand each part of the program. This is a "default constructor" of the class, you do note that it's name is same as class name and