Thread: A few programs in C

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    10

    Post A few programs in C

    You all helped me write these so, here they are:
    Code:
    // ASCII Converter
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    {
      char c, i = 'y';
      
      while(i != 'n')
      {
          printf("\nEnter symbols (chars or nums):  ");
          scanf("%c", &c);
          fflush(stdin);
          printf("\nThe ASCII code for %c is %d.\n", c, c);
          printf("\nConvert another symbol? yes or no:  ");
          scanf("%c", &i);
          fflush(stdin);
    //    system("cls");
     }
      
      return 0;
    }
    Code:
    /*
    ** Creates a harmless Message Window 
    ** scares the willies outa the user...
    **
    ** written in C++ for win32
    */
    
    #include <windows.h>
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
       int i = 0;
       
       while(i == 0)
       {
          if(MessageBox
             (NULL,
             "Format Drive C:?",
             "Format",
             MB_YESNO | MB_ICONINFORMATION)
             != IDYES)
          (MessageBox
             (NULL,
             "Warning: All data on non-removable\n         disk drive C: will be lost!\n\nContinue with format?",
             "Format",
             MB_YESNO | MB_ICONINFORMATION)
             != IDYES);
          MessageBox
             (NULL,
             "Format: Checking existing disk format.",
             "Format", 0);
          MessageBox
             (NULL,
             "Format: Formatting drive C:",
             "Format", 0);
          if(MessageBox
             (NULL,
             "Format another disk?",
             "Format",
             MB_YESNO | MB_ICONINFORMATION)
             != IDYES)
             i = 1;
       }
       return 0;
    }
    Code:
    // Empties Recycle Bin
    #include <windows.h>
    #include <shlobj.h>
    #define WIN32_LEAN_AND_MEAN
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
    PSTR szCmdLine, int iCmdShow)
    {
    
    //if(MessageBox(NULL, "Press ok to empty the Recycle Bin.", "recycler", MB_YESNO | MB_ICONINFORMATION) != IDYES)
    //return FALSE;
    
    SHEmptyRecycleBin(NULL, "", 0);
    
    return FALSE ;
    }
    Code:
    // Numbers Game
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void Start(int);
    int GetResults(int, int, int, int, char);
    
    int main()
    {
        int life = 5;
        
        printf("\n\n             ** Jackpot Game **\n");
        printf("  The goal of this game is to guess a number.\n\n");
        printf("              You get 5 lives.\n\n");
        printf("Jackpot will tell you if the number is too big\n");
        printf("  or too small compared to the secret number.\n\n");
        system("pause");
        Start(life);
        return 0;
    }
    
    void Start(int life)
    {
        int  guess = 0, low = 0, high = 30, secret = 0, j = 0, k = 0;
        char c = '1';
        
        while(c)
        {
            system("cls");
            printf("Select difficulty mode:\n\n");
            printf(" 0 : EXIT\n");
            printf(" 1 : Easy      (0 - 15)\n");
            printf(" 2 : Medium    (0 - 30)\n");
            printf(" 3 : Difficult (0 - 60)\n\n");
            printf("Enter a level: ");
            scanf("%c",&c);
            fflush(stdin);
            system("cls");
            if (c != '0');
            {
                switch (c)
                {
                    case '1' : high = 15;  // the random number will be between 0 and maxrand
                    break;
                    case '2' : high = 30;
                    break;
                    case '3' : high = 60;
                    break;
                    default  : exit (0);
                    return;
                    break;
                }
            }
            srand(time(0));            // init Rand() function
            secret = rand() % high;    // secret = a random value between 0 and high
            GetResults(life, low, high, secret, c);
        }
    }
    
    int GetResults (int life, int low, int high, int secret, char c)
    {
        int guess = 0, k = 0;
        
        while(k == 0)
        {
            printf("\nThe number is between %d and %d\n", low, high);
            printf("Number of remaining life: %d\n", life);
            printf("\nType a number: ");
            scanf("%d", &guess);           // read user's number
            fflush(stdin);
            
            if (life <= 0) // player has no more life
            {
                system("cls");
                printf("\n\nYou lose!\n\n");
                system("pause");
                return life;
            }
            
            if (guess == secret) // player found the secret number
            {
                system("cls");
                printf("\n\nYOU WIN !\n\n");
                system("pause");
                return life;
            }
            
            if ((guess > high) || (guess < low)) // 0 > guess > high - restart
            {
                system("cls");
                printf("\n\nError : Number not between %d and %d\n", low, high);
            }
            
            else
            {
                system("cls");
                life = life - 1;
                if (guess > secret)
                {
                    high = guess;
                }
                
                if (guess < secret)
                {
                    low = guess;
                }
            }
        }
    }
    Code:
    /* Renews IP Address w/o mucking about in command prompt */
    
    #include <windows.h>
    #include <stdio.h>
    
    int main(void)
    {
       /* TODO (#1#): supress command and activate messagebox */
       system("ipconfig /release_all");
       system("ipconfig /renew_all");
    //   system("ipconfig /batch RenewIP.dat");
    
       system("pause");
       
    //   MessageBox (NULL, "Your IP Has Been Renewed" , "Renew IP", 0);
       return 0;
    }
    Hope they help someone...particularly the new programmers.
    These were all projects I used to learn C...Good luck.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    fflush(stdin);
    See the FAQ for why this is wrong.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Starting programs
    By Molokai in forum C Programming
    Replies: 1
    Last Post: 04-16-2009, 10:10 AM
  2. Recommend upgrade path for C programs
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-22-2007, 07:32 AM
  3. Communication between programs?
    By johny145 in forum Windows Programming
    Replies: 3
    Last Post: 06-01-2005, 10:14 PM
  4. POSIX/DOS programs?
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 02-28-2003, 05:42 AM
  5. executing c++ programs on the web
    By gulti01 in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2002, 03:12 AM