Showing posts with label Defined Constants in C Program. Show all posts
Showing posts with label Defined Constants in C Program. Show all posts

Sunday, December 13, 2015

Draw Progress Bar using C Program

Program #29

Description:
Draw Progress Bar using C Program.  ASCII character plays to vital role to make it.
ASCII Positions of { 176, 177, 178 and 220} are good to make Progress Bar.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

/* Define default postion to
Row, Column, Progress bar length and ASCII Position used to make progress Bar
*/
#define DEFAULT_COL 25;
#define DEFAULT_ROW 12;
#define BLOCKS_TO_PRINT 25;
#define ASCII_POSITION 220;

/* !Important:
  Character Position 176, 177, 178 and 220
  are good to make Progress Bar */

void main() {
   int i, col, row, blocks;
   char myAscii;

   /* prototype declaration */
   char getAscii();

   /* Clear the Screen */
   clrscr();

   /* Set default position to print */
   col     = DEFAULT_COL;
   row     = DEFAULT_ROW;
   blocks  = BLOCKS_TO_PRINT;

   /* get expected ASCII */
   myAscii = getAscii();

   for(i=1;i<blocks;i++) {
      gotoxy(col++, row);
      printf("%c", myAscii);
      delay(50);
   }

   getch();
}

/* Iterate the loop until expected ASCII character */
char getAscii(){
  int i, ascii_pos;
  char cc;

  /* Assign default character position */
  ascii_pos = ASCII_POSITION;
  for (i=0; i<256; i++) {

    /*  Break the Loop, once we got the Character */
    if (i == ascii_pos)  break;
    cc = cc + 1;
  }
  return cc;
}



 

Saturday, December 12, 2015

Get Alphabet Input using C Program

Program #24

Description:
To detect only character input either lower/upper character and print.  Rest of the keypress are ignored.

#include<stdio.h>
#include<conio.h>
#include<dos.h>

/* user defined Constant to restrict max length */
#define MAXLENGTH 10

/* Global String Array */
char userInput[MAXLENGTH];

int main(){
 int i, charCounter=0;
 char c=' ';

 /* prototype declaration */
 void printReceivedInputs(int);
 clrscr();

 printf("\n!Important: Maximum %d valid characters only.\n", MAXLENGTH);
 for(i=1; i<46; i++){
   printf("-");
   delay(100);
 }

 printf("\nPlease Enter Alphabets value [a-zA-Z]:\n");
 for(;!kbhit();){
    c = getch();

    /* Stop getting input, If user press below keys
     Enter Key - 13
     ESC Key   - 27
     TAB Key   -  9
     Space Bar - 32
     Backspace -  8
    */

    if (charCounter == MAXLENGTH){
      printf("\n\n\nThanks for your Input!\n");
      for (i=0; i<22; i++){
 printf("_");
 delay(100);
      }
      break;
    } else if (c == 13 || c == 27 || c == 9 || c == 32 || c == 8){
      printf("\nUser Intruption !!");
      getch();
      return 0;
    }

    /* Accept only Lower Case charaters, else ignore */
    else if (c >= 97 && c <= 122){
      userInput[charCounter++] = c;
      /* Dynamically print the user input */
      printReceivedInputs(charCounter);
    }

    /* Accept only Upper Case charaters, else ignore */
    else if (c >= 65 && c <= 90){
      userInput[charCounter++] = c;
      /* Dynamically print the user input */
      printReceivedInputs(charCounter);
    }
 }

 printf("\n\nInput #\n%s", userInput);
 getch();
 return 0;
}

void printReceivedInputs(int charLen){
  int i;
  gotoxy(15,8);
  printf("          ");
  gotoxy(15,8);
  for (i=0; i<charLen; i++){
    printf("%c",userInput[i]);
  }
}



 

Friday, December 11, 2015

Get number input using C Program


Program #20

Description:
Get number [0-9] only as input from the user.  Even if user press characters, system will ignore and wait for valid input.  Once input reaches MAXLENGTH (user defined length) program will stop the execution.


#include<stdio.h>
#include<conio.h>
#include<dos.h>

/* user defined Constant to restrict max length */
#define MAXLENGTH 10

/* Global String Array */
char userInput[MAXLENGTH];
int main(){
 int i, charCounter=0;
 char c=' ';

 /* prototype declaration */
 void printReceivedInputs(int);

 clrscr();
 printf("\n!Important: Maximum %d valid characters only.\n", MAXLENGTH);
 for(i=1; i<46; i++){
   printf("-");
   delay(100);
 }
 printf("\nPlease Enter Integer value [0-9]:\n");

 for(;!kbhit();){
    c = getch();

    /* Stop getting input, If user press below keys
     Enter Key - 13
     ESC Key   - 27
     TAB Key   -  9
     Space Bar - 32
     Backspace -  8
    */

    if (charCounter == MAXLENGTH){
      printf("\n\n\nThanks for your Input!\n");
      for (i=0; i<22; i++){
 printf("_");
 delay(100);
      }
      break;
    } else if (c == 13 || c == 27 || c == 9 || c == 32 || c == 8){
      printf("\nUser Intruption !!");
      getch();
      return 0;
    }
   
/* Accept only Numbers, else ignore */
    else if (c >= 48 && c <= 57){
      userInput[charCounter++] = c;
     
/* Dynamically print the user input */
      printReceivedInputs(charCounter);
    }
 }

 printf("\n\nInput #\n%s", userInput);
 getch();
 return 0;
}

void printReceivedInputs(int charLen){
  int i;
  gotoxy(15,8);
 
  /* Clear the Output area */
  printf("          ");
 
  /* Start to print on perfect position */
  gotoxy(15,8);
  for (i=0; i<charLen; i++){
    printf("%c",userInput[i]);
  }
}


 

 

Popular Posts