Thread: Would like some feedback on my first somewhat useful program (Subnet Calculator)

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    3

    Would like some feedback on my first somewhat useful program (Subnet Calculator)

    Hi all....

    as part of our training we had to develop a subnet calculator; I feel that I'm pretty much done with it, just needs some fine tuning.

    one issue specificly that I'm looking for feedback on is the validation of the IP address. I know there must be a better way of doing it that I'm currently using but I'm just not quite sure how.

    Currently it works fine; except when the user inputs an IP address they can enter too many fields and incorrect seperators.

    ex. 12.23.34.45.56.67 will be saved as 12.23.34.45/56
    12/23/34/45/56 will be saved as 12.23.34.45/56

    I'm not really sure how to force them to enter the information in the proper format.


    here is the code ( warning it's kinda long... but it's my first attempt and didn't wanna focus on making in smaller until i get it working exactly the way i want )

    platform: windows platform using code blocks as the IDE

    Thanks for any help

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <conio.h>
    
    /******************************************************
    DECLARE VARIABLES
    *******************************************************/
    int oct1, oct2, oct3, oct4, cidr;
    int noct1=0, noct2=0, noct3=0, noct4=0;
    int boct1=255, boct2=255, boct3=255, boct4=255;
    int mask1=0, mask2=0, mask3=0, mask4=0;
    int netbits=0,bitsbor, maskSum, hostbits, netinc, cond, ans, hosts, nets, i;
    char classtype;
    
    /******************************************************
    START MAIN PROGRAM
    *******************************************************/
    int main (void)  {
    
        HANDLE  hConsole;
        hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    
        #define BLACK 0
        #define BLUE 1
        #define GREEN 2
        #define CYAN 3
        #define RED 4
        #define MAGENTA 5
        #define BROWN 6
        #define LIGHTGREY 7
        #define DARKGREY 8
        #define LIGHTBLUE 9
        #define LIGHTGREEN 10
        #define LIGHTCYAN 11
        #define LIGHTRED 12
        #define LIGHTMAGENTA 13
        #define YELLOW 14
        #define WHITE 15
        #define BLINK 128
    
    /******************************************************
    GET USER INPUT
    *******************************************************/
        do{
            system ("cls");
            system ("title Subnet Calculator");
            system ("mode con cols=65 lines=40");
            SetConsoleTextAttribute(hConsole, 11);
            printf ("Enter your IP address and subnet mask using CIDR notation:\n\n");
            SetConsoleTextAttribute(hConsole, 10);
            printf (">>> ");
            SetConsoleTextAttribute(hConsole, 14);
            printf (" ");
            scanf ("%d %*c %d %*c %d %*c %d %*c %d", &oct1, &oct2, &oct3, &oct4, &cidr);
    
            if (oct1>223 || oct2>255 || oct3>255 || oct4>255 || cidr<8 || cidr>30){
                system ("cls");
                SetConsoleTextAttribute(hConsole, 12);
                printf ("\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\n\n\n            You have entered an invalid IP address or CIDR.\n\n\n                   ***PROGRAM WILL SELF DESTRUCT***\n\n\n");
                system ("pause");
                exit (0);
            }
    
    /******************************************************
    DETERMINE CLASS TYPE
    *******************************************************/
            if (oct1>=1 && oct1<=127){
                classtype = 'A';
                netbits = 8;
            }
            else if (oct1>=128 && oct1<=191){
                classtype = 'B';
                netbits - 16;
            }
            else if (oct1>=192 && oct1<=223){
                classtype = 'C';
                netbits = 24;
            }
    
    /******************************************************
    DETERMINE BIT VALUE
    *******************************************************/
            bitsbor = cidr-netbits;
            if (bitsbor>16 && bitsbor<24){
                cond=bitsbor-16;
            }
            else if (bitsbor>8 && bitsbor<16){
                cond=bitsbor-8;
            }
            else {
                cond=bitsbor;
            }
            switch (cond) {
                case 1:
                    netinc = 128;
                    maskSum = 128;
                    break;
                case 2:
                    netinc = 64;
                    maskSum = 192;
                    break;
                case 3:
                    netinc = 32;
                    maskSum = 224;
                    break;
                case 4:
                    netinc = 16;
                    maskSum = 240;
                    break;
                case 5:
                    netinc = 8;
                    maskSum = 248;
                    break;
                case 6:
                    netinc = 4;
                    maskSum = 252;
                    break;
                case 7:
                    netinc = 2;
                    maskSum = 254;
                    break;
                case 8:
                    netinc = 1;
                    maskSum = 255;
                    break;
            }
    
    /******************************************************
    DETERMINE SUBNET MASK, NETWORK ID, AND BROADCAST ID
    *******************************************************/
            if (cidr>=24 && cidr<=30){
                mask1=255;
                mask2=255;
                mask3=255;
                mask4=maskSum;
                boct1=oct1;
                boct2=oct2;
                boct3=oct3;
                noct1=oct1;
                noct2=oct2;
                noct3=oct3;
                for (i=256; i>=oct4; i-=netinc){
                }
                noct4=i;
                boct4=noct4+netinc-1;
            }
    
            if (cidr>=16 && cidr <=23){
                mask1=255;
                mask2=255;
                mask3=maskSum;
                boct1=oct1;
                boct2=oct2;
                noct1=oct1;
                noct2=oct2;
                for (i=256; i>=oct3; i-=netinc){
                }
                noct3=i;
                boct3=noct3+netinc-1;
            }
    
            if (cidr>=8 && cidr<=15){
                mask1=255;
                mask2=maskSum;
                boct1=oct1;
                noct1=oct1;
                for (i=256; i>=oct2; i-=netinc){
                }
                noct2=i;
                boct2=noct2+netinc-1;
            }
    
    /******************************************************
    DETERMINE NUMBER OF AVAILABLE HOSTS & NETWORKS
    *******************************************************/
            hostbits=32-cidr;
            hosts=pow (2, hostbits)-2;
            nets=pow(2, bitsbor);
    
    /******************************************************
    DISPLAY RESULTS
    *******************************************************/
            printf ("\n\n\n");
    
            SetConsoleTextAttribute(hConsole, 10);
            printf ("You entered ............................... ");
            SetConsoleTextAttribute(hConsole, 13);
            printf ("%d.%d.%d.%d /%d\n", oct1, oct2, oct3, oct4, cidr);
    
            SetConsoleTextAttribute(hConsole, 10);
            printf ("Subnet Mask ............................... ");
            SetConsoleTextAttribute(hConsole, 13);
            printf ("%d.%d.%d.%d\n\n\n", mask1, mask2, mask3, mask4);
    
            SetConsoleTextAttribute(hConsole, 10);
            printf ("Class ..................................... ");
            SetConsoleTextAttribute(hConsole, 13);
            printf ("%c\n\n", classtype);
    
            SetConsoleTextAttribute(hConsole, 10);
            printf ("Network ID ................................ ");
            SetConsoleTextAttribute(hConsole, 13);
            printf ("%d.%d.%d.%d\n", noct1, noct2, noct3, noct4);
    
            SetConsoleTextAttribute(hConsole, 10);
            printf ("First assignable Address .................. ");
            SetConsoleTextAttribute(hConsole, 13);
            printf ("%d.%d.%d.%d\n", noct1, noct2, noct3, noct4+1);
    
            SetConsoleTextAttribute(hConsole, 10);
            printf ("Last assignable Address ................... ");
            SetConsoleTextAttribute(hConsole, 13);
            printf ("%d.%d.%d.%d\n", boct1, boct2, boct3, boct4-1);
    
            SetConsoleTextAttribute(hConsole, 10);
            printf ("Broadcast ID .............................. ");
            SetConsoleTextAttribute(hConsole, 13);
            printf ("%d.%d.%d.%d\n\n", boct1, boct2, boct3, boct4);
    
            SetConsoleTextAttribute(hConsole, 10);
            printf ("Borrowed bits ............................. ");
            SetConsoleTextAttribute(hConsole, 13);
            printf ("%d\n", bitsbor);
    
            SetConsoleTextAttribute(hConsole, 10);
            printf ("Subnet increments ......................... ");
            SetConsoleTextAttribute(hConsole, 13);
            printf ("%d\n", netinc);
    
            SetConsoleTextAttribute(hConsole, 10);
            printf ("Available subnets ......................... ");
            SetConsoleTextAttribute(hConsole, 13);
            printf ("%d\n", nets);
    
            SetConsoleTextAttribute(hConsole, 10);
            printf ("Available hosts per subnet ................ ");
            SetConsoleTextAttribute(hConsole, 13);
            printf ("%d\n\n\n", hosts);
    
            SetConsoleTextAttribute(hConsole, 11);
            printf ("Would you like to enter another IP address?\n\nEnter 1 to continue\nEnter 0 to exit\n\n");
            SetConsoleTextAttribute(hConsole, 10);
            printf (">>> ");
            SetConsoleTextAttribute(hConsole, 14);
            scanf ("%d", &ans);
    
        } while (ans==1);
    
        SetConsoleTextAttribute(hConsole, 10);
        printf ("\n\n\n\n");
    
        return 0;
    }  //END MAIN PROGRAM
    Last edited by digital_alchemy; 07-20-2010 at 07:38 PM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, if you want basic feedback: your indentation is broken.
    Code in main is not indented. Code inside loop is not indented.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    55
    Some comments would help other people understand your code more easily...

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    3
    Quote Originally Posted by Elysia View Post
    Well, if you want basic feedback: your indentation is broken.
    Code in main is not indented. Code inside loop is not indented.

    Well that's fine, but I mentioned a specific area that i'm looking for advice on as well.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You can't force the user to do anything. The right approach is to decide what action you want to take on bogus input (ask him to reenter the input, etc.). The user can be a monkey hitting random keys on the keyboard, and usually it is (figuratively speaking).
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm sure people with experience with subnets will offer advice, as well, in due time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In your error checking routine after the
    Code:
    system("pause");
    replace the
    Code:
    exit(0);
    with
    Code:
    continue;
    Also why are all of your variables global?

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    3
    Quote Originally Posted by jimblumberg View Post
    In your error checking routine after the
    Code:
    system("pause");
    replace the
    Code:
    exit(0);
    with
    Code:
    continue;
    Also why are all of your variables global?

    I tried replacing the code you suggested, but the only real difference is that it attempts to execute the rest of the program and exits anyway. I'm going to have to rewrite that section anyway.... I need to prompt the user to correct their input if they enter an invalid IP address.

    as far as the global variables go... That's just the way I originally wrote it, and I didn't figure it would present a problem. (actually as I just tyed that I think I may have thought of an instance where that would present a problem... but I gotta run right now.. gotta get back to class).

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  2. help with calculator program.
    By sadr in forum C Programming
    Replies: 9
    Last Post: 09-10-2009, 10:30 AM
  3. Need some help with Calculator program
    By Sshakey6791 in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2008, 09:36 AM
  4. Help with calculator program.
    By banjordan in forum C++ Programming
    Replies: 1
    Last Post: 12-03-2003, 10:01 PM
  5. Newbie Needs Help on Calculator Program
    By CompWiz84 in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2002, 02:21 PM