Tuesday, January 17, 2012

C code for PIC micros and Hi-Tech C


Types for Hi-Tech C

  • Data Types for Hi-Tech C, from Eduardo Rosso. Reasonably well known code, makes handling of data easier.

    Data types for Hi-Tech C.



Delay and Timeout Routines for Hi-Tech C (PIC16Fx core)

  •  Example project illustrating delay and timeout routines.
    Unlike the routines available from the Hi-Tech C site, these are written in in-line assembler and thus give precise timing.  Remember to match the actual microprocessor clock speed to the setting for PIC_CLK.
  • Delay routines v7.1 for Hi-Tech C and PIC16x core.



Delay and Timeout Routines for Hi-Tech C (PIC18Fx core)

  •  Example project illustrating delay and timeout routines.
    Unlike the routines available from the Hi-Tech C site, these are written in in-line assembler and thus give precise timing.  Remember to match the actual microprocessor clock speed to the setting for PIC_CLK.

    Note: these routines could cause problems if Hi-Tech C banks the assembly variables in the incorrect bank; the problem would only surface on a large project with high RAM usage. See file "Warning_deprecated_routines.txt" within the archive for an alternative solution.
  • Delay routines v2.2 for Hi-Tech C and PIC18x core.



RS232 Serial port




USB Serial port on PIC18F4550

  •  USB example for PIC18F4550. Plug your PIC18F4550 into your PC using a USB cable. A virtual RS232 COM port is created, allowing your application to print RS232 messages into HyperTerminal. Allows porting of legacy code that used RS232 comms.

    This project is a modified form of the CDC (Communications Class Driver) available from www.microchip.com. The www.htsoft.com forum has some discussion on this code.

    Note: ensure that you read "readme (including install notes).txt" after download.

    Schematics: this code runs on the PICDEM-FS board from Microchip. To make your own schematic, place a PIC18F4550 on a breadboard, ensure that you are using a 20Mhz crystal, place a 470nF capacitor on port Vusb, and hook the USB directly to the chip. It should start to work straight away. Download the PICDEM-FS datasheet for the exact schematic to use with this code.

    Result: start up HyperTerminal or putty, find the new COMx port that has been enumerated. Set it to 115200,N,8,1. The actual COM port speed doesn't matter, as its a virtual comm port. You will see "[alive]" being continuously generated on virtual com port X over USB. You can find the exact number of the virtual COM port by going into "Control Panel", then "System", then selecting the "Hardware" then "Device Manager", there will be a new COM port under "Ports (COM & LPT). You can change the number of the virtual comm port by going into properties, and selecting the new COM port.

    11th Nov 2007 - tried it on Hi-Tech C v9.62, it worked. Had reports that it does not work on Hi-Tech C v9.51.
    2nd Feb 2009 - customer got it working with Hi-Tech C v9.51, it worked after some tweaks. Added instructions to v1.10 of the download.

    Download PIC18F4550 USB serial port example v1.02 (540KB).
    Download PIC18F4550 USB serial port example v1.10 (540KB).



Bootloaders for PIC Microcontrollers

  • Bootloaders for various PIC microcontrollers.

    • PIC16F87x bootloader. Browse.
    • PIC18F1320 bootloader. This is a port of the PIC16F87x bootloader to the PIC18F1320. Project includes Microsoft Visual C++ source for the Windows downloader, and assembly source for the PIC core. For more background documentaiton about bootloaders in general, see the PIC16F87x bootloader page.

      Download (570KB).
    • PIC17C4x bootloader (obsolete).
      Download (224KB).
    • PIC18x52 bootloader. Browse.
    • dsPIC bootloader from Ingenia. For any dsPIC device. Browse to section.



dsPIC bootloader from Ingenia


This is the source code of the Ingenia dsPIC bootloader.

The bootloader consists of: 
  • Open source firmware code, written in assembler. Can be adapted for generic for all dsPIC devices. 
  • A Windows based Graphical User Interface. Comes with user's guide, algorithm flow, etc.
The main features of the firmware are: 
  • Auto-Baud rate detection. This allows one to use any speed of crystal.
  • Ability to read/write program memory.
  • Ability to read/write EEPROM Memory 
  • Ability to read/write configuration registers.
  • Easily add new dsPIC devices. By default comes only with dsPIC30F4011 and dsPIC30F3011. Define and protect memory zones through a XML configuration file.
Forum devoted to the Ingenia bootloader. See feedback from other users of this bootloader. 

Or browse directly to the Ingenia bootloader homepage or the Ingenia company homepage.
Download

Download firmware and Windows installer (3.8MB).
Download user manual (442KB). 
If you wish to contribute configuration files for this bootloader, for different dsPIC devices, email  support@microchipc.com.


Sample Projects in MPLab for Hi-Tech

  •  Example project in MPLab for PIC16F876 and Hi-Tech C 7.86pl2, showing how to output decimals using putch().
    Download.
  • Extremely simple example of how to efficiently count bits in an integer.
    Download.



I2C

  • Example Hi-Tech C code for I2C, interfacing to Microchip 24LC01B non-volatile EEPROM and Dallas Temperature sensors DS1775 and DS1721.
    Download (4KB).
  •  By Mike Pearce. From readme.txt: "A complete set of bit banged, software driven I2C routines I created for any PIC device - and they work!! These functions are single master only functions, and are ideal for communicating with things like EEPROMs, LCD Drivers, ADC Converters etc.". Intel enquired about using this code in one of their products.
    Download (51KB).
    See Mike Pearces projects below for more.
  •  Example code for I2C, routines for PIC16F877 to write to the 24LC01B EEPROM, using the PICDEM 2 demo board from Microchip. By Michael Alon.
    Download (5KB).



CRC

  • Excellent CRC code.
    // Update the CRC for transmitted and received data using
    // the CCITT 16bit algorithm (X^16 + X^12 + X^5 + 1).
    unsigned char ser_data;
    static unsigned int crc;
    crc = (unsigned char)(crc >> 8) | (crc << 8);
    crc ^= ser_data;
    crc ^= (unsigned char)(crc & 0xff) >> 4;
    crc ^= (crc << 8) << 4;
    crc ^= ((crc & 0xff) << 4) << 1;


    Hi Shane,

    I appreciate the time and effort you spent to make this page, well done !!

    I've been involved with PIC micros since 1996 and programming in
    C (CCS Compiler) for the last two years.

    I want to send you my best regards from Argentina.

    - Humberto 

Interfacing to miscellaneous hardware with Hi-Tech C

  •  PIC16F876 to PC comms via RS232 with sample Hi-Tech C code and VB 6 code. Link.
  • By Mike Pearce. From readme.txt: Dallas DS1820 Digital Temperature Probe routines for the PIC12Fx and PIC16Fx series. Includes CRC checking routines. Download (19KB). See Mike Pearces projects below for more.
  • PIC and SPI bus for a Linear Tech LTC2404 24 bit A/D module, from Scott Douglas, Project Engineer, Blodgett Combi. Heavily commented implementation. Download (6KB). 
  • How to use EEPROM on 12CE673/4. Download (1KB).
  •  For a PIC16F87x, how to initialize the built-in 256 bytes of EEPROM at compile time. Download.
  • How to use EEPROM under CCS C for 24LC256. Link.
  • How to use EEPROM under Hi-Tech C for 24LC01. Link.
  • Example A/D code for 12-bit Texas Instruments TLC2543.  Download. 
  • Example D/A code for 12-bit Analogue Devices AD7390.  Download.
  • How to use the 93XXX EEPROM chip utilizing built-in hardware SPI for 16X and 17X series micros. Download.
  • Example A/D code for inbuilt ports on PIC16F876/7.  Download.
  • Example C code for SPI to access the following chips.
    • A/D chip, the MAX186 12 bit 8-channel micro-wire and D/A chip, the LTC1446 12 bit 2-channel micro-wire. Download.

  • Example Hi-Tech C code for RS232, Dallas DS1821 temperature probe, I2C, A/D, LCD, keypad, see sample projects below by Mike Pearce.



Complete Design for Giant 8-foot LCD Counter

This is the complete design for a giant 8 foot money counter for student debt. It is still working beautifully to this day, counting up student debt in the main library at Canterbury University of New Zealand.
Download the complete design, including sample PIC code in C (for PIC16F876 or pIC16F877), Visual Basic code, Protel 99 .pcb files, plans and photos. 
Download (1080KB).
Explanation of the time-sliced multitasking used in this project.


Multitasking on a PIC in Hi-Tech C




USB to IBM-PC communications using PIC16F876


See the following link for Hi-Tech C source code for interfacing a 16F876 to a PC using a Philips PDIUSBD11 chip. Many thanks to Craig Peacock. http://www.beyondlogic.org/usbnutshell/usb7.htm#PIC16F876Example.




Mechanically scanned display source code in Hi-Tech C


From Rickard Gunée in Norway, the design for a rotor with a row of LEDs on the end. As it spins, it forms pictures. The photo to the right shows a game of tetris implemented on the spinning led rotor! See http://www.rickard.gunee.com/projects/


Other

  • Sample projects that come with Hi-Tech C compiler.  Look in directory c:\ht-pic\samples for a/d routines, delay, i2c, interrupt, interrupt serial, software serial, LCD, SCI, timer0 and more.
  • Sample routines that come with the Hi-Tech C compiler.  Look in directory c:\ht-pic\sources for many math, string, and variable routines.


read more "C code for PIC micros and Hi-Tech C"

10 Challenging star pattern programs in C

Computer languages are not as easy as human languages, they need you to become a computer yourself, think like a computer and write an algorithm. Computer programs are fun if you take them up as a challenge, as unsolved puzzles. There is lot of fun in taking up the challenge, understanding the problem, writing an algorithm, writing a program and most importantly running the program and obtaining required output. 
Here are few challenging C program questions for you. Let’s see how many of them you would be able to write without seeing the program solution given below. These questions are to print patterns using asterisk(star) character. Comment below if you have tougher pattern questions.


  1. Write a C program to print the following pattern:
    *
          *  *
       *  *  *
    *  *  *  *
    
  2. Write a C program to print the following pattern:
    *                   *
    * * *           * * *
    * * * * *   * * * * *
    * * * * * * * * * * *
    
  3. Write a C program to print the following pattern:
    *                         *
       *                   *
    *     *             *     *
       *     *       *     *
    *     *      *      *     *
       *     *       *     *
    *     *             *     *
       *                   *
    *                         *
    
  4. Write a C program to print the following pattern:
    *   *   *   *   *
      *   *   *   *
        *   *   *
          *   *
            *
          *   *
        *   *   *
      *   *   *   *
    *   *   *   *   *
    
  5. Write a C program to print the following pattern:
    *
            * * *
          * * * * *
        * * * * * * *
      * * * * * * * * *
        * * * * * * *
          * * * * *
            * * *
              *
            * * *
          * * * * *
    
  6. Write a C program to print the following pattern:
    *
           * *
             * * *
               * * * *
             * * *
           * *
         *
    
  7. Write a C program to print the following pattern:
    * * * * * * * * *
    * * * *   * * * *
    * * *       * * *
    * *           * *
    *               *
    * *           * *
    * * *       * * *
    * * * *   * * * *
    * * * * * * * * *
    
  8. Write a C program to print the following pattern:
    * * * * * * * * * * * * * * * * *
      * * * * * * *   * * * * * * *
        * * * * *       * * * * *
          * * *           * * *
            * * * * * * * * *
              * * * * * * *
                * * * * *
                  * * *
                    *
    
  9. Write a C program to print the following pattern:
    *
        * * *
      * * * * *
    * * * * * * *
    *           *
    * *       * *
    * * *   * * *
    * * * * * * *
    * * *   * * *
    * *       * *
    *           *
    * * * * * * *
      * * * * *
        * * *
          *
    
  10. Write a C program to print the following pattern:
    * * * * * * * * * * * * * * * * * * * * * * * * *
      *           *   *           *   *           *
        *       *       *       *       *       *
          *   *           *   *           *   *
            *               *               *
          *   *           *   *           *   *
        *       *       *       *       *       *
      *           *   *           *   *           *
    * * * * * * * * * * * * * * * * * * * * * * * * *
    
  1. Write a C program to print the following pattern:
  2. *
          *  *
       *  *  *
    *  *  *  *
    Program:
    /* This is a simple mirror-image of a right angle triangle */
    
    #include 
    int main() {
     char prnt = '*';
     int i, j, nos = 4, s;
     for (i = 1; i <= 5; i++) {
    for (s = nos; s >= 1; s--) {  // Spacing factor
       printf("  ");
      }
      for (j = 1; j <= i; j++) {
       printf("%2c", prnt);
      }
      printf("\n");
      --nos;   // Controls the spacing factor
     }
     return 0;
    }
    Download Code
  3. Write C program to print the following pattern:
    *                   *
    * * *           * * *
    * * * * *   * * * * *
    * * * * * * * * * * *
  4. Program:
    #include
    int main() {
     char prnt = '*';
     int i, j, k, s, c = 1, nos = 9;
     for (i = 1; c <= 4; i++) {
         // As we want to print the columns in odd sequence viz. 1,3,5,.etc
      if ((i % 2) != 0) {
       for (j = 1; j <= i; j++) {
     printf("%2c", prnt);
    }
    for (s = nos; s >= 1; s--) { //The spacing factor
        if (c == 4 && s == 1) {
         break;
        }
        printf("  ");
       }
       for (k = 1; k <= i; k++) {
        if (c == 4 && k == 5) {
         break;
        }
        printf("%2c", prnt);
       }
       printf("\n");
       nos = nos - 4;  // controls the spacing factor
       ++c;
      }
     }
     return 0;
    }
    Download Code
  5. Write C program to print the following pattern:
    *                         *
       *                   *
    *     *             *     *
       *     *       *     *
    *     *      *      *     *
       *     *       *     *
    *     *             *     *
       *                   *
    *                         *
  6. Program:
    #include
    int main() {
     char prnt = '*';
     int i, j, k, s, p, r, nos = 7;
    
     for (i = 1; i <= 5; i++) {
      for (j = 1; j <= i; j++) {
     if ((i % 2) != 0 && (j % 2) != 0) {
    printf("%3c", prnt);
    }
    else if ((i % 2) == 0 && (j % 2) == 0) {
    printf("%3c", prnt);
    }
    else {
    printf("   ");
    }
    }
    for (s = nos; s >= 1; s--) {  // for the spacing factor
       printf("   ");
      }
      for (k = 1; k <= i; k++) { //Joining seperate figures
    if (i == 5 && k == 1) {
     continue;
    }
    if ((k % 2) != 0) {
    printf("%3c", prnt);
    }
    else {
    printf("   ");
    }
    }
    printf("\n");
    nos = nos - 2;   // space control
    }  nos = 1;  // remaining half..
    for (p = 4; p >= 1; p--) {
      for (r = 1; r <= p; r++) {
    if ((p % 2) != 0 && (r % 2) != 0) {
    printf("%3c", prnt);
    }
    else if ((p % 2) == 0 && (r % 2) == 0) {
    printf("%3c", prnt);
    }
    else {
    printf("   ");
    }
    }
    for (s = nos; s >= 1; s--) {
       printf("   ");
      }
      for (k = 1; k <= p; k++) {
       if ((k % 2) != 0) {
        printf("%3c", prnt);
       }
    else {
        printf("   ");
       }
      }
      nos = nos + 2;  // space control
      printf("\n");
     }
     return 0;
    }
    Download Code Explanation: This can be seen as an inverted diamond composed of stars. It can be noted that the composition of this figure follows sequential pattern of consecutive stars and spaces. In case of odd row number, the odd column positions will be filled up with ‘*’, else a space will be spaced and vice-versa in case of even numbered row. In order to achieve this we will construct four different right angle triagles aligned as per the requirement.
  7. Write a C program to print the following pattern:
    *   *   *   *   *
      *   *   *   *
        *   *   *
          *   *
            *
          *   *
        *   *   *
      *   *   *   *
    *   *   *   *   *
  8. Program:
    #include
    int main() {
     char prnt = '*';
     int i, j, s, nos = 0;
     for (i = 9; i >= 1; (i = i - 2)) {
      for (s = nos; s >= 1; s--) {
       printf("  ");
      }
      for (j = 1; j <= i; j++) {
       if ((i % 2) != 0 && (j % 2) != 0) {
        printf("%2c", prnt);
       } else {
        printf("  ");
       }
      }
      printf("\n");
      nos++;
     }
     nos = 3;
     for (i = 3; i <= 9; (i = i + 2)) {
    for (s = nos; s >= 1; s--) {
       printf("  ");
      }
      for (j = 1; j <= i; j++) {
    
       if ((i % 2) != 0 && (j % 2) != 0) {
        printf("%2c", prnt);
       } else {
        printf("  ");
       }
      }
      nos--;
      printf("\n");
     }
     return 0;
    }
    Download Code
  9. Write a C program to print the following pattern:
    *
            * * *
          * * * * *
        * * * * * * *
      * * * * * * * * *
        * * * * * * *
          * * * * *
            * * *
              *
            * * *
          * * * * *
  10. Program:
    #include
    int main() {
     char prnt = '*';
     int i, j, k, s, nos = 4;
     for (i = 1; i <= 5; i++) {
    for (s = nos; s >= 1; s--) {
       printf("  ");
      }
      for (j = 1; j <= i; j++) {
       printf("%2c", prnt);
      }
      for (k = 1; k <= (i - 1); k++) {
    if (i == 1) {     continue;
    }
    printf("%2c", prnt);
    }
     printf("\n");   nos--;
    }
     nos = 1;
    for (i = 4; i >= 1; i--) {
      for (s = nos; s >= 1; s--) {
       printf("  ");
      }
      for (j = 1; j <= i; j++) {
       printf("%2c", prnt);
      }
      for (k = 1; k <= (i - 1); k++) {
       printf("%2c", prnt);
      }
      nos++;
      printf("\n");
     }
     nos = 3;
     for (i = 2; i <= 5; i++) {
    if ((i % 2) != 0) {
    for (s = nos; s >= 1; s--) {
        printf("  ");
       }
       for (j = 1; j <= i; j++) {
        printf("%2c", prnt);
       }
      }
      if ((i % 2) != 0) {
       printf("\n");
       nos--;
      }
     }
     return 0;
    }
    Download Code
  11. Write a C program to print the following pattern:
    *
           * *
             * * *
               * * * *
             * * *
           * *
         *
  12. Program:
    /*
        This can be seen as two right angle triangles sharing the same base
        which is modified by adding few extra shifting spaces
    */
    #include 
    // This function controls the inner loop and the spacing
    // factor guided by the outer loop index and the spacing index.
    int triangle(int nos, int i) {
     char prnt = '*';
     int s, j;
     for (s = nos; s >= 1; s--) {    // Spacing factor
      printf("  ");
     }
     for (j = 1; j <= i; j++) {      //The inner loop
      printf("%2c", prnt);
     }
     return 0;
    }
    
    int main() {
     int i, nos = 5;
     //draws the upper triangle
     for (i = 1; i <= 4; i++) {
     triangle(nos, i);    //Inner loop construction
     nos++;              // Increments the spacing factor
     printf("\n");  }
    nos = 7;  //Draws the lower triangle skipping its base.
    for (i = 3; i >= 1; i--) {
      int j = 1;
      triangle(nos, i);  // Inner loop construction
      nos = nos - j;     // Spacing factor
      printf("\n");
     }
     return 0;
    }
    Download Code
  13. Write a C program to print the following pattern:
    * * * * * * * * *
    * * * *   * * * *
    * * *       * * *
    * *           * *
    *               *
    * *           * *
    * * *       * * *
    * * * *   * * * *
    * * * * * * * * *
  14. Program:
    #include 
    
    int main() {
     char prnt = '*';
     int i, j, k, s, nos = -1;
     for (i = 5; i >= 1; i--) {
      for (j = 1; j <= i; j++) {
     printf("%2c", prnt);
    }
    for (s = nos; s >= 1; s--) {
       printf("  ");
      }
      for (k = 1; k <= i; k++) {
       if (i == 5 && k == 5) {
        continue;
       }
       printf("%2c", prnt);
      }
      nos = nos + 2;
      printf("\n");
     }
     nos = 5;
     for (i = 2; i <= 5; i++) {
      for (j = 1; j <= i; j++) {
     printf("%2c", prnt);
    }
    for (s = nos; s >= 1; s--) {
       printf("  ");
      }
      for (k = 1; k <= i; k++) {
       if (i == 5 && k == 5) {
        break;
       }
       printf("%2c", prnt);
      }
      nos = nos - 2;
      printf("\n");
     }
     return 0;
    }
    Download Code
  15. Write a C program to print the following pattern:
    * * * * * * * * * * * * * * * * *
      * * * * * * *   * * * * * * *
        * * * * *       * * * * *
          * * *           * * *
            * * * * * * * * *
              * * * * * * *
                * * * * *
                  * * *
                    *
  16. Program:
    #include 
    int main() {
     char prnt = '*';
     int i, j, k, s, sp, nos = 0, nosp = -1;
     for (i = 9; i >= 3; (i = i - 2)) {
      for (s = nos; s >= 1; s--) {
       printf("  ");
      }
      for (j = 1; j <= i; j++) {
    printf("%2c", prnt);
    }
    for (sp = nosp; sp >= 1; sp--) {
       printf("  ");
      }
      for (k = 1; k <= i; k++) {
     if (i == 9 && k == 1) {
    continue;
    }
    printf("%2c", prnt);
    }
    nos++;
    nosp = nosp + 2;
    printf("\n");
    }
    nos = 4;
    for (i = 9; i >= 1; (i = i - 2)) {
      for (s = nos; s >= 1; s--) {
       printf("  ");
      }
      for (j = 1; j <= i; j++) {
       printf("%2c", prnt);
      }
      nos++;
      printf("\n");
     }
    
     return 0;
    }
    Download Code
  17. Write a C program to print the following pattern:
    *
        * * *
      * * * * *
    * * * * * * *
    *           *
    * *       * *
    * * *   * * *
    * * * * * * *
    * * *   * * *
    * *       * *
    *           *
    * * * * * * *
      * * * * *
        * * *
          *
  18. Program:
    #include 
    /*
     * nos = Num. of spaces required in the triangle.
     * i   = Counter for the num. of charcters to print in each row
     * skip= A flag for checking whether to
     *       skip a character in a row.
     *
     */
    int triangle(int nos, int i, int skip) {
     char prnt = '*';
     int s, j;
     for (s = nos; s >= 1; s--) {
      printf("  ");
     }
     for (j = 1; j <= i; j++) {
      if (skip != 0) {
       if (i == 4 && j == 1) {
        continue;
       }
      }
      printf("%2c", prnt);
     }
     return 0;
    }
    
    int main() {
     int i, nos = 4;
     for (i = 1; i <= 7; (i = i + 2)) {
      triangle(nos, i, 0);
      nos--;
      printf("\n");
     }
     nos = 5;
     for (i = 1; i <= 4; i++) {
    triangle(1, i, 0); //one space needed in each case of the formation
    triangle(nos, i, 1); //skip printing one star in the last row.
    nos = nos - 2;
    printf("\n");
    }
    nos = 1;
    for (i = 3; i >= 1; i--) {
      triangle(1, i, 0);
      triangle(nos, i, 0);
      nos = nos + 2;
      printf("\n");
     }
     nos = 1;
     for (i = 7; i >= 1; (i = i - 2)) {
      triangle(nos, i, 0);
      nos++;
      printf("\n");
     }
     return 0;
    }
    Download Code
  19. Write a C program to print the following pattern:
    * * * * * * * * * * * * * * * * * * * * * * * * *
      *           *   *           *   *           *
        *       *       *       *       *       *
          *   *           *   *           *   *
            *               *               *
          *   *           *   *           *   *
        *       *       *       *       *       *
      *           *   *           *   *           *
    * * * * * * * * * * * * * * * * * * * * * * * * *
  20. Program:
    #include 
    
    /*
     * nos = Num. of spaces required in the triangle.
     * i   = Counter for the num. of characters to print in each row
     * skip= A flag for check whether to
     *       skip a character in a row.
     *
     */
    
    int triangle(int nos, int i, int skip) {
     char prnt = '*';
     int s, j;
     for (s = nos; s >= 1; s--) {
      printf("  ");
     }
     for (j = 1; j <= i; j++) {
     if (skip != 0) {
     if (i == 9 && j == 1) {
       continue;
     }
     }
    if (i == 1 || i == 9) {
     printf("%2c", prnt);
    }
    else if (j == 1 || j == i) {
     printf("%2c", prnt);
     } else {
     printf("  ");
    }  }
    return 0; }
    int main() {
    int i, nos = 0, nosp = -1, nbsp = -1;
    for (i = 9; i >= 1; (i = i - 2)) {
      triangle(nos, i, 0);
      triangle(nosp, i, 1);
      triangle(nbsp, i, 1);
      printf("\n");
      nos++;
      nosp = nosp + 2;
      nbsp = nbsp + 2;
     }
     nos = 3, nosp = 5, nbsp = 5;
     for (i = 3; i <= 9; (i = i + 2)) {
      triangle(nos, i, 0);
      triangle(nosp, i, 1);
      triangle(nbsp, i, 1);
      printf("\n");
      nos--;
      nosp = nosp - 2;
      nbsp = nbsp - 2;
     }
     return 0;
    }


read more "10 Challenging star pattern programs in C"

Hotel Reservation System (Mini Project in C)

/*
Language: C\C++
Category: Database\Files
Description: Hotel Reservation
*/
#include stdio.h
#include conio.h
#include stdlib.h
#include dos.h
#define size 51

void mainm(void);
void entry(void);
void modify(void);
void delet(void);
void view(void);

// Structure

struct hoteldata
{
char name[size][30], nic[size][20],rdate[size][12],cdate[size][12];
};

struct hoteldata guest;

struct hoteldata1
{
char name[size][30], nic[size][20],rdate[size][12],cdate[size][12];
};

struct hoteldata1 guest1;



// Initializations

int li,lp,sr[size];int dr[size];int rno;int i;int e;
char rc,ch,choice,echoice,rchoice,temp[80],vchoice,mf,ar;


// Main Fuction
void main(void)
{
clrscr();
sr[0]=1;dr[0]=1; // THE MAIN LOGIC !!

for(li=10;li<71;li++) { gotoxy(li,15); delay(30); printf("\/"); } for(li=70;li>=10;li--)
{
gotoxy(li,22);
delay(30);
printf("\\");
}
for(lp=16;lp<=21;lp++) { gotoxy(10,lp); delay(100); printf("-"); } for(lp=21;lp>=16;lp--)
{
gotoxy(70,lp);
delay(100);
printf("=");
}
gotoxy(16,17);
textcolor(BLINK + MAGENTA);
cprintf("Welcome To The NALAIQUE's Hotel Reservation System");
gotoxy(12,20);
cprintf("Designed and Coded By NALAIQUE Technologies (C) 2002-2003");
gotoxy(36,28);
textcolor(LIGHTGREEN);
cprintf("Coded By");
gotoxy(24,31);
textcolor(2);
cprintf("Salman Javaid, # 31, BCS - 1 / B");
gotoxy(26,34);
textcolor(3);
cprintf("Irum Shahid, # 3, BCS - 1 / B");
textcolor(4);
gotoxy(22,37);
cprintf("Rabia Binte Aslam, # 61, BCS - 1 / B");
textcolor(5);
gotoxy(25,40);
textcolor(6);
cprintf("Rabia Latif, # 64, BCS - 1 / B");
gotoxy(44,48);
textcolor(WHITE);
cprintf("Press Any Key To ..........Continue");
getch();
mainm();
}// Main Fuction End

//Main Menu Function
void mainm(void)
{
clrscr();
for(li=30;li<=50;li++) { gotoxy(li,14); delay(30); printf("*"); } for(li=50;li>=30;li--)
{
gotoxy(li,30);
delay(30);
printf("*");
}
for(lp=15;lp<30;lp++) { gotoxy(30,lp); delay(100); printf("|"); } for(lp=29;lp>=15;lp--)
{
gotoxy(50,lp);
delay(100);
printf("|");
}
gotoxy(15,10);
textcolor(LIGHTGRAY);
cprintf("Press the corresponding Keys for the desired action");
gotoxy(33,16);
textcolor(BROWN);
cprintf("R: Reservation");
gotoxy(33,19);
textcolor(LIGHTMAGENTA);
cprintf("V: Room Status");
gotoxy(33,22);
textcolor(LIGHTBLUE);
cprintf("M: Modification");
gotoxy(33,25);
textcolor(LIGHTRED);
cprintf("D: Deletion");
gotoxy(33,28);
textcolor(GREEN);
cprintf("Q: Quit");
textcolor(WHITE);
choice=getch();
switch(choice)
{// Choice Switch Begin
case 'R':
entry();
case 'V':
view();
case 'M':
modify();
case 'D':
delet();
case 'Q':
exit(0);
default:
{//Choice Default
gotoxy(33,40);
printf("\n\nIllegal Choice or make sure CAPS Lock is on");
getch();
mainm();

}//Choice Default End
} //Choice Switch End
} //Main Menu Function End



// Data Entry Function
void entry(void)
{//Data Entry Function Begin
clrscr();
printf("Do You Want To reserve A Room? (Y/N)\n");
echoice=getch();
switch(echoice) // Echoice Switch Begin
{
case 'Y':
{ // E-Choice Case Y Begin
printf("\n\nEnter S for Single Room or D for Double Room\n\n");
rchoice=getch();
switch(rchoice) // Rchoice Switch Begin
{
case 'S': // Rchoice
{// R-Choice Case S Begin
for(i=1;i51)
{//IF Condition Begin
printf("\n\nIllegal Room Number");
getch();
mainm();
} //IF Condition End
else
{ // Limit Else Falsified Begin
if(sr[rno]==1)
{//sr==1 if Begin
printf("\n\nSingle Room No %d Is Reserved",rno);
printf("\n\nOccupant's Name : %s \n\nOccupant's NIC Number : %s \
\n\nCheck In Date : %s\n\nCheck Out Date : %s \n\n", \
guest.name[rno],guest.nic[rno],guest.rdate[rno],guest.cdate[rno]);

}//sr==1 if End
else
{// sr else begin
printf("\n\nSingle Room No %d is Vacant",rno);
getch();
}//sr else end
if(dr[rno]==1)
{//dr==1 if Begin
printf("\n\nDouble Room No %d Is Reserved",rno);
printf("\nOccupant's Name : %s \n\nOccupant's NIC Number : \
%s \n\nCheck In Date : %s\n\nCheck Out Date : %s \n\n", \
guest1.name[rno],guest1.nic[rno],guest1.rdate[rno], \
guest1.cdate[rno]);
printf("\n\nPress Any Key To Return To The Main Menu");
getch();
mainm();
}//dr==1 if End
else
{// dr else begin
printf("\n\nDouble Room No %d is Vacant",rno);
getch();
mainm();
}//dr else end
}//Vchoice Case R End
}// Falsified Limit End
default:
{
//Vchoice Default Begin
printf("\n\nIllegal Choice or make sure CAPS Lock is on");
getch();
mainm();
} // Vchoice Default End
} // Vchoice Switch End
} // Data View Function End



//Modify Function
void modify(void)
{// Modify Function Begin
clrscr();
printf("\t\t\t\tData Modification\n\n\n\n\n\n");
printf("Enter The Room Number To Be Modified\n");
gets(temp);
rno=atoi(temp);
// Room Number Limit Check
if(rno<1 || rno >51)
{//IF Condition Begin
printf("\n\nIllegal Room Number");
getch();
mainm();
} //IF Condition End
else
{ // Limit Else Falsified Begin
printf("Enter S For Single Room / D For Double Room\n");
rc=getch();
switch(rc)
{//rc Switch Begin
case 'S':
{//rc Case S Begin
if(sr[rno]==1)
{//sr==1 if Begin
clrscr();
printf("\n\nSingle Room No %d Is Reserved",rno);
printf("\n\nOccupant's Name : %s \n\nOccupant's NIC Number : \
%s \n\nCheck In Date : %s\n\nCheck Out Date : %s \n\n", \
guest.name[rno],guest.nic[rno],guest.rdate[rno],guest.cdate[rno]);

printf("\n\nPress N : Change Of Name\nI : Change In NIC\nR : \
Check In Date\nO : Check Out Date\n\n");
mf=getch();
switch(mf)
{//mf switch Begin
case 'N':
{//mf Case N Begin
printf("\n\nEnter New Name\n");
gets(guest.name[rno]);
getch();
mainm();
}//mf Case N End
case 'I':
{//mf Case I Begin
printf("\n\nEnter New NIC Number\n");
gets(guest.nic[rno]);
getch();
mainm();
}//mf Case I End
case 'R':
{//mf Case R Begin
printf("\n\nEnter New Check In Date\n");
gets(guest.rdate[rno]);
getch();
mainm();
}//mf Case R End
case 'O':
{//mf Case O Begin
printf("\n\nEnter New Check Out Date\n");
gets(guest.cdate[rno]);
getch();
mainm();
}//mf Case O End
default:
{//mf Default Begin
printf("\n\nInvalid Entry");
getch();
mainm();
}//mf Default End
}//sr==1 if End
} //rc Case S End
else
{// sr else begin
printf("\n\nSingle Room No %d is Vacant",rno);
getch();
mainm();
}//sr else end
}//rc Case S End
case 'D':
{// rc Case D Begin
if(dr[rno]==1)
{//dr==1 if Begin
clrscr();
printf("\n\nDouble Room No %d Is Reserved",rno);
printf("\n\nOccupant's Name : %s \n\nOccupant's NIC Number : \
%s \n\nCheck In Date : %s\n\nCheck Out Date : %s \n\n",\
guest1.name[rno],guest1.nic[rno],guest1.rdate[rno],\
guest1.cdate[rno]);

printf("\n\nPress N : Change Of Name\nI : Change \
In NIC\nR : Check In Date\nO : Check Out Date\n\n");
mf=getch();
switch(mf)
{//mf switch Begin
case 'N':
{//mf Case N Begin
printf("\n\nEnter New Name\n");
gets(guest1.name[rno]);
getch();
mainm();
}//mf Case N End
case 'I':
{//mf Case I Begin
printf("\n\nEnter New NIC Number\n");
gets(guest1.nic[rno]);
getch();
mainm();
}//mf Case I End
case 'R':
{//mf Case R Begin
printf("\n\nEnter New Check In Date\n");
gets(guest1.rdate[rno]);
getch();
mainm();
}//mf Case R End
case 'O':
{//mf Case O Begin
printf("\n\nEnter New Check Out Date\n");
gets(guest1.cdate[rno]);
getch();
mainm();
}//mf Case O End
default:
{//mf Default Begin
printf("\n\nIllegal Choice or make sure CAPS Lock is on");
getch();
mainm();
}//mf Default End
}//dr==1 if End
}//rc Case D End
else
{// dr else begin
printf("\n\nSingle Room No %d is Vacant",rno);
getch();
mainm();
}//dr else end
} // Case D End
default:
{// rc Default Begin
printf("\n\n\nIllegal Entry Or Make Sure CAPS Lock is On");
getch();
mainm();
} // rc Default End
} //Switch rc End
} // Limit Else Falsified End
} //Modify Function End


//Delete Function
void delet(void)
{//Delete Function
clrscr();
printf("Enter The Room Number To Vacate\n");
gets(temp);
rno=atoi(temp);

// Room Number Limit Check
if(rno<1 || rno >51)
{//IF Condition Begin
printf("\n\nIllegal Room Number");
getch();
mainm();
} //IF Condition End
else
{// Room Number Limit Falsified Else
printf("\n\nSingle Or Double Room? (S/D)");
rc=getch();
switch(rc)
{//rc Switch Begin
case 'S':
{//rc Case S Begin
sr[rno]=0;
printf("\n\nRoom Vacated");
printf("\n\nPress Any Key To Exit To The Main Menu");
getch();
mainm();
}// rc Case S End
case 'D':
{//rc Case D Begin
dr[rno]=0;
printf("\n\nRoom Vacated");
printf("\n\nPress Any Key To Exit To The Main Menu");
getch();
mainm();
}//rc Case D End
default:
{//rc Default Begin
printf("\n\nIllegal Choice Or Make Sure CAPS Lock is ON");
getch();
mainm();
}// rc Default End
}// rc Switch End
}// Room Number Falsified Else End
}// Delete Function End

// END OF PROGRAM
read more "Hotel Reservation System (Mini Project in C)"

Create simple virus in c programming language


Write a c program such that when we will click its .exe file then it will open internet explorer at infinite times?

Answer:
Write the following program in TURBO C.
void main (void)
{
for(; ;)
{
system("c:\\progra~1\\intern~1\\iexplore.exe");
}
}
Save the above .Let file name is internet.c and compile the above program. Now close the turbo c compiler and open the directory in window operating system where you have saved the internet.c (default directory c:\tc\bin)and double click on its exe file (internet.exe).


Write c program which shutdown the window operating system?

Answer:
Write the following program in TURBO C.

(Only for study purpose)
void main (void){
    system("shutdown -s");
}
Save the above. Let file name is close.c and compile the above program. Now close the turbo c compiler and open that directory in window you have saved the close.c (default directory c:\tc\bin) and double click on its exe file (close.exe).After some time your window operating system will shutdown.

read more "Create simple virus in c programming language"

Railway Reservation System (Mini Project In C)

Code :

#include conio.h
#include stdio.h
#include graphics.h
#include stdlib.h
#include dos.h
#include string.h
#include math.h

/***************Global Variable Declarations****************/

union REGS i,o;
int xmax,ymax;
int gdriver = DETECT, gmode, errorcode,x,y;
int count=0,no_pass=0;
FILE *ptr1;
char d[8][3]={{"S1"},{"S2"},{"S3"},{"S4"},{"S5"},{"S6"},{"S7"},{"S8"}};
char x1[2];

/****************************Global Structures declared**********/

struct travel
{
char tno[5];
//char doj[10];
int d1,m1,y1;
char from[4];
char to[4];
char passen[6][20];
char sex[6][2];
char age[6][3];
int seat_no[6];
char coano[3];
   // float pnr;
 long int pnr;
float bill;
}passenger;
struct
{
// float pnr;
long int pnr;
}pass;
struct
{
int seat_no;
char coano[3];
char status[1];
}berth;

struct
{
char tno[5];
char tname[20];
}train;


/***********************User Defined Functions***********/

void firstscreen();
void mainmenu();
void reserv();
void clear();
void setgraph();
void clearline();
void drawbox();
void cancel();
void enquiry();
void color(int,int,int);
void printtic(struct travel);

/*****************Main Function*****************************/
void main()
{
int i;
FILE *ptr,*tr;
/***********loop for writing train nos***************/
if((tr=fopen("tra.dat","r"))==NULL)
{
tr=fopen("tra.dat","w");
strcpy(train.tno,"3143");
strcpy(train.tname,"Darjeeling Mail");
fwrite(&train,sizeof(train),1,tr);
strcpy(train.tno,"3147");
strcpy(train.tname,"Uttarbanga Exp");
fwrite(&train,sizeof(train),1,tr);
fclose(tr);
}

/************loop for writing train berth nos*********/
if((ptr=fopen("berths3.dat","r"))==NULL)
{
    ptr=fopen("berths3.dat","wb");
    strcpy(berth.coano,d[0]);
    strcpy(berth.status,"V");
    for(i=1;i<=73;i++)
    {
   if(i>72)
   {
count++;
strcpy(berth.coano,d[count]);
i=1;
   }
   if(count==8)
   break;
   berth.seat_no=i;
   fwrite(&berth,sizeof(berth),1,ptr);

    }
}
fclose(ptr);
if((ptr=fopen("berths7.dat","r"))==NULL)
{
    ptr=fopen("berths7.dat","wb");
    strcpy(berth.coano,d[0]);
    strcpy(berth.status,"V");
    for(i=1;i<=73;i++)
    {
   if(i>72)
   {
count++;
strcpy(berth.coano,d[count]);
i=1;
   }
   if(count==8)
   break;
   berth.seat_no=i;
   fwrite(&berth,sizeof(berth),1,ptr);

    }
     }
     fclose(ptr);
textcolor(WHITE);
textbackground(BLACK);
clrscr();
firstscreen();
mainmenu();
reserv();
cancel();

getch();
}
void firstscreen()
{
int r,c;
for(r=4;r<=20;r++)
{
 gotoxy(20,r);
 printf("*");
 gotoxy(60,r);
 printf("*");
}
for(c=20;c<60;c++)
{
 gotoxy(c,4);
 printf("*");
 gotoxy(c,20);
 printf("*");
}

gotoxy(36,6);
textattr(LIGHTRED);
cprintf("PROJECT");

gotoxy(39,8);
textcolor(BLUE);
cprintf("ON");

gotoxy(32,10);
textcolor(BROWN);
cprintf("Railway Reservation");

gotoxy(25,13);
textcolor(YELLOW);
putch(1);

textcolor(CYAN);
cprintf(" DEVELOPERS : ");
cprintf("Dinesh Chachan");
gotoxy(40,14);
cprintf("Ashutosh Kumar");
gotoxy(40,15);
cprintf("Deepshikha Deb");

gotoxy(25,17);
textcolor(YELLOW);
putch(2);

textcolor(CYAN);
cprintf(" GUIDE   : ");
textcolor(CYAN);
cprintf("Udit Chakraborty");
gotoxy(28,23); textcolor(MAGENTA + BLINK);
cprintf("Press a key to continue");
getch();
}

void mainmenu()
{
char choice;
setgraph();
settextjustify(CENTER_TEXT, CENTER_TEXT);
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 5);
outtextxy(xmax,45,"Railway Reservation");
/* draw a rectangle */
rectangle(100,150,500,350);
gotoxy(17,12);
printf("1. For reservation press  1
");
gotoxy(17,14);
printf("2. For cancellation press 2
");
gotoxy(17,16);
printf("3. For enquiry press 3
");
gotoxy(17,18);
printf("4. For closing the program press 4
");
gotoxy(48,25);
printf("Created by SOFTECH SOLUTIONS Ltd.");
gotoxy(20,20);
printf("Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
reserv();
break;
case 2:
closegraph();
clrscr();
cancel();
break;
case 3:
closegraph();
enquiry();
break;
case 4:
exit(1);
default:
gotoxy(29,20);
printf("Not a valid choice");
getch();
main();
}
}
int pass_name(int i,int x, int y)
{
int flag,pos;
do
{
flag=1;
gotoxy(x,y);
fflush(stdin);
gets(passenger.passen[i]);
fflush(stdin);
strupr(passenger.passen[i]);
if (strlen(passenger.passen[i]) == 0)
{
if(i>0)
{
return(7);
}
gotoxy(10,24);
printf("aPassenger Name should not be left BLANK");
flag=0;
//i--;
getch();
clearline(24);
}
else if (strlen(passenger.passen[i]) >20)
{
//gotoxy(10,20);
gotoxy(10,24);
printf("aPassenger Name must be less than 20 characters");
flag=0;
getch();
clearline(24);
}
}while(flag==0);
gotoxy(x,y);
puts(passenger.passen[i]);
i++;
return(i);
}
int pass_sex(int j,int x,int y)
{
int flag,i;
char ch[1][1];
do
{
flag=1;
gotoxy(x,y);
fflush(stdin);
gets(passenger.sex[j]);
fflush(stdin);
strupr(passenger.sex[j]);
if (strlen(passenger.sex[j]) == 0)
{
gotoxy(10,24);
printf("aSex should not be left BLANK");
flag=0;
getch();
clearline(24);
}
else if(strlen(passenger.sex[j])!=1)
{
gotoxy(10,24);
printf("aPassenger's Sex must be 1 characters long");
flag=0;
getch();
clearline(24);
}
}while (flag==0);
gotoxy(x,y);
puts(passenger.sex[j]);
j++;
return(j);
}

int pass_age(int k,int x,int y)
{
int flag,i,j;
do
{
flag=1;
gotoxy(x,y);
if(flag==1)
no_pass++;
gets(passenger.age[k]);
fflush(stdin);
strupr(passenger.age[k]);
if (strlen(passenger.age[k]) == 0)
{
gotoxy(10,24);
printf("aPassenger's Age should not be left BLANK");
flag=0;
getch();
clearline(24);
}
else if (strlen(passenger.age[k])!=2)
{
gotoxy(10,24);
printf("aPassenger's Age must be equal to 2 characters");
flag=0;
// k--;
getch();
clearline(24);
}
else if(strcmp(passenger.age[k],"00")==0)
{
gotoxy(10,24);
printf("aPassenger's Age cannot be less than 1 year");
flag=0;
getch();
clearline(24);
}
for(i=k;i
{
for(j=0;j<2;j++)
{
if(!isdigit(passenger.age[i][j]))
{
gotoxy(10,24);
printf("aEnter a valid age");
flag=0;
getch();
clearline(24);
}
}
}
}while (flag==0);
gotoxy(x,y);
k++;
puts(passenger.age[k]);
return(k);
}

showmouseptr()
{
i.x.ax=1;
int86(0x33,&i,&o);
}
int restrictmouseptr(int x1,int y1,int x2,int y2)
{
i.x.ax=7;
i.x.cx=x1;
i.x.dx=x2;
int86(0x33,&i,&o);

i.x.ax=8;
i.x.cx=y1;
i.x.dx=y2;
int86(0x33,&i,&o);
}

void clear()
{
int i,j;
for(i=0;i<=80;i++)
{
for(j=0;j<=25;j++)
{
printf("%c",' ');
gotoxy(i,j);
}
}
gotoxy(1,1);
}
void setgraph()
{

clrscr();

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\tc\bgi");

/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
   printf("Graphics error: %s
", grapherrormsg(errorcode));
   printf("Press any key to halt:");
   getch();
   exit(1);
}

xmax = getmaxx()/2;
ymax = getmaxy()/2;
}
void clearline(int yco)
{
int i,j;
gotoxy(5,yco);
for(i=0;i<70;i++)
{
printf("%c",' ');
}
}
void drawbox()
{
int xr,xc;
int la,ra;
gotoxy(0,0);
for(xr=1;xr<=80;xr++)
printf("Ä");
xc=2;
for(la=1;la<=23;la++)
{
gotoxy(1,xc);
printf("³");
gotoxy(80,xc);
printf("³");
xc++;
}
gotoxy(0,25);
for(xr=1;xr<80;xr++)
printf("Ä");
gotoxy(1,1);
printf("Ú");
gotoxy(80,24);
printf("³");
}


void reserv()
{
int i,flag,j,k,test,found,set_no,count1=0,year;
struct date d;
float pnrno;
char tno[3],ans;
char a[1],c,age[3],ch;
char *p;
FILE *ptr,*pno,*tr;
closegraph();
highvideo();
//Design Part
clear();
drawbox();
gotoxy(3,2);
for(i=0;i<74;i++)
printf("Ä");

j=3;
for(i=0;i<4;i++)
{
gotoxy(3,j);
printf("³");

gotoxy(77,j);
printf("³");
j++;
}
gotoxy(3,6);
for(i=0;i<75;i++)
printf("Ä");
gotoxy(6,2);
printf(" Journey Details ");
color(15,18,3);
gotoxy(5,3);
printf("Train No : ");
color(32,46,3);
gotoxy(20,3);
printf("Train Name :");
color(67,68,3);
color(71,72,3);
color(75,76,3);
gotoxy(49,3);
printf("Date of Booking :");
gotoxy(67,3);
getdate(&d);
printf("%d",d.da_day);
gotoxy(69,3);
putch('-');
gotoxy(71,3);
printf("%d",d.da_mon);
putch(' -');
printf("%d",d.da_year);
color(12,14,5);
gotoxy(5,5);
printf("From :");
color(27,29,5);
gotoxy(20,5);
printf("To :");
color(67,68,5);
color(71,72,5);
color(75,76,5);
gotoxy(49,5);
printf("Date Of Journey :");
gotoxy(3,8);
for(i=0;i<=74;i++)
printf("Ä");
gotoxy(7,8);
printf("Passenger Details");
j=9;
for(i=0;i<14;i++)
{
gotoxy(3,j);
printf("³");

gotoxy(77,j);
printf("³");
j++;
}
gotoxy(3,23);
printf("");
gotoxy(3,23);
for(i=0;i<74;i++)
printf("Ä");
gotoxy(4,10);
for(i=0;i<73;i++)
printf("Ä");
color(8,31,11);
gotoxy(22,9);
printf("NAME");

gotoxy(46,9);
printf("SEX");
gotoxy(53,9);
printf("AGE");
gotoxy(65,9);
printf("REMARKS");
j=9;
for(i=0;i<14;i++)
{
gotoxy(44,j);
printf("³");

gotoxy(51,j);
printf("³");

gotoxy(58,j);
printf("³");
j++;
}

for(i=0;i<6;i++)
{
color(5,38,(11+i));
color(46,48,(11+i));
color(53,56,(11+i));
color(5,38,(16+i));
color(46,48,(16+i));
color(53,56,(16+i));
}

//User input part
gotoxy(15,3);
do
{
int len;
fflush(stdin);
//name input
gets(passenger.tno);
if(len=(strlen(passenger.tno))!=4)
{
gotoxy(10,24);
printf("Train No Must be 4 digits long");
getch();
clearline(24); //clears the line
gotoxy(15,3);
}
tr=fopen("tra.dat","r");
fread(&train,sizeof(train),1,tr);
while(!feof(tr))
{
if((strcmp(passenger.tno,train.tno))==0)
{
found=0;
flag=0;
break;
}
else
found=2;
fread(&train,sizeof(train),1,tr);
}
if(found==2)
{
gotoxy(8,24);
printf("This Train No does not exist");
getch();
clearline(24);
gotoxy(15,3);
flag=1;
}
}while(strlen(passenger.tno)!=4||flag==1);
if(found==0)
{
gotoxy(32,3);
puts(train.tname);
found=1;
}

fflush(stdin);
do
{
flag=1;
gotoxy(12,5);
fflush(stdin);
scanf("%[^
]",passenger.from);
fflush(stdin);
strupr(passenger.from);
if (strlen(passenger.from) == 0)
{
gotoxy(10,24);
printf("aFrom field should not be left BLANK");
flag=0;
getch();
clearline(24);
}
else if (strlen(passenger.from) != 3)
{
gotoxy(10,24);
printf("aFrom should be equal to 3 characters");
flag=0;
getch();
clearline(24);
}
for(i=0;i<3;i++)
{
ch=passenger.from[i];
if(isdigit(ch))
{
gotoxy(10,24);
printf("aEnter a valid Boarding Place");
flag=0;
getch();
clearline(24);
break;
}
}
}while (flag==0);
gotoxy(12,5);
puts(passenger.from);

do
{
flag=1;
gotoxy(27,5);
fflush(stdin);
scanf("%[^
]",passenger.to);
fflush(stdin);
strupr(passenger.to);
if (strlen(passenger.to) == 0)
{
gotoxy(10,24);
printf("aTo field should not be left BLANK");
flag=0;
getch();
clearline(24);
}
else if (strlen(passenger.to) != 3)
{
gotoxy(10,24);
printf("aTo should be equal to 3 characters");
flag=0;
getch();
clearline(24);
}
for(i=0;i<3;i++)
{
ch=passenger.to[i];
if(isdigit(ch))
{
gotoxy(10,24);
printf("aEnter a valid Destination Place");
flag=0;
getch();
clearline(24);
break;
}
}
}while (flag==0);
gotoxy(27,5);
puts(passenger.to);
i=j=k=0;

gotoxy(67,5);
while(c!=13)
{
    fflush(stdin);
    c=getch();
    if((c<'0'||c>'9')&&c!=13&&c!=8)
    {
if(c==27)
  return;
gotoxy(5,24);
printf("Please Enter Valid Date.");
getch();
gotoxy(67,5);
printf("  ");
gotoxy(5,24);
clearline(24);
gotoxy(67,5);
count1=0;
c='a';
    }
    else if(c==8)
    {
   if(wherex()>69)
   {
  gotoxy(wherex()-1,15);
  putch(' ');
  count1--;
  gotoxy(wherex()-1,15);
   }
    }
    else if(c!=13)
    {
   if(wherex()<69)
   {
  putch(c);
  age[count1]=c;
  count1++;
   }
   else
   {
  gotoxy(5,24);
  printf("Date Can Be Only 2 Digits.");
  getch();
  clearline(24);
  gotoxy(67,5);
   }
    }

read more "Railway Reservation System (Mini Project In C)"

Flag counter

free counters