Nested while loop int i =1; int j=1; int rows=8; while ( i <= rows) { while (j <= i ) { cout <<j; j++ ; } cout <<"\n"; i ++; j = 1; } 12
The getche () Library function Echoes the character on the screen. Returns each character as soon as it is typed. It takes no arguments. Requires conio.h header file 13
The getche () Library function #include< iostream > using namespace std ; int main() { char ch ; cout <<“Want to continue (Press :(y/n)) : ”; cin >> ch ; return 0; } 14 #include< iostream > #include< conio.h > using namespace std ; int main() { char ch ; cout <<“Want to continue (Press :(y/n)) : ”; ch = getche (); return 0; }
int main() { char dir ='a'; int x=10,y=10; cout <<"\ nPress direction key ( n,s,e,w ): "; cin >> dir ; if( dir == 'n') {y--;} else if( dir == 's') {y++;} else if( dir == 'e') {x++;} else if( dir == 'w') {x--;} cout << "\ nYour location is " <<x<<","<<y; return 0; }
The getche () Library function int main() { char dir ='a'; int x=10,y=10; cout <<"Type enter to quit\n"; while( dir != '\r') { cout <<"\ nPress direction key ( n,s,e,w ): "; dir = getche (); if ( dir == 'n') {y--;} else if( dir == 's') {y++;} else if( dir == 'e') {x++;} else if( dir == 'w') {x--;} cout << "\ nYour location is " <<x<<","<<y; } return 0; }
Switch – Activity Write the direction program using switch statement. 17
The getche () Library function – using switch int main() { char dir ='a'; int x=10,y=10; while( dir != '\r') { cout << "\ nYour location is " <<x<<","<<y; cout <<"\ nPress direction key ( n,s,e,w ): "; dir = getche (); switch( dir ) case ‘n’: y--; break; case ‘s’: y++; break; case ‘e’: x++; break; case ‘w’: x--; break; case ‘\r’: cout <<“Exiting\n”; break; default: cout <<“Try Again\n”; } return 0; }
The getche () Library function int main(){ int chcount = 0; int wdcount =1; char ch ='a'; cout << "Enter a new phrase: "; while( ch != '\r' ) { ch = getche (); if ( ch == ' ') wdcount ++; else chcount ++; } cout <<"\ nWOrds =" << wdcount << endl ; cout <<"Letters=" <<( chcount - 1 )<< endl ; return 0; }
The getche () Library function int main() { int chcount = 0; int wdcount =1; char ch ='a'; cout << "Enter a new phrase: "; while(( ch = getche () )= '\r' ) { if ( ch == ' ') wdcount ++; else chcount ++; } cout <<"\ nWOrds =" << wdcount << endl ; cout <<"Letters=" <<( chcount - 1 )<< endl ; return 0; }