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++ > Computer Graphics Code Examples

Animated 3D Car in Turbo C++ 3.0

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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
/* Animated 3D Car in Turbo C++ 3.0 It consist of Lights effect, Sound Effect. Press 'L' for Lights effect and 'H' for Horn. */ #include<graphics.h> #include<conio.h> #include<dos.h> #include<stdlib.h> #include<process.h> void main() { int gd=DETECT,gm; initgraph(&gd,&gm,"c:\tc\bgi"); int c=12; setbkcolor(0); //setlinestyle(0,1,2); int t; while(1) { settextstyle(2,0,5); outtextxy(100,10,"Press L,H ,T,P"); outtextxy(100,30,"Press 1 for Quit"); as: setcolor(13); ellipse(380,127,20,152,130,35); //////////////////////////////rear////////////////////////// line(490,109,560,142); line(560,142,569,142); line(569,142,582,102); line(582,102,620,92); line(593,132,617,125); line(617,124,627,96); line(620,92,628,97); line(472,86,602,96); line(501,113,575,121); line(443,77,475,80); line(443,77,432,93); line(475,80,472,85); //setcolor(4); line(593,132,593,137); line(593,137,600,141); line(600,141,600,185); line(600,185,608,192); line(608,192,608,234); line(608,234,586,253); line(586,253,577,248); ///////////////////////// mirror line(263,112,363,127); line(193,160,263,112); line(193,160,220,170); line(220,170,280,180); line(280,180,320,185); line(320,185,363,127); ////////////////////////////////sidemirror line(340,194,460,169); line(460,169,519,152); ellipse(512,144,300,30,10,10); ellipse(467,143,28,100,50,30); line(510,128,521,138); line(435,116,440,171); // setcolor(4); ////////////////////////////////////////cont// line(339,194,372,144); // line(372,140,386,128); ellipse(454,208,87,123,128,95); line(372,144,384,128); int b,x,y; ////////////////////////lower line(365,298,524,264); line(365,298,330,310); line(330,310,323,310); ///////////////////////////////bumper ellipse(162,221,135,190,90,40); line(96,193,140,174); line(140,174,160,168); line(160,168,192,161); //////////////////////front ellipse(75,246,95,190,18,18); line(57,251,57,286); //setcolor(4); ellipse(181,178,232,263,200,137); ellipse(195,180,256,286,200,137); ellipse(191,171,228,247,200,100); ellipse(231,198,234,275,200,80); //setcolor(9); //ellipse(195,170,256,286,200,137); //setcolor(12); ellipse(196,167,228,246,200,90); ellipse(231,184,234,276,200,80); ellipse(191,200,228,246,200,90); ellipse(228,218,234,276,200,80); ellipse(258,268,180,220,200,40); ellipse(178,296,244,355,16,10); ellipse(238,249,227,250,200,60); /////////////wheel1 ellipse(302,281,320,77,26,45); ellipse(290,277,65,162,40,45); ellipse(278,288,144,212,31,45); /////////////wheel2 //setcolor(5); ellipse(302+260,229,328,87,26,45); ellipse(290+280-7,277-50+2,90,162,40,45); ellipse(278+270,288-50,144,215,27,45); b=0; int v=0; ///////// ellipse(302+250+v,227+b,295,90,29,41); ellipse(302+234+v,231+b,245,306,50,40); //setlinestyle(3,0,3); ellipse(302+248+v,229+b,0,360,21,30); ellipse(302+247+v,229+b,0,360,8,10); setfillstyle(6,11); //floodfill(302+248+v,230+b,13); //line(546,201,546,257); //line(554,201,554,257); //setcolor(4); line(546+v,201+b,546+v,220+b); line(551+v,201+b-2,551+v,220+b); line(546+v,238+b,546+v,257+b); line(551+v,238+b+2,551+v,257+b+2); line(530+v,225+b,541+v,225+b); line(530+v,230+b,541+v,230); line(557+v,225+b,570+v,225+b); line(557+v,230+b,570+v,230+b); line(563+v,206+b,552+v,222+b); line(534+v,246+b,543+v,232+b); line(566+v,210+b,556+v,223+b); line(536+v,250+b,544+v,238+b); line(536+v,207+b,546+v,222+b); line(532+v,213+b,542+v,224+b); line(556+v,235+b,566+v,247+b); line(551+v,237+b,563+v,253+b); //////////////////////////////////////////////////// v=-260; b=56; ellipse(302+233+v,221+b,260,60,49,51); //ellipse(302+234+v,231+b,245,306,50,40); //setlinestyle(3,0,3); ellipse(302+243+v,224+b,0,360,28,35); // line(249,328,269,328); ellipse(300+245+v,223+b,0,360,10,12); ellipse(285+249+v,239+b,210,260,30,33); //floodfill(285+258+v,230+b,12); b=45; v=v-4; line(546+v,201+b,546+v,220+b+2); line(551+v,201+b,551+v,220+b+2); b=b+8; line(546+v,238+b,546+v,257+b+4); line(551+v,238+b,551+v,257+b+4); v=v-2; line(530+v-6,225+b,541+v,225+b); line(530+v-6,230+b,541+v,230+b); v=v+5; line(557+v,225+b,570+v+3,225+b); line(557+v-1,230+b,570+v+3,230+b); b=b-5; v=v-5; line(565+v+3,206+b,552+v+4,222+b-2); b=b+15; line(534+v,246+b,543+v+3,232+b-5); b=b-10; line(566+v+7,210+b-5,556+v+4,220+b); line(536+v-5,250+b,544+v-2,238+b-4); line(536+v,207+b-8,545+v,222+b-5); line(531+v,212+b-8,542+v,224+b-2); line(556+v,235+b,566+v+3,247+b+5); line(551+v,237+b,563+v+2,253+b+3); ///////////////////lights ellipse(199,250,144,345,18,8); line(185,245,206,230); //setcolor(4); ellipse(223,234,340,110,8,5); line(230,237,217,252); line(206,230,220,229); //setfillstyle(1,4); //floodfill(200,240,12); ///////////////////////////////////// line(90,223,152,236); line(152,236,137,254); line(90,223,90,242); //setfillstyle(10,9); //floodfill(91,230,14); ellipse(240,270,104,136,100,60); ellipse(185,237,120,160,100,60); ellipse(80,221,357,134,10,10); line(152,236,168,228); /////////////////////////////////////////////// line(435,116,440,171); //////////////////////////////////////////hp //line(134,185,220,210); line(134,185,196,160); line(214,212,318,185); /////////////////////////////////////////////////light //setcolor(14); ellipse(166,247,99,330,8,8); ellipse(171,243,310,129,7,7); putpixel(174,250,13); putpixel(173,251,13); putpixel(164,239,13); putpixel(165,238,13); /////////////////////////////////////////road///////////////////// setcolor(13); line(1,430,639,300); line(1,445,639,315); line(1,210,93,194); line(1,195,194,158); //line(1,170,639,71); //line(1,170,229,135); line(520,90,639,71); line(478,86,639,56); int c=0; line(10,194+c,10,208+c); line(40,189+c,40,204+c); line(70,183+c,70,198+c); line(100,176+c,100,190+c); line(130,170+c,130,177+c); line(160,166+c,160,168+c); line(190,160+c,190,161+c); line(190+330,78+c,190+330,89+c); line(190+360,72+c,190+360,85+c); line(190+390,67+c,190+390,81+c); line(190+420,62+c,190+420,76+c); line(190+449,57+c,190+449,71+c); c=236; line(10,192+c,10,208+c); line(40,189+c-2,40,204+c-3); line(70,183+c-3,70,198+c-3); line(100,176+c-2,100,190+c-2); line(130,170+c-2,130,177+c+5); line(160,166+c-3,160,168+c+8); line(190,160+c-4,190,161+c+9); line(190+30,156+c-5,190+30,170+c-5); line(190+30+30,156+c-12,190+30+30,170+c-12); line(190+90,156+c-18,190+90,170+c-17); line(190+120,156+c-25,190+120,170+c-25); line(190+150,156+c-30,190+150,170+c-30); line(190+180,156+c-37,190+180,170+c-36); line(190+210,156+c-42,190+210,170+c-42); line(190+240,156+c-48,190+240,170+c-48); line(190+270,156+c-55,190+270,170+c-54); line(190+300,156+c-61,190+300,170+c-61); line(190+330,78+c+10,190+330,89+c+13); line(190+360,72+c+11,190+360,85+c+13); line(190+390,67+c+10,190+390,81+c+10); line(190+420,62+c+8,190+420,76+c+10); line(190+449,57+c+8,190+449,71+c+8); /////////////////road setcolor(12); /////////////////////////////1 line(1,310,25,306); line(6,318,30,315); line(1,310,6,318); line(25,306,30,314); int k,m; k=13*45+19; m=16*(-8); //2 setcolor(12); line(605,310-128,629,306-128); line(610,318-128,634,315-128); line(605,310-128,610,318-128); line(629,306-128,634,314-128); setcolor(12); //////////////////////////////////3 k=45; m=-8; line(46,302,70,298); line(51,310,75,307); line(46,302,51,310); line(70,298,75,306); setfillstyle(1,0); floodfill(64,303,12); setfillstyle(1,14); floodfill(14,314,12); floodfill(617,183,12); setfillstyle(1,0); floodfill(14,314,12); floodfill(617,183,12); setfillstyle(1,14); floodfill(64,303,12); t=getch(); if(t=='1') exit(0); if(t=='h') { sound(710); delay(500); nosound(); //break; } if(t=='t') { while(!kbhit()) { setfillstyle(1,0); floodfill(536,213,13); floodfill(563,213,13); floodfill(561,244,13); floodfill(538,244,13); floodfill(274,295,13); floodfill(294,295,13); floodfill(274,265,13); floodfill(294,265,13); floodfill(548,250,13); floodfill(548,214,13); floodfill(533,228,13); floodfill(563,228,13); floodfill(262,281,13); floodfill(308,281,13); floodfill(284,251,13); floodfill(284,295,13); setfillstyle(1,random(12)); floodfill(200,250,13); delay(10); //setfillstyle(1,11); floodfill(170,250,13); floodfill(80,230,13); } setfillstyle(1,0); floodfill(200,250,13); delay(10); //setfillstyle(1,11); floodfill(170,250,13); floodfill(80,230,13); } if(t=='l') { while(!kbhit()) { delay(120); setfillstyle(6,0); //////////////////////////ty floodfill(536,213,13); floodfill(563,213,13); floodfill(561,244,13); floodfill(538,244,13); floodfill(274,295,13); floodfill(294,295,13); floodfill(274,265,13); floodfill(294,265,13); setfillstyle(1,0); floodfill(64,303,12); ///////////////////////////////////road setfillstyle(9,0); /////////////////////color floodfill(81-40+5,419+7,13); floodfill(151-40,409+7,13); floodfill(211-40,397+7,13); floodfill(271-40,380+7,13); floodfill(331-40,368+7,13); floodfill(396-40,355+7,13); floodfill(450-40,345+7,13); floodfill(510-40,335+7,13); floodfill(570-40,325+7,13); floodfill(630-40,312+7,13); ////////////////////// floodfill(50,197,13); floodfill(110,177,13); floodfill(166,165,13); floodfill(527,86,13); floodfill(587,71,13); setfillstyle(6,14); //////////////////////////ty floodfill(548,250,13); floodfill(548,214,13); floodfill(533,228,13); floodfill(563,228,13); floodfill(262,281,13); floodfill(308,281,13); floodfill(284,251,13); floodfill(284,295,13); ////////////////////////////////////////road setfillstyle(9,10);///////////////////////////////////color floodfill(19,429,13); floodfill(81,419,13); floodfill(151,409,13); floodfill(211,397,13); floodfill(271,380,13); floodfill(331,368,13); floodfill(396,355,13); floodfill(450,345,13); floodfill(510,335,13); floodfill(570,325,13); floodfill(630,312,13); ////////////////////////////////////// floodfill(20,197,13); floodfill(80,187,13); floodfill(133,174,13); floodfill(517,86,13); floodfill(557,81,13); floodfill(627,70,13); setfillstyle(1,14); floodfill(14,314,12); floodfill(617,183,12); /////////////////////////////////////// setfillstyle(10,4); floodfill(302+248,230,13); floodfill(302+248+v,230+b,13); ///light setfillstyle(6,11); /////////// floodfill(200,250,13); floodfill(170,250,13); floodfill(80,230,13); delay(120); setfillstyle(6,0);/////////////////////ty floodfill(548,250,13); floodfill(548,214,13); floodfill(533,228,13); floodfill(563,228,13); floodfill(262,281,13); floodfill(308,281,13); floodfill(284,251,13); floodfill(284,295,13); /////////////////////////////////////road setfillstyle(9,0); ///////////////color floodfill(19,429,13); floodfill(81,419,13); floodfill(151,409,13); floodfill(211,397,13); floodfill(271,380,13); floodfill(331,368,13); floodfill(396,355,13); floodfill(450,345,13); floodfill(510,335,13); floodfill(570,325,13); floodfill(630,312,13); /////////////////////////////////////////////////////// floodfill(20,197,13); floodfill(80,187,13); floodfill(133,174,13); floodfill(517,86,13); floodfill(557,81,13); floodfill(627,70,13); ///////////////////////////// setfillstyle(1,0); floodfill(14,314,12); floodfill(617,183,12); setfillstyle(6,10); /////////////ty floodfill(536,213,13); floodfill(563,213,13); floodfill(561,244,13); floodfill(538,244,13); floodfill(274,295,13); floodfill(294,295,13); floodfill(274,265,13); floodfill(294,265,13); ////////////////////////////////////////////////road setfillstyle(9,14);/////////////////////////////////////////color floodfill(81-40+5,419+7,13); floodfill(151-40,409+7,13); floodfill(211-40,397+7,13); floodfill(271-40,380+7,13); floodfill(331-40,368+7,13); floodfill(396-40,355+7,13); floodfill(450-40,345+7,13); floodfill(510-40,335+7,13); floodfill(570-40,325+7,13); floodfill(630-40,312+7,13); ///////////////////////////////////////// floodfill(50,197,13); floodfill(110,177,13); floodfill(166,165,13); floodfill(527,86,13); floodfill(587,71,13); setfillstyle(1,14); floodfill(64,303,12); setfillstyle(9,4); floodfill(302+248,230,13); floodfill(302+248+v,230+b,13); delay(20); setfillstyle(1,14); floodfill(200,250,13); floodfill(170,250,13); floodfill(80,230,13); delay(20); setfillstyle(1,0); floodfill(200,250,13); floodfill(170,250,13); floodfill(80,230,13); } } if(t=='p') { int n=0; while(!kbhit()) { if(n<=60) n++; setcolor(0); rectangle(1+1,-10,90-1,-12+n); delay(14); setcolor(9); rectangle(1,-10,90,-10+n); if(n==60) { outtextxy(10,10,"L-LIGHTS"); outtextxy(10,20,"H-HORN"); //outtextxy(10,30,"T-AllOY"); delay(400); } } setcolor(0); rectangle(1,-10,90,-10+n); rectangle(1,-10,90,-11+n); outtextxy(10,10,"L-LIGHTS"); outtextxy(10,20,"H-HORN"); //outtextxy(10,30,"T-AllOY"); } } circle(300,100,3); nosound(); getch(); }
If Else Statement in C++
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,
Syntax for If Statement in C++
if (condition) { // body of if 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.
Syntax for If...Else Statement
if (condition) { // block of code if condition is true } else { // block of code if condition is false }
The if..else statement evaluates the condition inside the parenthesis. If the condition evaluates true, the code inside the body of if is executed, the code inside the body of else is skipped from execution. If the condition evaluates false, the code inside the body of else is executed, the code inside the body of if is skipped from execution. The if...else statement is used to execute a block of code among two alternatives. However, if we need to make a choice between more than two alternatives, we use the if...else if...else statement.
Syntax for If...Else...Else If Statement in C++
if (condition1) { // code block 1 } else if (condition2){ // code block 2 } else { // code block 3 }
• If condition1 evaluates to true, the code block 1 is executed. • If condition1 evaluates to false, then condition2 is evaluated. • If condition2 is true, the code block 2 is executed. • If condition2 is false, the code block 3 is executed. There can be more than one else if statement but only one if and else statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
Syntax for If Else If Ladder in C++
if (condition) statement 1; else if (condition) statement 2; . . else statement;
Working of the if-else-if ladder: 1. Control falls into the if block. 2. The flow jumps to Condition 1. 3. Condition is tested. If Condition yields true, goto Step 4. If Condition yields false, goto Step 5. 4. The present block is executed. Goto Step 7. 5. The flow jumps to Condition 2. If Condition yields true, goto step 4. If Condition yields false, goto Step 6. 6. The flow jumps to Condition 3. If Condition yields true, goto step 4. If Condition yields false, execute else block. Goto Step 7. 7. Exits the if-else-if ladder. • The if else ladder statement in C++ programming language is used to check set of conditions in sequence. • This is useful when we want to selectively executes one code block(out of many) based on certain conditions. • It allows us to check for multiple condition expressions and execute different code blocks for more than two conditions. • A condition expression is tested only when all previous if conditions in if-else ladder is false. • If any of the conditional expression evaluates to true, then it will execute the corresponding code block and exits whole if-else ladder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* If Else Statement in C++ Language */ #include <iostream> using namespace std; int main () { // local variable declaration: int a = 100; // check the boolean condition if( a < 20 ) { // if condition is true then print the following cout << "a is less than 20;" << endl; } else { // if condition is false then print the following cout << "a is not less than 20;" << endl; } cout << "value of a is : " << a << endl; return 0; }
#include Directive in C++
#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.
Syntax for #include Directive in C++
#include "user-defined_file"
Including using " ": When using the double quotes(" "), the preprocessor access the current directory in which the source "header_file" is located. This type is mainly used to access any header files of the user's program or user-defined files.
#include <header_file>
Including using <>: While importing file using angular brackets(<>), the the preprocessor uses a predetermined directory path to access the file. It is mainly used to access system header files located in the standard system directories. Header File or Standard files: This is a file which contains C/C++ function declarations and macro definitions to be shared between several source files. Functions like the printf(), scanf(), cout, cin and various other input-output or other standard functions are contained within different header files. So to utilise those functions, the users need to import a few header files which define the required functions. User-defined files: These files resembles the header files, except for the fact that they are written and defined by the user itself. This saves the user from writing a particular function multiple times. Once a user-defined file is written, it can be imported anywhere in the program using the #include preprocessor. • In #include directive, comments are not recognized. So in case of #include <a//b>, a//b is treated as filename. • In #include directive, backslash is considered as normal text not escape sequence. So in case of #include <a\nb>, a\nb is treated as filename. • You can use only comment after filename otherwise it will give error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* using #include directive in C language */ #include <stdio.h> int main() { /* * C standard library printf function * defined in the stdio.h header file */ printf("I love you Clementine"); printf("I love you so much"); printf("HappyCodings"); return 0; }
rectangle() Function in C++
rectangle() is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw the rectangle. left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner.
Syntax for rectangle() Function in C++
rectangle(int left, int top, int right, int bottom);
left
X coordinate of top left corner.
top
Y coordinate of top left corner.
right
X coordinate of bottom right corner.
bottom
Y coordinate of bottom right corner. To create a rectangle, you have to pass the four parameters in this function. The two parameters represent the left and top upper left corner. Similarly, the right bottom parameter represents the lower right corner of the rectangle. This function does not return any value.
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
/* function rectangle() draws a rectangle in graphic mode. */ int main() { // location of left, top, right, bottom int left = 150, top = 150; int right = 450, bottom = 450; // initgraph initializes the graphics system // by loading a graphics driver from disk initgraph(&gd, &gm, ""); // rectangle function rectangle(left, top, right, bottom); left = 200, = 250; right = 150, = 300; rectangle(left, top, right, bottom); left = 100, = 200; right = 450, = 100; rectangle(left, top, right, bottom); getch(); return 0; }
main() Function in C++
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.
Syntax for main() Function in C++
void main() { ............ ............ }
void
void is a keyword in C++ language, void means nothing, whenever we use void as a function return type then that function nothing return. here main() function no return any value.
main
main is a name of function which is predefined function in C++ library. In place of void we can also use int return type of main() function, at that time main() return integer type value. 1) It cannot be used anywhere in the program a) in particular, it cannot be called recursively b) its address cannot be taken 2) It cannot be predefined and cannot be overloaded: effectively, the name main in the global namespace is reserved for functions (although it can be used to name classes, namespaces, enumerations, and any entity in a non-global namespace, except that a function called "main" cannot be declared with C language linkage in any namespace). 3) It cannot be defined as deleted or (since C++11) declared with C language linkage, constexpr (since C++11), consteval (since C++20), inline, or static. 4) The body of the main function does not need to contain the return statement: if control reaches the end of main without encountering a return statement, the effect is that of executing return 0;. 5) Execution of the return (or the implicit return upon reaching the end of main) is equivalent to first leaving the function normally (which destroys the objects with automatic storage duration) and then calling std::exit with the same argument as the argument of the return. (std::exit then destroys static objects and terminates the program). 6) (since C++14) The return type of the main function cannot be deduced (auto main() {... is not allowed). 7) (since C++20) The main function cannot be a coroutine.
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
/* simple code example by main() function in C++ */ #include <iostream> using namespace std; int main() { int day = 4; switch (day) { case 1: cout << "Monday"; break; case 2: cout << "Tuesday"; break; case 3: cout << "Wednesday"; break; case 4: cout << "Thursday"; break; case 5: cout << "Friday"; break; case 6: cout << "Saturday"; break; case 7: cout << "Sunday"; break; } return 0; }
setfillstyle() Function in C++
The header file graphics.h contains setfillstyle() function which sets the current fill pattern and fill color. Current fill pattern and fill color is used to fill the area. setfillstyle sets the current fill pattern and fill color. To set a user-defined fill pattern, do not give a pattern of 12 (USER_FILL) to setfillstyle; instead, call setfillpattern.
Syntax for setfillstyle() Function in C++
#include<graphics.h> void setfillstyle(int pattern, int color);
color
Specify the color • BLACK – 0 • BLUE – 1 • GREEN – 2 • CYAN – 3 • RED – 4 • MAGENTA – 5 • BROWN – 6 • LIGHTGRAY – 7 • DARKGRAY – 8 • LIGHTBLUE – 9 • LIGHTGREEN – 10 • LIGHTCYAN – 11 • LIGHTRED – 12 • LIGHTMAGENTA – 13 • YELLOW – 14 • WHITE – 15
pattern
Specify the pattern • EMPTY_FILL – 0 • SOLID_FILL – 1 • LINE_FILL – 2 • LTSLASH_FILL – 3 • SLASH_FILL – 4 • BKSLASH_FILL – 5 • LTBKSLASH_FILL – 6 • HATCH_FILL – 7 • XHATCH_FILL – 8 • INTERLEAVE_FILL – 9 • WIDE_DOT_FILL – 10 • CLOSE_DOT_FILL – 11 • USER_FILL – 12 If invalid input is passed to setfillstyle, graphresult returns -1(grError), and the current fill pattern and fill color remain unchanged. The EMPTY_FILL style is like a solid fill using the current background color (which is set by setbkcolor). This function does not return any value.
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
/* The header file graphics.h contains setfillstyle() function which sets the current fill pattern and fill color. floodfill() function is used to fill an enclosed area. Current fill pattern and fill color is used to fill the area. */ #include <graphics.h> // driver code int main() { // gm is Graphics mode which is // a computer display mode that // generates image using pixels. // DETECT is a macro defined in // "graphics.h" header file int gd = DETECT, gm; // initgraph initializes the // graphics system by loading // a graphics driver from disk initgraph(&gd, &gm, " "); // center and radius of circle int x_circle = 250; int y_circle = 250; int radius=100; // setting border color int border_color = WHITE; // set color and pattern setfillstyle(HATCH_FILL,RED); // x and y is a position and // radius is for radius of circle circle(x_circle,y_circle,radius); // fill the color at location // (x, y) with in border color floodfill(x_circle,y_circle,border_color); getch(); // closegraph function closes the // graphics mode and deallocates // all memory allocated by // graphics system closegraph(); return 0; }
settextstyle() Function in C++
Settextstyle function is used to change the way in which text appears, using it we can modify the size of text, change direction of text and change the font of text. settextstyle sets the text font, the direction in which text is displayed, and the size of the characters. A call to settextstyle affects all text output by outtext and outtextxy.
Syntax for settextstyle() Function in C++
#include <graphics.h> void settextstyle(int font, int direction, int charsize);
font
One 8x8 bit-mapped font and several "stroked" fonts are available. The 8x8 bit-mapped font is the default. The enumeration font_names, which is defined in graphics.h, provides names for these different font settings: • DEFAULT_FONT – 0 8x8 bit-mapped font • TRIPLEX_FONT – 1 Stroked triplex font • SMALL_FONT – 2 Stroked small font • SANS_SERIF_FONT – 3 Stroked sans-serif font • GOTHIC_FONT – 4 Stroked gothic font • SCRIPT_FONT – 5 Stroked script font • SIMPLEX_FONT – 6 Stroked triplex script font • TRIPLEX_SCR_FONT – 7 Stroked triplex script font • COMPLEX_FONT – 8 Stroked complex font • EUROPEAN_FONT – 9 Stroked European font • BOLD_FONT – 10 Stroked bold font The default bit-mapped font is built into the graphics system. Stroked fonts are stored in *.CHR disk files, and only one at a time is kept in memory. Therefore, when you select a stroked font (different from the last selected stroked font), the corresponding *.CHR file must be loaded from disk. To avoid this loading when several stroked fonts are used, you can link font files into your program. Do this by converting them into object files with the BGIOBJ utility, then registering them through registerbgifont.
direction
Font directions supported are horizontal text (left to right) and vertical text (rotated 90 degrees counterclockwise). The default direction is HORIZ_DIR. The size of each character can be magnified using the charsize factor. If charsize is nonzero, it can affect bit-mapped or stroked characters. A charsize value of 0 can be used only with stroked fonts.
charsize
• If charsize equals 1, outtext and outtextxy displays characters from the 8x8 bit-mapped font in an 8x8 pixel rectangle onscreen. • If charsize equals 2, these output functions display characters from the 8x8 bit-mapped font in a 16*16 pixel rectangle, and so on (up to a limit of ten times the normal size). • When charsize equals 0, the output functions outtext and outtextxy magnify the stroked font text using either the default character magnification factor (4) or the user-defined character size given by setusercharsize. Always use textheight and textwidth to determine the actual dimensions of the text. This function needs to be called before the outtextxy() function, otherwise there will be no effect on text and output will be the same.
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
/* settextstyle() function sets the current text font, direction and character size. All calls to outtext() and outtextxy() are affected by the new settings. */ int main() { int gm, gd; gd = VGA; gm = VGAHI; initgraph(&gd, &gm, ""); settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 4); outtextxy(32, 8, "SANS_SERIF_FONT"); settextstyle(DEFAULT_FONT, HORIZ_DIR, 4); outtextxy(32, 58, "DEFAULT_FONT"); settextstyle(GOTHIC_FONT, HORIZ_DIR, 4); outtextxy(32, 108, "GOTHIC_FONT"); settextstyle(SCRIPT_FONT, HORIZ_DIR, 4); outtextxy(32, 158, "SCRIPT_FONT"); getch(); closegraph(); }
initgraph() Function in C++
To create a program in Graphics Mode, the first step would be to include the header file graphics.h. This file is required for Graphics programming. After this, the graphics have to be initialized. C Language supports 16 Bit's MS-DOS environment. Initializing the Graphics mode is to call various functions, one such is called initgraph. initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver), and putting the system into graphics mode. To start the graphics system, first call the initgraph function. initgraph loads the graphics driver and puts the system into graphics mode. You can tell initgraph to use a particular graphics driver and mode, or to autodetect the attached video adapter at run time and pick the corresponding driver. If you tell initgraph to autodetect, it calls detectgraph to select a graphics driver and mode. initgraph also resets all graphics settings to their defaults (current position, palette, color, viewport, and so on) and resets graphresult to 0.
Syntax for initgraph() Function in C++
void initgraph (int *graphdriver, int *graphmode, char *pathtodriver);
graphdriver
This is an integer that indicates that the graphics driver has been used.
graphmode
It is also an integer value that detects the available graphics driver and initializes the graphics mode according to its highest resolution.
pathtodriver
This is the path of the directory that first searches the initgraph function graphics driver. If the graphics driver is not available then the system searches it in the current directory. It is necessary to pass the correct value of the three parameters in the initgraph function or else an unpredictable output is obtained.
intgd = DETECT, gm; initgraph (&gd, &gm, " ");
To initialize Graphics mode, you only have to write two lines. Here, we have taken two integer variables 'd' and 'm'. Here, DETECT is an enumeration type that identifies and identifies the proper graphics driver. The initgraph function has to pass the address of both the variables. You can see in the example that we have given a space at the position of the third variable. This means that if you do not know the driver's path then you can leave it blank. The compiler will auto-detect the path. initgraph always sets the internal error code; on success, it sets the code to 0. If an error occurred, *graphdriver is set to -2, -3, -4, or -5, and graphresult returns the same value as listed below: • grNotDetected -2 Cannot detect a graphics card • grFileNotFound -3 Cannot find driver file • grInvalidDriver -4 Invalid driver • grNoLoadMem -5 Insufficient memory to load driver
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
/* initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver), and putting the system into graphics mode. To start the graphics system, first call the initgraph function. initgraph loads the graphics driver and puts the system into graphics mode. You can tell initgraph to use a particular graphics driver and mode, or to autodetect the attached video adapter at run time and pick the corresponding driver. */ int DGraphics::Init( int gmode ) { int gdriver = VGA, errorcode; gdriver=installuserdriver("SVGA256",NULL); initgraph(&gdriver, &gmode, ""); if ( (errorcode = graphresult()) != grOk ) { cout << "Error: Graphics - %s\n" << grapherrormsg(errorcode); return FALSE; } ActiveMode=gmode; return TRUE; }
setbkcolor() Function in C++
setbkcolor() function is used to set the background color in graphics mode. The default background color is black and default drawing color as we know is white. setbkcolor() function takes only one argument it would be either the name of color defined in graphics.h header file or number associated with those colors. If we write setbkcolor(yellow) it changes the background color in Green. The possible color values are from 0 - 15 black, blue, green, cyan, red, magenta, brown, lightgray, darkgray, lightblue, lightgreen, lightcyan, lightred, lightmagenta, yellow, white and blink (128).
Syntax for setbkcolor() Function in C++
#include<graphics> void setbkcolor(int color);
color
specify the color setbkcolor sets the background to the color specified by color. The argument color can be a name or a number as listed below. (These symbolic names are defined in graphics.h.) This function does not return any value. INT VALUES corresponding to Colors: • BLACK 0 • BLUE 1 • GREEN 2 • CYAN 3 • RED 4 • MAGENTA 5 • BROWN 6 • LIGHTGRAY 7 • DARKGRAY 8 • LIGHTBLUE 9 • LIGHTGREEN 10 • LIGHTCYAN 11 • LIGHTRED 12 • LIGHTMAGENTA 13 • YELLOW 14 • WHITE 15
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
/* change the background to the color specified by color in graphics mode. */ /* Program to make digital clock in C++ graphics */ #include<graphics> #include<conio.h> #include<time.h> int main() { initwindow(700,500,"CLOCK",300,100);//displays graphics window char t[15],date[10]; while(1) { setbkcolor(5);//set background color _strtime(t);//pick system time and saves in char array setcolor(11);//color of time settextstyle(4, HORIZ_DIR, 8);//font of time outtextxy(100, 100, t);//prints time delay(1000); } getch(); closegraph(); }
line() Function in C++
The header file graphics.h contains line() function which is used to draw a line from a point(x1, y1) to point(x2, y2) i.e. (x1, y1) and (x2, y2) are end points of the line. The function line() draws a line on the graphics screen between two specified points. So this function requires four parameters namely x1, y1, x2, and y2 to represent two points. This function draws a line from (x1, y1) coordinates to (x2, y2) coordinates on the graphics screen.
Syntax for line() Function in C++
void line(int x1, int y1, int x2, int y2);
x1
X coordinate of first point
y1
Y coordinate of first point.
x2
X coordinate of second point.
y2
Y coordinate of second point. You can change "linestyle", "pattern", "thickness" of the line by setlinestyle() function.
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
/* draw a line in C++ graphic code example */ #include<iostream.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT,gm,x,y; clrscr(); initgraph(&gd,&gm,"c:\\TC\\bgi"); //INITIALISING GRAPHICS MODE setlinestyle(0,0,3); outtextxy(300,150,"LINE()"); line(350,60,200,200); outtextxy(300,300," CURRENT POSITION"); linerel(320,350); outtextxy(335,315,"LINEREL()"); outtextxy(30,30," CURRENT POSITION"); lineto(30,200); outtextxy(70,45,"LINETO()"); getch(); closegraph(); }
setcolor() Function in C++
setcolor() function is used to set the foreground color in graphics mode. After resetting the foreground color you will get the text or any other shape which you want to draw in that color. setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor. The current drawing color is the value to which pixels are set when lines, and so on are drawn. The drawing colors shown below are available for the CGA and EGA, respectively.
Syntax for setcolor() Function in C++
void setcolor(int color);
color
specify the color setcolor() functions contains only one argument that is color. It may be the color name enumerated in graphics.h header file or number assigned with that color. This function does not return any value. INT VALUES corresponding to Colors: • BLACK 0 • BLUE 1 • GREEN 2 • CYAN 3 • RED 4 • MAGENTA 5 • BROWN 6 • LIGHTGRAY 7 • DARKGRAY 8 • LIGHTBLUE 9 • LIGHTGREEN 10 • LIGHTCYAN 11 • LIGHTRED 12 • LIGHTMAGENTA 13 • YELLOW 14 • WHITE 15
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
/* setcolor() function change the current drawing color in graphic mode. */ #include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT,gm; initgraph(&gd,&gm," "); setbkcolor(5);//set background color setcolor(11);//color of time settextstyle(4, HORIZ_DIR, 8);//font of time setcolor(GREEN); circle(320,240,100); setcolor(RED); outtextxy(320,80."It is circle"); getch(); closegraph(); }
kbhit() Function in C++
The kbhit is basically the Keyboard Hit. This function is present at conio.h header file. So for using this, we have to include this header file into our code. The functionality of kbhit() is that, when a key is pressed it returns nonzero value, otherwise returns zero. kbhit() is used to determine if a key has been pressed or not. If a key has been pressed then it returns a non zero value otherwise returns zero.
Syntax for kbhit() Function in C++
#include <conio.h> int kbhit();
Function returns true (non-zero) if there is a character in the input buffer, otherwise false. Note : kbhit() is not a standard library function and should be avoided.
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
/* kbhit() function is not defined as part of the ANSI C/C++ standard. It is generally used by Borland's family of compilers. It returns a non-zero integer if a key is in the keyboard buffer. It will not wait for a key to be pressed. */ // C++ program code example to fetch key pressed using kbhit() #include <conio.h> #include <iostream> int main() { char ch; while (1) { if (kbhit) { // Stores the pressed key in ch ch = getch(); // Terminates the loop // when escape is pressed if (int(ch) == 27) break; cout << "Key pressed= " << ch; } } return 0; }
putpixel() Function in C++
The header file graphics.h contains putpixel() function which plots a pixel at location (x, y) of specified color. Where, (x, y) is the location at which pixel is to be put, and color specifies the color of the pixel. To put a pixel on the screen at a particular position, calling the pixel() function is a good way. This function takes three parameters as the position of the pixel and also the color of the pixel.
Syntax for putpixel() Function in C++
#include <graphics.h> void putpixel(int x, int y, int color);
x
X coordinate of the point
y
Y coordinate of the point
color
specifies the color of the pixel To use these function in your program, we would need to include graphics.h file in your program. You should also use getch() function to make the screen freeze.
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
/* putpixel() function writes a pixel to the specified position in the bitmap, using the current drawing mode and the bitmap's clipping rectangle. */ /* plot a point in the color defined by color at (x,y) by putpixel() function code example. */ #include <graphics.h> #include <stdio.h> // driver code int main() { // gm is Graphics mode which is // a computer display mode that // generates image using pixels. // DETECT is a macro defined in // "graphics.h" header file int gd = DETECT, gm, color; // initgraph initializes the // graphics system by loading a // graphics driver from disk initgraph(&gd, &gm, ""); // putpixel function putpixel(85, 35, GREEN); putpixel(30, 40, RED); putpixel(115, 50, YELLOW); putpixel(135, 50, CYAN); putpixel(45, 60, BLUE); putpixel(20, 100, WHITE); putpixel(200, 100, LIGHTBLUE); putpixel(150, 100, LIGHTGREEN); putpixel(200, 50, YELLOW); putpixel(120, 70, RED); getch(); // closegraph function closes the // graphics mode and deallocates // all memory allocated by // graphics system . closegraph(); return 0; }
getch() Function in C++
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.
Syntax for getch() Function in C++
#include <conio.h> int getch(void);
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. We use a getch() function in a C/ C++ program to hold the output screen for some time until the user passes a key from the keyboard to exit the console screen. Using getch() function, we can hide the input character provided by the users in the ATM PIN, password, etc. • getch() method pauses the Output Console until a key is pressed. • It does not use any buffer to store the input character. • The entered character is immediately returned without waiting for the enter key. • The entered character does not show up on the console. • The getch() method can be used to accept hidden inputs like password, ATM pin numbers, etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* wait for any character input from keyboard by getch() function code example. The getch() function is very useful if you want to read a character input from the keyboard. */ // C code to illustrate working of // getch() to accept hidden inputs #include<iostream.h> #include<conio.h> void main() { int a=10, b=20; int sum=0; clrscr(); sum=a+b; cout<<"Sum: "<<sum; getch(); // use getch() befor end of main() }
sound() Function in C++
Our system can create various sounds on different frequencies. The sound() is very useful as it can create very nice music with the help of programming and our user can enjoy music during working in out the program. Sound function produces the sound of a specified frequency. Used for adding music to a C++ program.
Syntax for sound() Function in C++
void sound(unsigned frequency);
frequency
the frequency of the sound
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* sound() function produces the sound of a specified frequency. */ int k; //loop to increment the value of a till 100. for ( k = 1 ; a <= 100 ; a = k++ ) { //calling the function for producing //the sound of value a. sound(a); //delay the sound 10 miliseconds. delay(10); } // function to stop the system sound. nosound(); return 0;
While Loop Statement in C++
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.
Syntax for While Loop Statement in C++
while (condition) { // body of the loop }
• A while loop evaluates the condition • If the condition evaluates to true, the code inside the while loop is executed. • The condition is evaluated again. • This process continues until the condition is false. • When the condition evaluates to false, the loop terminates. Do not forget to increase the variable used in the condition, otherwise the loop will never end!
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
/* While Loop Statement in C++ language */ // program to find the sum of positive numbers // if the user enters a negative number, the loop ends // the negative number entered is not added to the sum #include <iostream> using namespace std; int main() { int number; int sum = 0; // take input from the user cout << "Enter a number: "; cin >> number; while (number >= 0) { // add all positive numbers sum += number; // take input again if the number is positive cout << "Enter a number: "; cin >> number; } // display the sum cout << "\nThe sum is " << sum << endl; return 0; }
delay() Function in C++
delay() function is used to hold the program's execution for given number of milliseconds, it is declared in dos.h header file. There can be many instances when we need to create a delay in our programs. C++ provides us with an easy way to do so. We can use a delay() function for this purpose in our code. We can run the code after a specific time in C++ using delay() function.
Syntax for delay() Function in C++
void delay(unsigned int milliseconds);
milliseconds
how many milliseconds to delay The function takes one parameter which is unsigned integer. Here, void suggests that this function returns nothing. 'delay' is the function name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* hold the program's execution for given number of milliseconds by delay() function code example. */ #include<iostream.h> #include<dos.h> //for delay() #include<conio.h> //for getch() int main() { clrscr(); int n; cout<<"Enter the delay (in seconds) you want to make after giving input."<<endl; cin>>n; delay(n*1000); cout<<"This has been printed after "<< n <<" seconds delay"; getch(); return 0; }
circle() Function in C++
This library function is declared in graphics.h and used to draw a circle; it takes centre point coordinates and radius. Circle function is used to draw a circle with center (x,y) and third parameter specifies the radius of the circle. The code given below draws a circle. Where, (x, y) is center of the circle. 'radius' is the Radius of the circle.
Syntax for circle() Function in C++
#include <graphics.h> circle(x, y, radius);
x
X-coordinate of the circle
y
Y-coordinate of the circle
radius
radius of the circle This function does not return any value.
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
/* draw a circle with center (x,y) and third parameter specifies the radius of the circle by circle() function code example. */ int gd=DETECT,gm=0,col=0,dol=600; initgraph(&gd,&gm,"c:/tc/bgi"); settextstyle(10,HORIZ_DIR,1); outtextxy(30,30,""); settextstyle(11,HORIZ_DIR,1); settextstyle(10,HORIZ_DIR,1); outtextxy(30,200,"Hit ENTER to Start the Magic..."); settextstyle(12,HORIZ_DIR,1); getch(); cleardevice(); while(!kbhit()) { for(int j=0;j<=50;j++) { { setcolor(2); circle(col,100,50+j); setfillstyle(4,2); floodfill(col,100,2); delay(3); col++; if(col>=600) col=0; }
outtextxy() Function in C++
outtextxy displays a text string in the viewport at the given position (x, y), using the current justification settings and the current font, direction, and size. To maintain code compatibility when using several fonts, use textwidth and textheight to determine the dimensions of the string. If a string is printed with the default font using outtext or outtextxy, any part of the string that extends outside the current viewport is truncated. outtextxy is for use in graphics mode; it will not work in text mode.
Syntax for outtextxy() Function in C++
#include <graphics.h> void outtextxy(int x, int y, char *string);
x
x-coordinate of the point
y
y-coordinate of the point
string
string to be displayed where, x, y are coordinates of the point and, third argument contains the address of string to be displayed. This function does not return any value.
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
/* outtextxy() function displays the text or string at a specified point (x, y) on the screen. */ // C++ Implementation for outtextxy() #include <graphics.h> int main() { textcolor(RED); cleardevice(); setcolor(RED); outtextxy(150,205,"Enter the Username:"); outtextxy(150,245,"Enter the Password:"); outtextxy(150,355,"Thank you"); outtextxy(150,445,"nice job"); outtextxy(50,105,"good day"); outtextxy(50,145,"pan"); outtextxy(50,255,"go"); outtextxy(50,245,"nice day"); return 0; }
Break Statement in C++
Break statement in C++ is a loop control statement defined using the break keyword. It is used to stop the current execution and proceed with the next one. When a compiler calls the break statement, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements. In the case of a nested loop, break the statement stops the execution of the inner loop and proceeds with the outer loop. The statement itself says it breaks the loop. When the break statement is called in the program, it immediately terminates the loop and transfers the flow control to the statement mentioned outside the loop.
Syntax for Break Statement in C++
// jump-statement; break;
The break statement is used in the following scenario: • When a user is not sure about the number of iterations in the program. • When a user wants to stop the program based on some condition. The break statement terminates the loop where it is defined and execute the other. If the condition is mentioned in the program, based on the condition, it executes the loop. If the condition is true, it executes the conditional statement, and if the break statement is mentioned, it will immediately break the program. otherwise, the loop will iterate until the given condition fails. if the condition is false, it stops the program.
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
/* break statement with while loop code example */ // program to find the sum of positive numbers // if the user enters a negative numbers, break ends the loop // the negative number entered is not added to sum #include <iostream> using namespace std; int main() { int number; int sum = 0; while (true) { // take input from the user cout << "Enter a number: "; cin >> number; // break condition if (number < 0) { break; } // add all positive numbers sum += number; } // display the sum cout << "The sum is " << sum << endl; return 0; }
nosound() Function in C++
The nosound() function in C language is used to stop the sound played by sound() function. The nosound() function is simply silent the system. The sound() and nosound() functions are very useful as they can create very nice music with the help of programming and our user can enjoy music during working in out the program.
Syntax for nosound() Function in C++
void nosound();
You can use the function nosound to turn off the PC speaker.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* you can simply silent the system by nosound() function code example. */ #include <stdio.h> //to use 'sound()', 'delay()' functions #include <dos.h> int main() { //calling the function for producing //the sound of frequency 400. sound(400); //function to delay the sound for //half of second. delay(500); //calling the function to stop the //system sound. nosound(); return 0; }
exit() Function in C++
The exit function terminates the program normally. Automatic objects are not destroyed, but static objects are. Then, all functions registered with atexit are called in the opposite order of registration. The code is returned to the operating system. An exit code of 0 or EXIT_SUCCESS means successful completion. If code is EXIT_FAILURE, an indication of program failure is returned to the operating system. Other values of code are implementation-defined.
Syntax for exit() Function in C++
void exit (int status);
status
Status code. If this is 0 or EXIT_SUCCESS, it indicates success. If it is EXIT_FAILURE, it indicates failure. Calls all functions registered with the atexit() function, and destroys C++ objects with static storage duration, all in last-in-first-out (LIFO) order. C++ objects with static storage duration are destroyed in the reverse order of the completion of their constructor. (Automatic objects are not destroyed as a result of calling exit().) Functions registered with atexit() are called in the reverse order of their registration. A function registered with atexit(), before an object obj1 of static storage duration is initialized, will not be called until obj1's destruction has completed. A function registered with atexit(), after an object obj2 of static storage duration is initialized, will be called before obj2's destruction starts. Normal program termination performs the following (in the same order): • Objects associated with the current thread with thread storage duration are destroyed (C++11 only). • Objects with static storage duration are destroyed (C++) and functions registered with atexit are called. • All C streams (open with functions in <cstdio>) are closed (and flushed, if buffered), and all files created with tmpfile are removed. • Control is returned to the host environment. Note that objects with automatic storage are not destroyed by calling exit (C++). If status is zero or EXIT_SUCCESS, a successful termination status is returned to the host environment. If status is EXIT_FAILURE, an unsuccessful termination status is returned to the host environment. Otherwise, the status returned depends on the system and library implementation. Flushes all buffers, and closes all open files. All files opened with tmpfile() are deleted. Returns control to the host environment from the program. exit() returns no values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* terminate the process normally, performing the regular cleanup for terminating programs by exit() function code example */ #include<iostream> using namespace std; int main() { int i; cout<<"Enter a non-zero value: "; //user input cin>>i; if(i) // checks whether the user input is non-zero or not { cout<<"Valid input.\n"; } else { cout<<"ERROR!"; //the program exists if the value is 0 exit(0); } cout<<"The input was : "<<i; }
setlinestyle() Function in C++
setlinestyle() is a function which is used to draw the line of different- different styles. Turbo C compiler provides five line styles that are solid, dotted, center, dashed and user defined. These all five line styles are already enumerated in graphics.h header file as given below: setlinestyle() function contains three parameters type, pattern and thickness. First parameter contains the type of line like solid, dashed or dotted etc. Second parameter is applicable only when type of line is user defined. Third parameter specifies the thickness of the line it takes values 1 (line thickness of one pixel (normal)) or 3 (line thickness of three pixels (thick).
Syntax for setlinestyle() Function in C++
#include <graphics.h> void setlinestyle(int linestyle, unsigned upattern, int thickness);
linestyle
First parameter contains the type of line like solid, dashed or dotted etc. The enumeration line_styles, which is defined in graphics.h, gives names to these operators: • SOLID_LINE 0 Solid line • DOTTED_LINE 1 Dotted line • CENTER_LINE 2 Centered line • DASHED_LINE 3 Dashed line • USERBIT_LINE 4 User-defined line style
upattern
Second parameter is applicable only when type of line is user defined.
thickness
Third parameter specifies the thickness of the line it takes values 1 (line thickness of one pixel (normal)) or 3 (line thickness of three pixels (thick). • NORM_WIDTH 1 1 pixel wide • THICK_WIDTH 3 3 pixels wide Note: The linestyle parameter does not affect arcs, circles, ellipses, or pie slices. Only the thickness parameter is used. If invalid input is passed to setlinestyle, graphresult returns -11, and the current line style remains unchanged.
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
/* setlinestyle() function sets the style for all lines drawn by line, lineto, rectangle, drawpoly, and so on. */ // C Implementation for setlinestyle() #include <graphics.h> // driver code int main() { // gm is Graphics mode which is // a computer display mode that // generates image using pixels. // DETECT is a macro defined in // "graphics.h" header file int gd = DETECT, gm; // variable to change the // line styles int c; // initial coordinate to // draw line int x = 200, y = 100; // initgraph initializes the // graphics system by loading a // graphics driver from disk initgraph(&gd, &gm, ""); // To keep track of lines for ( c = 0 ; c < 5 ; c++ ) { // setlinestyle function setlinestyle(c, 0, 1); // Drawing line line(x, y, x+200, y); y = y + 25; } getch(); // closegraph function closes the // graphics mode and deallocates // all memory allocated by // graphics system . closegraph(); return 0; }
ellipse() Function in C++
Ellipse is used to draw an ellipse (x,y) are coordinates of center of the ellipse, startangle is the starting angle, end angle is the ending angle, and fifth and sixth parameters specifies the X and Y radius of the ellipse. To draw a complete ellipse strangles and end angle should be 0 and 360 respectively.
Syntax for ellipse() Function in C++
#include <graphics.h> void ellipse(int x, int y, int startangle, int endangle, int xradius, int yradius);
x
x-coordinate of center of the ellipse
y
y-coordinate of center of the ellipse
startangle
starting angle
endangle
ending angle
xradius
specifies the X radius of the ellipse
yradius
specifies the Y radius of the ellipse Making a circle and an ellipse in C can be done easily. How to do is, first initialize a graph with two parameters and a path to the "bgi" folder in your system. To make an ellipse on the screen, all we need to do is call the ellipse() function with six numbers as the coordinates of the ellipse. These six co-ordinates decide the location of the ellipse, angles, and radius from X-axis and Y-axis. We would need to include graphics.h file in your program.
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
/* graphics.h library is used to include and facilitate graphical operations in program. C graphics using graphics.h functions can be used to draw different shapes, display text in different fonts, change colors and many more. Using functions of graphics.h you can make graphics programs, animations, projects and games. You can draw circles, lines, rectangles, bars and many other geometrical figures. You can change their colors using the available functions and fill them. */ /* draw an ellipse on the screen by ellipse() function code example */ #include <graphics.h> int main() { // gm is Graphics mode which is a computer display // mode that generates image using pixels. // DETECT is a macro defined in "graphics.h" header file int gd = DETECT, gm; // location of ellipse int x = 250, y = 200; // here is the starting angle // and end angle int start_angle = 0; int end_angle = 360; // radius from x axis and y axis int x_rad = 100; int y_rad = 50; // initgraph initializes the graphics system // by loading a graphics driver from disk initgraph(&gd, &gm, ""); // ellipse function ellipse(x, y, start_angle, end_angle, x_rad, y_rad); getch(); // closegraph function closes the graphics // mode and deallocates all memory allocated // by graphics system . closegraph(); return 0; }
floodfill() Function in C++
floodfill function is used to fill an enclosed area. Current fill pattern and fill color is used to fill the area.(x, y) is any point on the screen if (x,y) lies inside the area then inside will be filled otherwise outside will be filled, border specifies the color of boundary of area. To change fill pattern and fill color use setfillstyle.
Syntax for floodfill() Function in C++
#include <graphics.h> void floodfill(int x, int y, int border_color).
x
X coordinate of the point within the enclosed area to be filled
y
Y coordinate of the point within the enclosed area to be filled
border_color
specify the color int values corresponding to colors: • BLACK 0 • BLUE 1 • GREEN 2 • CYAN 3 • RED 4 • MAGENTA 5 • BROWN 6 • LIGHTGRAY 7 • DARKGRAY 8 • LIGHTBLUE 9 • LIGHTGREEN 10 • LIGHTCYAN 11 • LIGHTRED 12 • LIGHTMAGENTA 13 • YELLOW 14 • WHITE 15
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
/* floodfill() fills an enclosed area on bitmap devices. (x,y) is a "seed point" within the enclosed area to be filled. The area bounded by the color border is flooded with the current fill pattern and fill color. If the seed point is within an enclosed area, the inside will be filled. If the seed is outside the enclosed area, the exterior will be filled. Use fillpoly instead of floodfill whenever possible so that you can maintain code compatibility with future versions. */ /* fill an enclosed area on bitmap devices by floodfill() function code example. */ #include <stdio.h> #include <conio.h> void main() { int d,m; int midx,midy; d=DETECT; initgraph(&d,&m,"c:\\tc\\bgi"); midx=getmaxx()/2; midy=getmaxy()/2; circle(midx,midy,50); floodfill(midx,midy,15); circle(midx+50,midy+100,80); floodfill(midx,midy,15); getch(); closegraph(); }


Full form of Ascii is American Standard Code for Information Interchange. It is a character "Encoding Scheme". Originally based on the English alphabet, every character or number